Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / code / cas.lisp
bloba73cef449343ab2a6fc327477405f41ffcb6ff4f
1 (in-package "SB!IMPL")
3 ;;;; COMPARE-AND-SWAP
4 ;;;;
5 ;;;; SB-EXT:COMPARE-AND-SWAP is the public API for now.
6 ;;;;
7 ;;;; Internally our interface has CAS, GET-CAS-EXPANSION, DEFINE-CAS-EXPANDER,
8 ;;;; DEFCAS, and #'(CAS ...) functions -- making things mostly isomorphic with
9 ;;;; SETF.
11 ;;; This is what it all comes down to.
12 (def!macro cas (place old new &environment env)
13 #!+sb-doc
14 "Synonym for COMPARE-AND-SWAP.
16 Additionally DEFUN, DEFGENERIC, DEFMETHOD, FLET, and LABELS can be also used to
17 define CAS-functions analogously to SETF-functions:
19 (defvar *foo* nil)
21 (defun (cas foo) (old new)
22 (cas (symbol-value '*foo*) old new))
24 First argument of a CAS function is the expected old value, and the second
25 argument of is the new value. Note that the system provides no automatic
26 atomicity for CAS functions, nor can it verify that they are atomic: it is up
27 to the implementor of a CAS function to ensure its atomicity.
29 EXPERIMENTAL: Interface subject to change."
30 (multiple-value-bind (temps place-args old-temp new-temp cas-form)
31 (get-cas-expansion place env)
32 `(let* (,@(mapcar #'list temps place-args)
33 (,old-temp ,old)
34 (,new-temp ,new))
35 ,cas-form)))
37 (defun get-cas-expansion (place &optional environment)
38 #!+sb-doc
39 "Analogous to GET-SETF-EXPANSION. Returns the following six values:
41 * list of temporary variables
43 * list of value-forms whose results those variable must be bound
45 * temporary variable for the old value of PLACE
47 * temporary variable for the new value of PLACE
49 * form using the aforementioned temporaries which performs the
50 compare-and-swap operation on PLACE
52 * form using the aforementioned temporaries with which to perform a volatile
53 read of PLACE
55 Example:
57 (get-cas-expansion '(car x))
58 ; => (#:CONS871), (X), #:OLD872, #:NEW873,
59 ; (SB-KERNEL:%COMPARE-AND-SWAP-CAR #:CONS871 #:OLD872 :NEW873).
60 ; (CAR #:CONS871)
62 (defmacro my-atomic-incf (place &optional (delta 1) &environment env)
63 (multiple-value-bind (vars vals old new cas-form read-form)
64 (get-cas-expansion place env)
65 (let ((delta-value (gensym \"DELTA\")))
66 `(let* (,@(mapcar 'list vars vals)
67 (,old ,read-form)
68 (,delta-value ,delta)
69 (,new (+ ,old ,delta-value)))
70 (loop until (eq ,old (setf ,old ,cas-form))
71 do (setf ,new (+ ,old ,delta-value)))
72 ,new))))
74 EXPERIMENTAL: Interface subject to change."
75 ;; FIXME: this seems wrong on two points:
76 ;; 1. if TRULY-THE had a CAS expander (which it doesn't) we'd want
77 ;; to use %MACROEXPAND[-1] so as not to lose the "truly-the"-ness
78 ;; 2. if both a CAS expander and a macro exist, the CAS expander
79 ;; should be preferred before macroexpanding (just like SETF does)
80 (let ((expanded (sb!xc:macroexpand place environment)))
81 (flet ((invalid-place ()
82 (error "Invalid place to CAS: ~S -> ~S" place expanded)))
83 (unless (consp expanded)
84 ;; FIXME: Allow (CAS *FOO* <OLD> <NEW>), maybe?
85 (invalid-place))
86 (let ((name (car expanded)))
87 (unless (symbolp name)
88 (invalid-place))
89 (acond
90 ((info :cas :expander name)
91 ;; CAS expander.
92 (funcall it expanded environment))
94 ;; Structure accessor
95 ((structure-instance-accessor-p name)
96 (expand-structure-slot-cas it name expanded))
98 ;; CAS function
100 (with-unique-names (old new)
101 (let ((vars nil)
102 (vals nil)
103 (args nil))
104 (dolist (x (reverse (cdr expanded)))
105 (cond ((constantp x environment)
106 (push x args))
108 (let ((tmp (gensymify x)))
109 (push tmp args)
110 (push tmp vars)
111 (push x vals)))))
112 (values vars vals old new
113 `(funcall #'(cas ,name) ,old ,new ,@args)
114 `(,name ,@args))))))))))
116 (defun expand-structure-slot-cas (info name place)
117 (let* ((dd (car info))
118 (structure (dd-name dd))
119 (slotd (cdr info))
120 (index (dsd-index slotd))
121 (type (dsd-type slotd)))
122 (unless (eq t (dsd-raw-type slotd))
123 (error "Cannot use COMPARE-AND-SWAP with structure accessor ~
124 for a typed slot: ~S"
125 place))
126 (when (dsd-read-only slotd)
127 (error "Cannot use COMPARE-AND-SWAP with structure accessor ~
128 for a read-only slot: ~S"
129 place))
130 (destructuring-bind (op arg) place
131 (aver (eq op name))
132 (with-unique-names (instance old new)
133 (values (list instance)
134 (list arg)
137 `(truly-the (values ,type &optional)
138 (%compare-and-swap-instance-ref
139 (the ,structure ,instance)
140 ,index
141 (the ,type ,old)
142 (the ,type ,new)))
143 `(,op ,instance))))))
145 (def!macro define-cas-expander (accessor lambda-list &body body)
146 #!+sb-doc
147 "Analogous to DEFINE-SETF-EXPANDER. Defines a CAS-expansion for ACCESSOR.
148 BODY must return six values as specified in GET-CAS-EXPANSION.
150 Note that the system provides no automatic atomicity for CAS expansion, nor
151 can it verify that they are atomic: it is up to the implementor of a CAS
152 expansion to ensure its atomicity.
154 EXPERIMENTAL: Interface subject to change."
155 `(eval-when (:compile-toplevel :load-toplevel :execute)
156 (setf (info :cas :expander ',accessor)
157 ,(make-macro-lambda `(cas-expand ,accessor) lambda-list body
158 'define-cas-expander accessor))))
160 ;; FIXME: this interface is bogus - short-form DEFSETF/CAS does not
161 ;; want a lambda-list. You just blindly substitute
162 ;; (CAS (PLACE arg1 ... argN) old new) -> (F arg1 ... argN old new).
163 ;; What role can this lambda-list have when there is no user-provided
164 ;; code to read the variables?
165 ;; And as mentioned no sbcl-devel, &REST is beyond bogus, it's broken.
167 (def!macro defcas (accessor lambda-list function &optional docstring)
168 #!+sb-doc
169 "Analogous to short-form DEFSETF. Defines FUNCTION as responsible
170 for compare-and-swap on places accessed using ACCESSOR. LAMBDA-LIST
171 must correspond to the lambda-list of the accessor.
173 Note that the system provides no automatic atomicity for CAS expansions
174 resulting from DEFCAS, nor can it verify that they are atomic: it is up to the
175 user of DEFCAS to ensure that the function specified is atomic.
177 EXPERIMENTAL: Interface subject to change."
178 (multiple-value-bind (llks reqs opts rest)
179 (parse-lambda-list lambda-list
180 :accept (lambda-list-keyword-mask '(&optional &rest))
181 :context "a DEFCAS lambda-list")
182 (declare (ignore llks))
183 `(define-cas-expander ,accessor ,lambda-list
184 ,@(when docstring (list docstring))
185 ;; FIXME: if a &REST arg is present, this is really weird.
186 (let ((temps (mapcar #'gensymify ',(append reqs opts rest)))
187 (args (list ,@(append reqs opts rest)))
188 (old (gensym "OLD"))
189 (new (gensym "NEW")))
190 (values temps
191 args
194 `(,',function ,@temps ,old ,new)
195 `(,',accessor ,@temps))))))
197 (def!macro compare-and-swap (place old new)
198 #!+sb-doc
199 "Atomically stores NEW in PLACE if OLD matches the current value of PLACE.
200 Two values are considered to match if they are EQ. Returns the previous value
201 of PLACE: if the returned value is EQ to OLD, the swap was carried out.
203 PLACE must be an CAS-able place. Built-in CAS-able places are accessor forms
204 whose CAR is one of the following:
206 CAR, CDR, FIRST, REST, SVREF, SYMBOL-PLIST, SYMBOL-VALUE, SVREF, SLOT-VALUE
207 SB-MOP:STANDARD-INSTANCE-ACCESS, SB-MOP:FUNCALLABLE-STANDARD-INSTANCE-ACCESS,
209 or the name of a DEFSTRUCT created accessor for a slot whose declared type is
210 either FIXNUM or T. Results are unspecified if the slot has a declared type
211 other then FIXNUM or T.
213 In case of SLOT-VALUE, if the slot is unbound, SLOT-UNBOUND is called unless
214 OLD is EQ to SB-PCL:+SLOT-UNBOUND+ in which case SB-PCL:+SLOT-UNBOUND+ is
215 returned and NEW is assigned to the slot. Additionally, the results are
216 unspecified if there is an applicable method on either
217 SB-MOP:SLOT-VALUE-USING-CLASS, (SETF SB-MOP:SLOT-VALUE-USING-CLASS), or
218 SB-MOP:SLOT-BOUNDP-USING-CLASS.
220 Additionally, the PLACE can be a anything for which a CAS-expansion has been
221 specified using DEFCAS, DEFINE-CAS-EXPANDER, or for which a CAS-function has
222 been defined. (See SB-EXT:CAS for more information.)
224 `(cas ,place ,old ,new))
226 ;;; Out-of-line definitions for various primitive cas functions.
227 (macrolet ((def (name lambda-list ref &optional set)
228 #!+compare-and-swap-vops
229 (declare (ignore ref set))
230 `(defun ,name (,@lambda-list old new)
231 #!+compare-and-swap-vops
232 (,name ,@lambda-list old new)
233 #!-compare-and-swap-vops
234 (progn
235 #!+sb-thread
236 ,(error "No COMPARE-AND-SWAP-VOPS on a threaded build?")
237 #!-sb-thread
238 (let ((current (,ref ,@lambda-list)))
239 ;; Shouldn't this be inside a WITHOUT-INTERRUPTS ?
240 (when (eq current old)
241 ,(if set
242 `(,set ,@lambda-list new)
243 `(setf (,ref ,@lambda-list) new)))
244 current)))))
245 (def %compare-and-swap-car (cons) car)
246 (def %compare-and-swap-cdr (cons) cdr)
247 (def %compare-and-swap-instance-ref (instance index) %instance-ref %instance-set)
248 (def %compare-and-swap-symbol-info (symbol) symbol-info)
249 (def %compare-and-swap-symbol-value (symbol) symbol-value)
250 (def %compare-and-swap-svref (vector index) svref))
252 ;; Atomic increment/decrement ops on tagged storage cells (as contrasted with
253 ;; specialized arrays and raw structure slots) are defined in terms of CAS.
255 ;; This code would be more concise if workable versions
256 ;; of +-MODFX, --MODFX were defined generically.
257 #-sb-xc-host
258 (macrolet ((modular (fun a b)
259 #!+(or x86 x86-64)
260 `(,(let ((*package* (find-package "SB!VM")))
261 (symbolicate fun "-MODFX"))
262 ,a ,b)
263 #!-(or x86 x86-64)
264 ;; algorithm of https://graphics.stanford.edu/~seander/bithacks
265 `(let ((res (logand (,fun ,a ,b)
266 (ash sb!ext:most-positive-word
267 (- sb!vm:n-fixnum-tag-bits))))
268 (m (ash 1 (1- sb!vm:n-fixnum-bits))))
269 (- (logxor res m) m))))
271 ;; Atomically frob the CAR or CDR of a cons, or a symbol-value.
272 ;; The latter will be a global value because the ATOMIC-INCF/DECF
273 ;; macros work on a symbol only if it is known global.
274 (macrolet ((def-frob (name op type slot)
275 `(defun ,name (place delta)
276 (declare (type ,type place) (type fixnum delta))
277 (loop (let ((old (the fixnum (,slot place))))
278 (when (eq (cas (,slot place) old
279 (modular ,op old delta)) old)
280 (return old)))))))
281 (def-frob %atomic-inc-symbol-global-value + symbol symbol-value)
282 (def-frob %atomic-dec-symbol-global-value - symbol symbol-value)
283 (def-frob %atomic-inc-car + cons car)
284 (def-frob %atomic-dec-car - cons car)
285 (def-frob %atomic-inc-cdr + cons cdr)
286 (def-frob %atomic-dec-cdr - cons cdr)))