Skip to content

Commit

Permalink
dependant: improve conditional-store
Browse files Browse the repository at this point in the history
In case of real multiprocessing wrapping a body in without-interrupts
is useless -- another thread may modify the place concurrently without
invoking any interrupts. For that we use a lock by default.

Clasp, ECL and SBCL have CAS operators which we use.
  • Loading branch information
dkochmanski committed May 11, 2020
1 parent c6ef889 commit 7b133e8
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions dependent.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -805,25 +805,34 @@
;; This should use GET-SETF-METHOD to avoid evaluating subforms multiple times.
;; It doesn't because CLtL doesn't pass the environment to GET-SETF-METHOD.

;; FIXME: both sbcl and ecl has compare-and-swap these days.
;; FIXME: Verify for clasp
#-(or (and clasp threads) ecl sbcl)
(progn
(defvar *conditional-store-lock*
(make-process-lock "conditional store"))
(defmacro conditional-store (place old-value new-value)
`(holding-lock (*conditional-store-lock*)
(if (eq ,place ,old-value)
(prog1 t
(setf ,place ,new-value))
nil))))

#-sbcl
#+(and clasp threads)
(defmacro conditional-store (place old-value new-value)
`(without-interrupts
(cond ((eq ,place ,old-value)
(setf ,place ,new-value)
t))))
(let ((ov (gensym)))
`(let ((,ov ,old-value))
(eq ,ov (mp:cas ,place ,ov ,new-value)))))

#+ecl
(defmacro conditional-store (place old-value new-value)
(let ((ov (gensym)))
`(let ((,ov ,old-value))
(eq ,ov (mp:compare-and-swap ,place ,ov ,new-value)))))

#+sbcl
(progn
(defvar *conditional-store-lock*
(sb-thread:make-mutex :name "conditional store"))
(defmacro conditional-store (place old-value new-value)
`(sb-thread:with-mutex (*conditional-store-lock*)
(cond ((eq ,place ,old-value)
(setf ,place ,new-value)
t)))))
(defmacro conditional-store (place old-value new-value)
(let ((ov (gensym)))
`(let ((,ov ,old-value))
(eq ,ov (sb-ext:compare-and-swap ,place ,ov ,new-value)))))

;;;----------------------------------------------------------------------------
;;; IO Error Recovery
Expand Down

0 comments on commit 7b133e8

Please sign in to comment.