For people who are interested, here is my own version of atom updating 
functions:

;; A wrapped up atom that can be used in those lock-* functions
(deftype LockAtom
  [atom]
  clojure.lang.IDeref
  (deref [this]
    @(.atom this)))

;; (lock-atom (+ 4 5)) => #<LockAtom@4e5497cb: 9>
(defmacro lock-atom
  "Like ATOM, but create a lockable atom."
  [expr]
  `(LockAtom. (atom ~expr)))

(defn lock-swap!
  "Like swap!, however, it will first lock the atom to ensure no other 
thread can change
  the atom at the same time, so no re-try will happen. This is useful for 
atom update
  functions that can have side effects, or high contension situations where 
re-tries are
  causing performance degradation. Works only with lock-atoms."
  [lock-atom & rest-args]
  (locking lock-atom
    (apply swap! (.atom lock-atom) rest-args)))

(defn lock-reset!
  "Like reset!, but works on lock-atoms."
  [lock-atom new-val]
  (locking lock-atom
    (reset! (.atom lock-atom) new-val)))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to