Skip to content

Commit 23bb861

Browse files
committed
method handle for isVirtual invoke.
1 parent f53e387 commit 23bb861

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
(ns ^{:skip-wiki true}
1010
clojure.core.async.impl.dispatch
11-
(:import [java.util.concurrent Executors ExecutorService ThreadFactory]))
11+
(:import [java.util.concurrent Executors ExecutorService ThreadFactory]
12+
[java.lang.invoke MethodHandles MethodHandle MethodType]))
1213

1314
(set! *warn-on-reflection* true)
1415

@@ -77,8 +78,8 @@
7778
(when (or (< maj major)
7879
(< min minor)
7980
(< incr incremental))
80-
(throw (ex-info (str "Clojure version greater than " maj "." min "." incr
81-
" required to run this version of core.async")
81+
(throw (ex-info (str "Clojure version greater than or equal to " maj "." min "." incr
82+
" required to the the go block analyzer.")
8283
{:clojure-version *clojure-version*})))))
8384

8485
(def virtual-threads-available?
@@ -103,14 +104,18 @@
103104
(and virtual-threads-available?
104105
(not= (vthreads-directive) "avoid")))
105106

106-
(def ^:private virtual?
107+
(def ^:private virtual-thread?
107108
(if virtual-threads-available?
108-
(eval `(fn [^Thread t#] (~'.isVirtual t#)))
109+
(let [lookup (MethodHandles/lookup)
110+
t (MethodType/methodType Boolean/TYPE)
111+
^MethodHandle mh (.findVirtual lookup Thread "isVirtual" t)]
112+
(fn [^Thread thread]
113+
(.invokeWithArguments mh [thread])))
109114
(constantly false)))
110115

111116
(defn in-vthread? []
112117
(and virtual-threads-available?
113-
(virtual? (Thread/currentThread))))
118+
(virtual-thread? (Thread/currentThread))))
114119

115120
(defn report-vthreads-not-available-error! []
116121
(throw (ex-info "Code compiled to target virtual threads, but is running without vthread support."

0 commit comments

Comments
 (0)