CL-STM

Examples 

-*- lisp -*-

(in-package :cl-stm)

Concurrent counter 

(deftclass counter ()
  ((count :accessor count-of
          :initarg :count
          :initform 0)))
(deftransaction increment ((c counter) &optional (d 1))
  (incf (count-of c) d))
(deftransaction decrement ((c counter) &optional (d 1))
  (decf (count-of c) d))
(deftransaction reset ((c counter))
  (setf (count-of c) 0))
(deftransaction swap ((c1 counter) (c2 counter))
  (psetf (count-of c1) (count-of c2)
         (count-of c2) (count-of c1)))

Mutable Variables 

(defparameter *empty-sym* (gensym "EMPTY"))
(deftclass mvar ()
  ((value :accessor value-of
          :initarg :value
          :initform *empty-sym*)))
(deftransaction empty? ((mvar mvar))
  (eq (value-of mvar) *empty-sym*))
(deftransaction empty! ((mvar mvar))
  (setf (value-of mvar) *empty-sym*))
(deftransaction take ((mvar mvar))
  (if (empty? mvar)
      (retry)
      (prog1 (value-of mvar)
        (empty! mvar))))
(deftransaction put ((mvar mvar) val)
  (if (empty? mvar)
      (setf (value-of mvar) val)
      (retry)))