Created
April 4, 2014 17:50
-
-
Save ztellman/9979592 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
(def ^ThreadLocal stack-depth (ThreadLocal.)) | |
(def ^Executor overflow-protection-pool | |
(let [cnt (atom 0)] | |
(Executors/newCachedThreadPool | |
(thread-factory #(str "manifold-overflow-protection-pool-" (swap! cnt inc)))))) | |
(def ^:const max-depth 50) | |
(defmacro without-overflow [& body] | |
`(let [depth# (.get stack-depth) | |
depth'# (if (nil? depth#) 0 depth#) | |
f# (fn [] ~@body)] | |
(if (> depth'# max-depth) | |
(do | |
(.execute overflow-protection-pool ^Runnable f#) | |
nil) | |
(try | |
(.set stack-depth (unchecked-inc (unchecked-long depth'#))) | |
(f#) | |
(finally | |
(when (nil? depth#) | |
(.set stack-depth nil))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment