@@ -34,7 +34,8 @@ to catch and handle."
34
34
(:import [java.util.concurrent.atomic AtomicLong]
35
35
[java.util.concurrent.locks Lock]
36
36
[java.util.concurrent Executors Executor ThreadLocalRandom]
37
- [java.util Arrays ArrayList]))
37
+ [java.util Arrays ArrayList]
38
+ [clojure.lang Var]))
38
39
39
40
(alias 'core 'clojure.core)
40
41
@@ -462,29 +463,43 @@ to catch and handle."
462
463
[& body]
463
464
(#'clojure.core.async.impl.go/go-impl &env body))
464
465
465
- (require '[clojure.core.async.impl.exec.services :as exec-services])
466
+ (defn thread-impl
467
+ [f workload]
468
+ (let [c (chan 1 )]
469
+ (let [binds (Var/getThreadBindingFrame )]
470
+ (dispatch/executor-service-call
471
+ (fn []
472
+ (Var/resetThreadBindingFrame binds)
473
+ (try
474
+ (let [ret (f )]
475
+ (when-not (nil? ret)
476
+ (>!! c ret)))
477
+ (finally
478
+ (close! c))))
479
+ workload))
480
+ c))
466
481
467
482
(defn thread-call
468
483
" Executes f in another thread, returning immediately to the calling
469
484
thread. Returns a channel which will receive the result of calling
470
485
f when completed, then close."
471
486
[f]
472
- (exec-services/ thread-call f :mixed ))
487
+ (thread-impl f :mixed ))
473
488
474
489
(defmacro thread
475
490
" Executes the body in another thread, returning immediately to the
476
491
calling thread. Returns a channel which will receive the result of
477
492
the body when completed, then close."
478
493
[& body]
479
- `(thread-call (^:once fn* [] ~@body)))
494
+ `(thread-impl (^:once fn* [] ~@body) :mixed ))
480
495
481
496
(defmacro io-thread
482
497
" Executes the body in a thread intended for blocking I/O workloads,
483
498
returning immediately to the calling thread. The body must not do
484
499
extended computation (if so, use 'thread' instead). Returns a channel
485
500
which will receive the result of the body when completed, then close."
486
501
[& body]
487
- `(exec-services/ thread-call (^:once fn* [] ~@body) :io ))
502
+ `(thread-impl (^:once fn* [] ~@body) :io ))
488
503
489
504
; ;;;;;;;;;;;;;;;;;;; ops ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
490
505
0 commit comments