Skip to content

Commit 201df12

Browse files
committed
checking if parking op called in vthread
1 parent dd86e6e commit 201df12

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,20 @@ IOC and vthread code.
164164
[^long msecs]
165165
(timers/timeout msecs))
166166

167-
(defn- defparkingop* [op doc arglist body]
167+
(defn- defparkingop* [op doc arglist]
168168
(let [as (mapv #(list 'quote %) arglist)
169169
blockingop (-> op name (str "!") symbol)]
170170
`(def ~(with-meta op {:arglists `(list ~as) :doc doc})
171-
(if dispatch/vthreads-available-and-allowed?
172-
(fn [~'& ~'args] ~(list* apply blockingop '[args]))
173-
(fn ~arglist ~@body)))))
171+
(fn [~'& ~'args]
172+
(if (= "VirtualThread" (.getSimpleName (class (Thread/currentThread))))
173+
~(list* apply blockingop '[args])
174+
(assert nil ~(str op " used not in (go ...) block")))))))
174175

175176
(defmacro defparkingop
176-
"Emits a Var definition that initializes as a parking op or as a
177-
blocking op when vthreads available and allowed."
178-
[op doc arglist & body]
179-
(defparkingop* op doc arglist body))
177+
"Emits a Var with a function that checks if it's running in a virtual thread. If so then
178+
the related blocking op will be called, otherwise the function throws."
179+
[op doc arglist]
180+
(defparkingop* op doc arglist))
180181

181182
(defmacro defblockingop
182183
[op doc arglist & body]
@@ -206,8 +207,7 @@ IOC and vthread code.
206207
"takes a val from port. Must be called inside a (go ...) block, or on
207208
a virtual thread. Will return nil if closed. Will park if nothing is
208209
available."
209-
[port]
210-
(assert nil "<! used not in (go ...) block"))
210+
[port])
211211

212212
(defn take!
213213
"Asynchronously takes a val from port, passing to fn1. Will pass nil
@@ -247,8 +247,7 @@ IOC and vthread code.
247247
inside a (go ...) block, or on a virtual thread. Will park if no buffer
248248
space is available.
249249
Returns true unless port is already closed."
250-
[port val]
251-
(assert nil ">! used not in (go ...) block"))
250+
[port val])
252251

253252
(defn- nop [_])
254253
(def ^:private fhnop (fn-handler nop))
@@ -409,8 +408,7 @@ IOC and vthread code.
409408
used, nor in what order should they be, so they should not be
410409
depended upon for side effects."
411410

412-
[ports & {:as opts}]
413-
(assert nil "alts! used not in (go ...) block"))
411+
[ports & {:as opts}])
414412

415413
(defn do-alt [alts clauses]
416414
(assert (even? (count clauses)) "unbalanced clauses")

0 commit comments

Comments
 (0)