Skip to content

Commit f53e387

Browse files
committed
wip
1 parent fe4573a commit f53e387

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ IOC and vthread code.
205205

206206
(defparkingop <!
207207
"takes a val from port. Must be called inside a (go ...) block, or on
208-
a virtual thread. Will return nil if closed. Will park if nothing is
209-
available."
208+
a virtual thread (no matter how it was started). Will return nil if
209+
closed. Will park if nothing is available."
210210
[port])
211211

212212
(defn take!
@@ -244,8 +244,8 @@ IOC and vthread code.
244244

245245
(defparkingop >!
246246
"puts a val into port. nil values are not allowed. Must be called
247-
inside a (go ...) block, or on a virtual thread. Will park if no buffer
248-
space is available.
247+
inside a (go ...) block, or on a virtual thread (no matter how it
248+
was started). Will park if no buffer space is available.
249249
Returns true unless port is already closed."
250250
[port val])
251251

@@ -387,14 +387,14 @@ IOC and vthread code.
387387

388388
(defparkingop alts!
389389
"Completes at most one of several channel operations. Must be called
390-
inside a (go ...) block, or on a virtual thread. ports is a vector of
391-
channel endpoints, which can be either a channel to take from or a vector
392-
of [channel-to-put-to val-to-put], in any combination. Takes will be
393-
made as if by <!, and puts will be made as if by >!. Unless
394-
the :priority option is true, if more than one port operation is
395-
ready a non-deterministic choice will be made. If no operation is
396-
ready and a :default value is supplied, [default-val :default] will
397-
be returned, otherwise alts! will park until the first operation to
390+
inside a (go ...) block, or on a virtual thread (no matter how it was
391+
started). ports is a vector of channel endpoints, which can be either
392+
a channel to take from or a vector of [channel-to-put-to val-to-put],
393+
in any combination. Takes will be made as if by <!, and puts will be
394+
made as if by >!. Unless the :priority option is true, if more than one
395+
port operation is ready a non-deterministic choice will be made. If no
396+
operation is ready and a :default value is supplied, [default-val :default]
397+
will be returned, otherwise alts! will park until the first operation to
398398
become ready completes. Returns [val port] of the completed
399399
operation, where val is the value taken for takes, and a
400400
boolean (true unless already closed, as per put!) for puts.
@@ -512,20 +512,25 @@ IOC and vthread code.
512512
(when ret @ret)))
513513

514514
(defn- dynamic-require [nsym]
515-
(dispatch/ensure-clojure-version! 1 11 4)
515+
(dispatch/ensure-clojure-version! 1 12 3)
516516
(require nsym))
517517

518518
(defn- go* [body env]
519-
(cond (not dispatch/target-vthreads?)
520-
(do (dynamic-require 'clojure.core.async.impl.go)
521-
((find-var 'clojure.core.async.impl.go/go-impl) env body))
522-
523-
(or dispatch/vthreads-available-and-allowed? clojure.core/*compile-files*)
519+
(cond (and (not dispatch/virtual-threads-available?)
520+
dispatch/target-vthreads?
521+
(not clojure.core/*compile-files*))
522+
(dispatch/report-vthreads-not-available-error!)
523+
524+
(or dispatch/target-vthreads?
525+
(and dispatch/unset-vthreads?
526+
dispatch/virtual-threads-available?
527+
(not clojure.core/*compile-files*)))
524528
`(do (dispatch/ensure-runtime-vthreads!)
525529
(thread-call (^:once fn* [] ~@body) :io))
526530

527531
:else
528-
(dispatch/report-vthreads-not-available-error!)))
532+
(do (dynamic-require 'clojure.core.async.impl.go)
533+
((find-var 'clojure.core.async.impl.go/go-impl) env body))))
529534

530535
(defmacro go
531536
"Asynchronously executes the body, returning immediately to the

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
" required to run this version of core.async")
8282
{:clojure-version *clojure-version*})))))
8383

84-
(def ^:private virtual-threads-available?
84+
(def virtual-threads-available?
8585
(try
8686
(Class/forName "java.lang.Thread$Builder$OfVirtual")
8787
true
@@ -103,8 +103,14 @@
103103
(and virtual-threads-available?
104104
(not= (vthreads-directive) "avoid")))
105105

106+
(def ^:private virtual?
107+
(if virtual-threads-available?
108+
(eval `(fn [^Thread t#] (~'.isVirtual t#)))
109+
(constantly false)))
110+
106111
(defn in-vthread? []
107-
(= "VirtualThread" (.getSimpleName (class (Thread/currentThread)))))
112+
(and virtual-threads-available?
113+
(virtual? (Thread/currentThread))))
108114

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

0 commit comments

Comments
 (0)