Skip to content

Instantly share code, notes, and snippets.

@sunng87
Created February 24, 2012 06:25
Show Gist options
  • Save sunng87/1898383 to your computer and use it in GitHub Desktop.
Save sunng87/1898383 to your computer and use it in GitHub Desktop.
dosync+
(defmacro dosync+ [listeners & body]
`(let [on-start# (get ~listeners :on-start identity)
on-retry# (get ~listeners :on-retry identity)
on-committed# (get ~listeners :on-committed identity)
retires# (atom 0)
ctx# (atom {})]
(dosync
(if (zero? @retires#)
(swap! ctx# on-start#)
(swap! ctx# on-retry#))
(swap! retires# inc)
~@body)
(on-committed# @ctx#)))
(def stm-mon
{:on-start (fn [_]
(do
(println "started")
{:r 0 :t (System/currentTimeMillis)}))
:on-retry (fn [ctx]
(do
(println "retrying")
(update-in ctx [:r] inc)))
:on-committed (fn [ctx]
(do
(println "committed")
(println (str "total retry: " (:r ctx)))
(println (str "total time: "
(- (System/currentTimeMillis) (:t ctx))))))})
(def m (ref 0))
(def n (ref []))
(defn run! []
(dosync+ stm-mon
(alter m inc)
(alter n conj @m)))
(dorun (pmap (fn [_] (run!)) (range 1000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment