Created
June 11, 2010 22:59
-
-
Save timcharper/435165 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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