Skip to content

Instantly share code, notes, and snippets.

@ztellman
Created April 4, 2014 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ztellman/9979592 to your computer and use it in GitHub Desktop.
Save ztellman/9979592 to your computer and use it in GitHub Desktop.
(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