Skip to content

Commit e2dcf01

Browse files
committed
io-thread impl for pre-vthread release
1 parent 3dd3141 commit e2dcf01

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -465,28 +465,24 @@ to catch and handle."
465465
(defonce ^:private ^Executor thread-macro-executor
466466
(Executors/newCachedThreadPool (conc/counted-thread-factory "async-thread-macro-%d" true)))
467467

468-
(def ^ExecutorService io-thread-exec
469-
(if (= "21" (System/getProperty "java.vm.specification.version"))
470-
(eval '(Executors/newThreadPerTaskExecutor (-> (Thread/ofVirtual)
471-
(java.lang.Thread$Builder/.name "io-thread-" 0)
472-
.factory)))
473-
thread-macro-executor))
468+
(defonce ^:private ^ExecutorService io-thread-exec thread-macro-executor)
474469

475470
(defmacro io-thread
476-
"Asynchronously executes the body in a virtual thread, returning immediately
477-
to the calling thread.
471+
"Asynchronously executes the body in a thread compatible with I/O workload,
472+
returning immediately to the calling thread. Only blocking operations should
473+
be used in io-thread bodies.
478474
479475
io-thread blocks should not (either directly or indirectly) perform operations
480-
that may block indefinitely. Doing so risks pinning the virtual thread
481-
to its carrier thread.
476+
that never block and run pure compute operations. Parking ops
477+
(i.e. <!, >! and alt!/alts!) used in io-thread bodies will throw at
478+
runtime.
482479
483480
Returns a channel which will receive the result of the body when
484481
completed"
485482
[& body]
486483
`(let [c# (chan 1)
487484
captured-bindings# (Var/getThreadBindingFrame)]
488-
(.execute
489-
io-thread-exec
485+
(.execute ^ExecutorService @#'io-thread-exec
490486
(^:once fn* []
491487
(Var/resetThreadBindingFrame captured-bindings#)
492488
(try

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@
185185
(binding [test-dyn true]
186186
(is (<!! (thread test-dyn))))))
187187

188+
(deftest io-thread-tests
189+
(testing "io-thread"
190+
(let [c1 (chan)
191+
c2 (chan)
192+
c3 (chan)]
193+
(io-thread (>!! c2 (clojure.string/upper-case (<!! c1))))
194+
(io-thread (>!! c3 (clojure.string/reverse (<!! c2))))
195+
(>!! c1 "loop")
196+
(is (= "POOL" (<!! c3))))))
188197

189198
(deftest ops-tests
190199
(testing "map<"

0 commit comments

Comments
 (0)