Source

clojurian / clojurian-atom.scm

Full commit
(module clojurian-atom

(atom atom-value atom? compare-and-set! swap! reset!)

(import chicken scheme)
(use srfi-18)

(define-record atom
  mutex value)

(define (atom value)
  (make-atom (make-mutex) value))

(define (compare-and-set! atom old new)
  (and (eq? (atom-value atom) old)
       (dynamic-wind
           (lambda ()
             (mutex-lock! (atom-mutex atom)))
           (lambda ()
             (and (eq? (atom-value atom) old)
                  (begin
                    (atom-value-set! atom new)
                    #t)))
           (lambda ()
             (mutex-unlock! (atom-mutex atom))))))

(define (swap! atom proc . args)
  (let loop ()
    (let* ((old (atom-value atom))
           (new (apply proc old args)))
      (if (compare-and-set! atom old new)
          new
          (loop)))))

(define (reset! atom value)
  (dynamic-wind
      (lambda ()
        (mutex-lock! (atom-mutex atom)))
      (lambda ()
        (atom-value-set! atom value)
        value)
      (lambda ()
        (mutex-unlock! (atom-mutex atom)))))

)