Created
March 4, 2014 18:48
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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