-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtvar.lisp
73 lines (57 loc) · 2.16 KB
/
tvar.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;; -*- lisp -*-
;; This file is part of STMX.
;; Copyright (c) 2013-2016 Massimiliano Ghilardi
;;
;; This library is free software: you can redistribute it and/or
;; modify it under the terms of the Lisp Lesser General Public License
;; (http://opensource.franz.com/preamble.html), known as the LLGPL.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty
;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;; See the Lisp Lesser General Public License for more details.
(in-package :stmx.util)
;;;; ** Transactional cell implemented with a TVAR
;;; Max: we could use the same trick as in tcell.lisp:
;;; a special *empty-tvar* value to mean "cell is empty".
;;; Anyway, using tvar functions bound-$? and unbind-$ is less verbose
;;; and feels more "natural".
;; no need to wrap empty? in a transaction:
;; bound-$? is atomic, transaction aware, and performs a single read
(defmethod empty? ((var tvar))
(not (bound-$? var)))
(defmethod empty! ((var tvar))
"Remove value from tvar."
(fast-atomic
(unbind-$ var)))
;; no need to specialize (full?) on TVARs: the method in container.lisp is enough
;;
;; (defmethod full? ((var tvar))
;; (not (empty? var)))
(defmethod peek ((var tvar) &optional default)
(peek-$ var default))
(defmethod take ((var tvar))
(fast-atomic
(multiple-value-bind (took? value) (try-take-$ var)
(if took?
value
(retry)))))
(defmethod put ((var tvar) value)
(fast-atomic
(if (try-put-$ var value)
value
(retry))))
(defmethod try-take ((var tvar))
"hand-made, nonblocking version of (take place) for TVARs.
Less general but approx. 3 times faster (on SBCL 1.0.57.0.debian,
Linux amd64) than the unspecialized (try-take place) which calls
\(atomic (nonblocking (take place)))"
(fast-atomic
(try-take-$ var)))
(defmethod try-put ((var tvar) value)
"hand-made, nonblocking version of (put place) for TVARs.
Less general but approx. 3 times faster (on SBCL 1.0.57.0.debian,
Linux amd64) than the unspecialized (try-put place) which calls
\(atomic (nonblocking (put place value)))"
(fast-atomic
(try-put-$ var value)))