Skip to content

Commit bb36b24

Browse files
committed
First pass of removing vthreads, except in io-thread. WiP
1 parent 77b9779 commit bb36b24

File tree

2 files changed

+3
-70
lines changed

2 files changed

+3
-70
lines changed

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

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,7 @@ core.async. If not supplied, the Executor for :io will be
4040
used instead.
4141
4242
The set of contexts may grow in the future so the function should
43-
return nil for unexpected contexts.
44-
45-
Use the Java system property `clojure.core.async.vthreads` to control
46-
how core.async uses JDK 21+ virtual threads. The property can be one of
47-
the following values:
48-
49-
unset - core.async will opportunistically use vthreads when available
50-
(≥ Java 21) and will otherwise use the old IOC impl. io-thread and :io
51-
thread pool will run on platform threads if vthreads are not available.
52-
If AOT compiling, go blocks will always use IOC so that the resulting
53-
bytecode works on all JVMs (so no change in compiled output)
54-
55-
\"target\" - means that you are targeting virtual threads. At runtime
56-
from source, go blocks will throw if vthreads are not available.
57-
If AOT compiling, go blocks are always compiled as normal Clojure
58-
code to be run on vthreads and will throw at runtime if vthreads are
59-
not available (Java <21)
60-
61-
\"avoid\" - means that vthreads will not be used by core.async - you can
62-
use this to minimize impacts if you are not yet ready to utilize vthreads
63-
in your app. If AOT compiling, go blocks will use IOC. At runtime, io-thread
64-
and the :io thread pool use platform threads
65-
66-
Note: existing IOC compiled go blocks from older core.async versions continue
67-
to work (we retain and load the IOC state machine runtime - this does not
68-
require the analyzer), and you can interact with the same channels from both
69-
IOC and vthread code.
70-
"
43+
return nil for unexpected contexts."
7144
(:refer-clojure :exclude [reduce transduce into merge map take partition
7245
partition-by bounded-count])
7346
(:require [clojure.core.async.impl.protocols :as impl]
@@ -516,22 +489,6 @@ IOC and vthread code.
516489
(let [ret (impl/take! port (fn-handler nop false))]
517490
(when ret @ret)))
518491

519-
(defn- go* [body env]
520-
(cond (and (not dispatch/virtual-threads-available?)
521-
dispatch/target-vthreads?
522-
(not clojure.core/*compile-files*))
523-
(dispatch/report-vthreads-not-available-error!)
524-
525-
(or dispatch/target-vthreads?
526-
(and dispatch/unset-vthreads?
527-
dispatch/virtual-threads-available?
528-
(not clojure.core/*compile-files*)))
529-
`(do (dispatch/ensure-runtime-vthreads!)
530-
(thread-call (^:once fn* [] ~@body) :io))
531-
532-
:else
533-
((requiring-resolve 'clojure.core.async.impl.go/go-impl) env body)))
534-
535492
(defmacro go
536493
"Asynchronously executes the body, returning immediately to the
537494
calling thread. Additionally, any visible calls to <!, >! and alt!/alts!
@@ -548,7 +505,7 @@ IOC and vthread code.
548505
Returns a channel which will receive the result of the body when
549506
completed"
550507
[& body]
551-
(go* body &env))
508+
((requiring-resolve 'clojure.core.async.impl.go/go-impl) &env body))
552509

553510
(defonce ^:private thread-macro-executor nil)
554511

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,6 @@
8484
(catch ClassNotFoundException _
8585
false)))
8686

87-
(defn- vthreads-directive
88-
"Retrieves the value of the sysprop clojure.core.async.vthreads."
89-
[]
90-
(System/getProperty "clojure.core.async.vthreads"))
91-
92-
(def target-vthreads?
93-
(= (vthreads-directive) "target"))
94-
95-
(def unset-vthreads?
96-
(nil? (vthreads-directive)))
97-
98-
(def vthreads-available-and-allowed?
99-
(and virtual-threads-available?
100-
(not= (vthreads-directive) "avoid")))
101-
10287
(def ^:private virtual-thread?
10388
(if virtual-threads-available?
10489
(eval `(fn [^Thread t#] (~'.isVirtual t#)))
@@ -108,18 +93,9 @@
10893
(and virtual-threads-available?
10994
(virtual-thread? (Thread/currentThread))))
11095

111-
(defn report-vthreads-not-available-error! []
112-
(throw (ex-info "Code compiled to target virtual threads, but is running without vthread support."
113-
{:runtime-jvm-version (System/getProperty "java.version")
114-
:vthreads-directive (vthreads-directive)})))
115-
116-
(defn ensure-runtime-vthreads! []
117-
(when (not vthreads-available-and-allowed?)
118-
(report-vthreads-not-available-error!)))
119-
12096
(defn- make-io-executor
12197
[]
122-
(if vthreads-available-and-allowed?
98+
(if virtual-threads-available?
12399
(let [svt (.getDeclaredMethod Thread "startVirtualThread" (into-array Class [Runnable]))]
124100
(reify Executor
125101
(execute [_ r]

0 commit comments

Comments
 (0)