Skip to content

Commit f9c0841

Browse files
committed
rewrite thread* in terms of returning-chan, bound-fn
1 parent 547e2e6 commit f9c0841

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -462,43 +462,41 @@ to catch and handle."
462462
[& body]
463463
(#'clojure.core.async.impl.go/go-impl &env body))
464464

465-
(defn thread-call-impl
466-
[f workload]
467-
(let [c (chan 1)]
468-
(let [binds (Var/getThreadBindingFrame)]
469-
(dispatch/exec
470-
(fn []
471-
(Var/resetThreadBindingFrame binds)
472-
(try
473-
(let [ret (f)]
474-
(when-not (nil? ret)
475-
(>!! c ret)))
476-
(finally
477-
(close! c))))
478-
workload))
479-
c))
465+
(defn returning-chan [f c]
466+
(fn []
467+
(try
468+
(let [ret (f)]
469+
(when-not (nil? ret)
470+
(>!! c ret)))
471+
(finally (close! c)))))
480472

481473
(defn thread-call
482474
"Executes f in another thread, returning immediately to the calling
483475
thread. Returns a channel which will receive the result of calling
484476
f when completed, then close."
485477
[f]
486-
(thread-call-impl f :mixed))
478+
(let [c (chan 1)]
479+
(-> f bound-fn* (returning-chan c) (dispatch/exec :mixed))
480+
c))
487481

488482
(defmacro thread
489483
"Executes the body in another thread, returning immediately to the
490484
calling thread. Returns a channel which will receive the result of
491485
the body when completed, then close."
492486
[& body]
493-
`(thread-call-impl (^:once fn* [] ~@body) :mixed))
487+
`(let [c# (chan 1)]
488+
(-> (^:once fn* [] ~@body) bound-fn* (returning-chan c#) (dispatch/exec :mixed))
489+
c#))
494490

495491
(defmacro io-thread
496492
"Executes the body in a thread intended for blocking I/O workloads,
497493
returning immediately to the calling thread. The body must not do
498494
extended computation (if so, use 'thread' instead). Returns a channel
499495
which will receive the result of the body when completed, then close."
500496
[& body]
501-
`(thread-call-impl (^:once fn* [] ~@body) :io))
497+
`(let [c# (chan 1)]
498+
(-> (^:once fn* [] ~@body) bound-fn* (returning-chan c#) (dispatch/exec :io))
499+
c#))
502500

503501
;;;;;;;;;;;;;;;;;;;; ops ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
504502

0 commit comments

Comments
 (0)