awwx.ws

lock1

individual atomic locks

http://awwx.ws/lock1.arc:

(def lock ()
  (obj sema (scheme.make-semaphore 1)
       nest (scheme.make-thread-cell nil)))

(def atomic-lock-invoke (lock thunk)
  (if (scheme.thread-cell-ref lock!nest)
       (thunk)
       (do ((scheme thread-cell-set!) lock!nest t)
           (after (scheme.call-with-semaphore lock!sema thunk)
                  ((scheme thread-cell-set!) lock!nest nil)))))

(mac atomic-lock (lock . body)
  `(atomic-lock-invoke ,lock (fn () ,@body)))

This hack provides an extension to Arc’s atomic by providing individual locks. Only one thread at a time will execute inside of an atomic-lock with a given lock, but threads calling atomic-lock with different locks will not affect each other.

Individual locks are useful when you’re performing I/O inside of the locked code, and you don’t want other unrelated threads to hang waiting for your I/O to complete.

Prerequisites

This hack depends on arc3.1 and scheme0.

License

Same as Arc.

Contact me

Twitter: awwx
Email: andrew.wilcox [at] gmail.com