-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* some simplification/speed improvements * This aint workin' yet * all tests pass with cooperative reference cleanup * editing comments * simplification after reading more of concurrent-deque api * This aint workin' yet * all tests pass with cooperative reference cleanup * editing comments * simplification after reading more of concurrent-deque api * updated tech.resource
- Loading branch information
Showing
6 changed files
with
150 additions
and
86 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
(ns libpython-clj.python.gc | ||
"Binding of various sort of gc semantics optimized specifically for | ||
libpython-clj. For general bindings, see tech.resource" | ||
(:import [java.util.concurrent ConcurrentHashMap ConcurrentLinkedDeque] | ||
[java.lang.ref ReferenceQueue] | ||
[tech.resource GCReference])) | ||
|
||
|
||
(set! *warn-on-reflection* true) | ||
|
||
|
||
(defonce ^:dynamic *stack-gc-context* nil) | ||
(defn stack-context | ||
^ConcurrentLinkedDeque [] | ||
*stack-gc-context*) | ||
|
||
|
||
(defonce reference-queue-var (ReferenceQueue.)) | ||
(defn reference-queue | ||
^ReferenceQueue [] | ||
reference-queue-var) | ||
|
||
|
||
(defonce ptr-set-var (ConcurrentHashMap/newKeySet)) | ||
(defn ptr-set | ||
^java.util.Set [] | ||
ptr-set-var) | ||
|
||
|
||
(defn track | ||
[item dispose-fn] | ||
(let [ptr-val (GCReference. item (reference-queue) (fn [ptr-val] | ||
(.remove (ptr-set) ptr-val) | ||
(dispose-fn))) | ||
^ConcurrentLinkedDeque stack-context (stack-context)] | ||
;;We have to keep track of the pointer. If we do not the pointer gets gc'd then | ||
;;it will not be put on the reference queue when the object itself is gc'd. | ||
;;Nice little gotcha there. | ||
(if stack-context | ||
(.add stack-context ptr-val) | ||
;;Ensure we don't lose track of the weak reference. If it gets cleaned up | ||
;;the gc system will fail. | ||
(.add (ptr-set) ptr-val)) | ||
item)) | ||
|
||
|
||
(defn clear-reference-queue | ||
[] | ||
(when-let [next-ref (.poll (reference-queue))] | ||
(.run ^Runnable next-ref) | ||
(recur))) | ||
|
||
|
||
(defn clear-stack-context | ||
[] | ||
(when-let [next-ref (.pollLast (stack-context))] | ||
(.run ^Runnable next-ref) | ||
(recur))) | ||
|
||
|
||
(defmacro with-stack-context | ||
[& body] | ||
`(with-bindings {#'*stack-gc-context* (ConcurrentLinkedDeque.)} | ||
(try | ||
~@body | ||
(finally | ||
(clear-stack-context))))) |
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
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
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