Skip to content

Instantly share code, notes, and snippets.

@timcharper
Created June 11, 2010 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timcharper/435165 to your computer and use it in GitHub Desktop.
Save timcharper/435165 to your computer and use it in GitHub Desktop.
(defmacro with-monitor
"Yields an object and a fn to tell if the object has been GC'd yet
The infinite loop (alive-fn) will quit when the return value (monitor) is no longer referenced and GC is run
(with-monitor [monitor alive?]
(let [alive-fn (fn [] (loop []
(println \"alive...\")
(java.lang.Thread/sleep 1000)
(when (alive?) (recur))))]
(future (alive-fn))
monitor))"
[[tracer-binding alive?-fn-binding] & body]
`(let [~tracer-binding (Object.)
weak-ref# (java.lang.ref.WeakReference. ~tracer-binding)
~alive?-fn-binding (fn [] (not (nil? (.get weak-ref#))))]
~@body))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment