Skip to content

Instantly share code, notes, and snippets.

@jhrr
Created March 4, 2014 18:48
Show Gist options
  • Save jhrr/9353006 to your computer and use it in GitHub Desktop.
Save jhrr/9353006 to your computer and use it in GitHub Desktop.
SBCL exports a symbol sb-ext:compare-and-swap, which can be used to do small-scale transactional tricks, and is often an order of magnitude faster than the equivalent locking solution. Here is a very simple concurrency-safe stack. The macro is a convenient wrapper that helps use compare-and-swap in a 'transactional' way. http://marijnhaverbeke.n…
(defmacro concurrent-update (place var &body body)
`(flet ((action (,var) ,@body))
(let ((prev ,place))
(loop :until (eq (sb-ext:compare-and-swap ,place prev (action prev)) prev)
:do (setf prev ,place))
prev)))
(defun make-cstack (&rest elements)
(cons :stack elements))
(defun push-cstack (element stack)
(concurrent-update (cdr stack) elts
(cons element elts))
(values))
(defun pop-cstack (stack)
(car (concurrent-update (cdr stack) elts
(cdr elts))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment