Skip to content

Commit 5df8bd7

Browse files
committed
ASYNC-259 Implement go-checking with new executors
1 parent a5d129d commit 5df8bd7

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/main/clojure/clojure/core/async/impl/dispatch.clj

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
(set! *warn-on-reflection* true)
1414

15-
(defonce ^:private in-dispatch (ThreadLocal.))
16-
1715
(defonce executor nil)
1816

1917
(defn counted-thread-factory
@@ -37,15 +35,29 @@
3735
(.setName (format name-format (swap! counter inc)))
3836
(.setDaemon daemon))))))))
3937

38+
;; go blocking checking
39+
40+
(defonce in-go-dispatch (ThreadLocal.))
41+
42+
(defmacro with-dispatch-thread-marking
43+
[& body]
44+
(if (Boolean/getBoolean "clojure.core.async.go-checking")
45+
`(try
46+
(.set in-go-dispatch true)
47+
~@body
48+
(finally
49+
(.set in-go-dispatch false)))
50+
`(do ~@body)))
51+
4052
(defn in-dispatch-thread?
41-
"Returns true if the current thread is a go block dispatch pool thread"
53+
"Returns true if the current thread is used for go block dispatch"
4254
[]
43-
(boolean (.get ^ThreadLocal in-dispatch)))
55+
(boolean (.get ^ThreadLocal in-go-dispatch)))
4456

4557
(defn check-blocking-in-dispatch
46-
"If the current thread is a dispatch pool thread, throw an exception"
58+
"If the current thread is being used for go block dispatch, throw an exception"
4759
[]
48-
(when (.get ^ThreadLocal in-dispatch)
60+
(when (in-dispatch-thread?)
4961
(throw (IllegalStateException. "Invalid blocking call in dispatch thread"))))
5062

5163
(defn ex-handler

src/main/clojure/clojure/core/async/impl/go.clj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,11 +1048,12 @@
10481048
captured-bindings# (Var/getThreadBindingFrame)]
10491049
(dispatch/run
10501050
(^:once fn* []
1051-
(let [~@(mapcat (fn [[l sym]] [sym `(^:once fn* [] ~(vary-meta l dissoc :tag))]) crossing-env)
1052-
f# ~(state-machine
1053-
`(do ~@body) 1 [crossing-env env] rt/async-custom-terminators)
1054-
state# (-> (f#)
1055-
(rt/aset-all! rt/USER-START-IDX c#
1056-
rt/BINDINGS-IDX captured-bindings#))]
1057-
(rt/run-state-machine-wrapped state#))))
1058-
c#)))
1051+
(dispatch/with-dispatch-thread-marking
1052+
(let [~@(mapcat (fn [[l sym]] [sym `(^:once fn* [] ~(vary-meta l dissoc :tag))]) crossing-env)
1053+
f# ~(state-machine
1054+
`(do ~@body) 1 [crossing-env env] rt/async-custom-terminators)
1055+
state# (-> (f#)
1056+
(rt/aset-all! rt/USER-START-IDX c#
1057+
rt/BINDINGS-IDX captured-bindings#))]
1058+
(rt/run-state-machine-wrapped state#)))))
1059+
c#)))

0 commit comments

Comments
 (0)