Skip to content

Commit babf4ed

Browse files
committed
incorporated feedback from AM
1 parent 5e0a9dc commit babf4ed

File tree

3 files changed

+32
-38
lines changed

3 files changed

+32
-38
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ IOC and vthread code.
8585

8686
(alias 'core 'clojure.core)
8787

88-
(def go-becomes-ioc? (not (or (dispatch/vthreads-available-and-allowed?)
89-
(dispatch/target-vthreads?))))
88+
(def ^:private go-becomes-ioc?
89+
(not (or (dispatch/vthreads-available-and-allowed?)
90+
(dispatch/target-vthreads?))))
9091

9192
(set! *warn-on-reflection* false)
9293

@@ -515,6 +516,25 @@ IOC and vthread code.
515516
(let [ret (impl/take! port (fn-handler nop false))]
516517
(when ret @ret)))
517518

519+
(defn- dynamic-require [& args]
520+
(let [p (promise)
521+
n *ns*
522+
ll @#'clojure.core/*loaded-libs*]
523+
(.start
524+
(Thread.
525+
(fn []
526+
(try
527+
(let [result (binding [*ns* n
528+
clojure.core/*loaded-libs* ll]
529+
(apply require args))]
530+
(deliver p result))
531+
(catch Throwable t
532+
(deliver p t))))))
533+
(let [res @p]
534+
(if res
535+
(throw res)
536+
res))))
537+
518538
(defmacro go
519539
"Asynchronously executes the body, returning immediately to the
520540
calling thread. Additionally, any visible calls to <!, >! and alt!/alts!
@@ -531,11 +551,11 @@ IOC and vthread code.
531551
Returns a channel which will receive the result of the body when
532552
completed"
533553
[& body]
534-
(let [rt-check-step (when clojure.core/*compile-files*
535-
`(dispatch/ensure-runtime-vthreads!))]
536-
(if go-becomes-ioc?
537-
(do (clojure.core.async.impl.dispatch/dynamic-require 'clojure.core.async.impl.go)
538-
((find-var 'clojure.core.async.impl.go/go-impl) &env body))
554+
(if go-becomes-ioc?
555+
(do (dynamic-require 'clojure.core.async.impl.go)
556+
((find-var 'clojure.core.async.impl.go/go-impl) &env body))
557+
(let [rt-check-step (when clojure.core/*compile-files*
558+
`(dispatch/ensure-runtime-vthreads!))]
539559
`(do ~rt-check-step
540560
(thread-call (^:once fn* [] ~@body) :io)))))
541561

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@
7272
[workload]
7373
(Executors/newCachedThreadPool (counted-thread-factory (str "async-" (name workload) "-%d") true)))
7474

75-
(def virtual-threads-available?
75+
(def ^:private virtual-threads-available?
7676
(delay
7777
(try
7878
(Class/forName "java.lang.Thread$Builder$OfVirtual")
7979
true
8080
(catch ClassNotFoundException _
8181
false))))
8282

83-
(defn vthreads-directive
83+
(defn- vthreads-directive
8484
"Retrieves the value of the sysprop clojure.core.async.vthreads."
8585
[]
8686
(System/getProperty "clojure.core.async.vthreads"))
@@ -89,32 +89,14 @@
8989
(= (vthreads-directive) "target"))
9090

9191
(defn vthreads-available-and-allowed? []
92-
(and (not= (vthreads-directive) "avoid")
93-
@virtual-threads-available?))
92+
(and @virtual-threads-available?
93+
(not= (vthreads-directive) "avoid")))
9494

9595
(defn ensure-runtime-vthreads! []
9696
(when (not (vthreads-available-and-allowed?))
9797
(throw (ex-info "Code compiled to target virtual threads, but is running on a JVM without vthread support."
9898
{:runtime-jvm-version (System/getProperty "java.version")}))))
9999

100-
(defn dynamic-require [& args]
101-
(let [p (promise)
102-
n *ns*
103-
ll @#'clojure.core/*loaded-libs*]
104-
(.start
105-
(Thread.
106-
(fn []
107-
(deliver p
108-
(binding [*ns* n
109-
clojure.core/*loaded-libs* ll]
110-
(try
111-
(apply require args)
112-
(catch Throwable t t)))))))
113-
(let [res @p]
114-
(if res
115-
(throw res)
116-
res))))
117-
118100
(defn- make-io-executor
119101
[]
120102
(if (vthreads-available-and-allowed?)

src/test/clojure/clojure/core/async_test.clj

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,7 @@
193193
(io-thread (>!! c2 (clojure.string/upper-case (<!! c1))))
194194
(io-thread (>!! c3 (clojure.string/reverse (<!! c2))))
195195
(>!! c1 "loop")
196-
(is (= "POOL" (<!! c3)))))
197-
#_(testing "io-thread parking op should fail"
198-
(let [c1 (chan)]
199-
(io-thread
200-
(try
201-
(>! c1 :no)
202-
(catch AssertionError _
203-
(>!! c1 :yes))))
204-
(is (= :yes (<!! c1))))))
196+
(is (= "POOL" (<!! c3))))))
205197

206198
(deftest ops-tests
207199
(testing "map<"

0 commit comments

Comments
 (0)