Debian Codegraph @

src/doc/manual/extensions/mp_ref_atomic.txi

raw
1 @node Atomic operations
2 @subsection Atomic operations
3 ECL supports both compare-and-swap and fetch-and-add (which may be
4 faster on some processors) atomic operations on a number of different
5 places. The compare-and-swap macro is user extensible with a protocol
6 similar to @code{setf}.
7
8 @node Atomic operations dictionary
9 @subsection Atomic operations dictionary
10
11 @subsubheading C Reference
12 @cppdef ecl_compare_and_swap
13 @deftypefun cl_object ecl_compare_and_swap (cl_object *slot, cl_object old, cl_object new)
14
15 Perform an atomic compare and swap operation on @var{slot} and return
16 the previous value stored in @var{slot}. If the return value is equal
17 to @var{old} (comparison by @code{==}), the operation has succeeded.
18 This is a inline-only function defined in "ecl/ecl_atomics.h".
19 @end deftypefun
20
21 @cppdef ecl_atomic_incf
22 @deftypefun cl_object ecl_atomic_incf (cl_object *slot, cl_object increment)
23 @end deftypefun
24 @cppdef ecl_atomic_incf_by_fixnum
25 @deftypefun cl_object ecl_atomic_incf_by_fixnum (cl_object *slot, cl_fixnum increment)
26
27 Atomically increment @var{slot} by the given increment and return the
28 previous value stored in @var{slot}. The consequences are undefined if
29 the value of @var{slot} is not of type @code{fixnum}.
30 @coderef{ecl_atomic_incf} signals an error if @var{increment} is not of
31 type @code{fixnum}. This is a inline-only function defined in
32 "ecl/ecl_atomics.h".
33 @end deftypefun
34
35 @cppdef ecl_atomic_index_incf
36 @deftypefun cl_index ecl_atomic_index_incf (cl_index *slot);
37
38 Atomically increment @var{slot} by 1 and return the new value stored
39 in @var{slot}.
40 @end deftypefun
41
42 @cppdef ecl_atomic_get
43 @deftypefun cl_object ecl_atomic_get (cl_object *slot)
44
45 Perform a volatile load of the object in @var{slot} and then
46 atomically set @var{slot} to @code{ECL_NIL}. Returns the value
47 previously stored in @var{slot}.
48 @end deftypefun
49
50 @cppdef ecl_atomic_push
51 @deftypefun void ecl_atomic_push (cl_object *slot, cl_object o)
52 @end deftypefun
53 @cppdef ecl_atomic_pop
54 @deftypefun cl_object ecl_atomic_pop (cl_object *slot)
55
56 Like push/pop but atomic.
57 @end deftypefun
58
59 @subsubheading Lisp Reference
60 @lspdef mp:atomic-incf
61 @lspdef mp:atomic-decf
62 @defmac mp:atomic-incf place &optional (increment 1)
63 @defmacx mp:atomic-decf place &optional (increment 1)
64
65 Atomically increments/decrements the fixnum stored in @var{place} by
66 the given @var{increment} and returns the value of @var{place} before
67 the increment. Incrementing and decrementing is done using modular
68 arithmetic, so that @coderef{mp:atomic-incf} of a place whose value is
69 @code{most-positive-fixnum} by 1 results in
70 @code{most-negative-fixnum} stored in place.
71
72 Currently the following places are supported:
73
74 @code{car}, @code{cdr}, @code{first}, @code{rest}, @code{svref},
75 @code{symbol-value}, @code{slot-value},
76 @code{clos:standard-instance-access},
77 @code{clos:funcallable-standard-instance-access}.
78
79 For @code{slot-value}, the object should have no applicable methods
80 defined for @code{slot-value-using-class} or @code{(setf
81 slot-value-using-class)}.
82
83 The consequences are undefined if the value of @var{place} is not of
84 type @code{fixnum}.
85 @end defmac
86
87 @lspdef mp:compare-and-swap
88 @defmac mp:compare-and-swap place old new
89
90 Atomically stores @var{new} in @var{place} if @var{old} is @code{eq}
91 to the current value of @var{place}. Returns the previous value of
92 @var{place}: if the returned value is @code{eq} to @var{old}, the swap
93 was carried out.
94
95 Currently, the following places are supported:
96
97 @code{car}, @code{cdr}, @code{first}, @code{rest}, @code{svref},
98 @code{symbol-plist}, @code{symbol-value}, @code{slot-value},
99 @code{clos:standard-instance-access},
100 @code{clos:funcallable-standard-instance-access}, a structure slot
101 accessor@footnote{The creation of atomic structure slot accessors can be
102 deactivated by supplying a @code{(:atomic-accessors nil)} option to
103 @code{defstruct}.} or any other place for which a compare-and-swap
104 expansion was defined by @coderef{mp:defcas} or
105 @coderef{mp:define-cas-expander}.
106
107 For @code{slot-value}, @code{slot-unbound} is called if the slot is
108 unbound unless @var{old} is @code{eq} to @code{si:unbound}, in which
109 case @var{old} is returned and @var{new} is assigned to the slot.
110 Additionally, the object should have no applicable methods defined for
111 @code{slot-value-using-class} or @code{(setf slot-value-using-class)}.
112 @end defmac
113
114 @lspdef mp:atomic-update
115 @defmac mp:atomic-update place update-fn &rest arguments
116
117 Atomically updates the CAS-able @var{place} to the value returned by
118 calling @var{update-fn} with @var{arguments} and the old value of
119 @var{place}. @var{update-fn} must be a function accepting @code{(1+
120 (length arguments))} arguments. Returns the new value which was stored
121 in @var{place}.
122
123 @var{place} may be read and @var{update-fn} may be called more than
124 once if multiple threads are trying to write to @var{place} at the
125 same time.
126
127 @exindex Atomic update of a structure slot
128 Example:
129
130 Atomic update of a structure slot. If the update would not be atomic,
131 the result would be unpredictable.
132 @lisp
133 (defstruct test-struct
134 (slot1 0))
135 (let ((struct (make-test-struct)))
136 (mapc #'mp:process-join
137 (loop repeat 100
138 collect (mp:process-run-function
139 ""
140 (lambda ()
141 (loop repeat 1000 do
142 (mp:atomic-update (test-struct-slot1 struct) #'1+)
143 (sleep 0.00001))))))
144 (test-struct-slot1 struct))
145 => 100000
146 @end lisp
147 @end defmac
148
149 @lspdef mp:atomic-push
150 @lspdef mp:atomic-pop
151 @defmac mp:atomic-push obj place
152 @defmacx mp:atomic-pop place
153
154 Like @code{push}/@code{pop}, but atomic. @var{place} must be CAS-able
155 and may be read multiple times before the update succeeds.
156 @end defmac
157
158 @lspdef mp:define-cas-expander
159 @defmac mp:define-cas-expander accessor lambda-list &body body
160
161 Define a compare-and-swap expander similar to
162 @code{define-setf-expander}. Defines the compare-and-swap-expander for
163 generalized-variables @code{(accessor ...)}. When a form
164 @code{(mp:compare-and-swap (accessor arg1 ... argn) old new)} is
165 evaluated, the forms given in the body of
166 @coderef{mp:define-cas-expander} are evaluated in order with the
167 parameters in @code{lambda-list} bound to @code{arg1 ... argn}. The
168 body must return six values
169 @lisp
170 (var1 ... vark)
171 (form1 ... formk)
172 old-var
173 new-var
174 compare-and-swap-form
175 volatile-access-form
176 @end lisp
177 in order (Note that @code{old-var} and @code{new-var} are single
178 variables, unlike in @code{define-setf-expander}). The whole
179 @code{compare-and-swap} form is then expanded into
180 @lisp
181 (let* ((var1 from1) ... (vark formk)
182 (old-var old-form)
183 (new-var new-form))
184 compare-and-swap-form).
185 @end lisp
186 Note that it is up to the user of this macro to ensure atomicity for
187 the resulting compare-and-swap expansions.
188
189 @exindex Define a compare-and-swap expansion
190 @paragraph Example
191
192 @coderef{mp:define-cas-expander} can be used to define a more
193 convienient compare-and-swap expansion for a class slot. Consider the
194 following class:
195 @lisp
196 (defclass food ()
197 ((name :initarg :name)
198 (deliciousness :initform 5 :type '(integer 0 10)
199 :accessor food-deliciousness)))
200
201 (defvar *spätzle* (make-instance 'food :name "Spätzle"))
202 @end lisp
203 We can't just use @coderef{mp:compare-and-swap} on
204 @code{*spätzle*}:
205 @lisp
206 > (mp:compare-and-swap (food-deliciousness *x*) 5 10)
207
208 Condition of type: SIMPLE-ERROR
209 Cannot get the compare-and-swap expansion of (FOOD-DELICIOUSNESS *X*).
210 @end lisp
211 We can use @code{symbol-value}, but let's define a more convenient
212 compare-and-swap expander:
213 @lisp
214 (mp:define-cas-expander food-deliciousness (food)
215 (let ((old (gensym))
216 (new (gensym)))
217 (values nil nil old new
218 `(progn (check-type ,new (integer 0 10))
219 (mp:compare-and-swap (slot-value ,food 'deliciousness)
220 ,old ,new))
221 `(food-deliciousness ,food))))
222 @end lisp
223 Now finally, we can safely store our rating:
224 @lisp
225 > (mp:compare-and-swap (food-deliciousness *spätzle*) 5 10)
226
227 5
228 @end lisp
229 @end defmac
230
231 @lspdef mp:defcas
232 @defmac mp:defcas accessor cas-fun &optional documentation
233
234 Define a compare-and-swap expansion similar to the short form
235 of @code{defsetf}. Defines an expansion
236 @lisp
237 (compare-and-swap (accessor arg1 ... argn) old new)
238 => (cas-fun arg1 ... argn old new)
239 @end lisp
240 Note that it is up to the user of this macro to ensure atomicity for
241 the resulting compare-and-swap expansions.
242 @end defmac
243
244 @lspdef mp:remcas
245 @defun mp:remcas symbol
246
247 Remove a compare-and-swap expansion. It is an equivalent of
248 @code{fmakunbound (setf symbol)} for cas expansions.
249 @end defun
250
251 @lspdef mp:get-cas-expansion
252 @defun mp:get-cas-expansion place &optional environment
253
254 Returns the compare-and-swap expansion forms and variables as defined in
255 @coderef{mp:define-cas-expander} for @var{place} as six values.
256 @end defun