@@ -30,7 +30,7 @@ to catch and handle."
30
30
clojure.core.async.impl.go ; ; TODO: make conditional
31
31
[clojure.core.async.impl.mutex :as mutex]
32
32
[clojure.core.async.impl.concurrent :as conc]
33
- )
33
+ [clojure.core.async.impl.exec.threadpool :as threadp] )
34
34
(:import [java.util.concurrent.atomic AtomicLong]
35
35
[java.util.concurrent.locks Lock]
36
36
[java.util.concurrent Executors Executor ThreadLocalRandom ExecutorService]
@@ -462,45 +462,41 @@ to catch and handle."
462
462
[& body]
463
463
(#'clojure.core.async.impl.go/go-impl &env body))
464
464
465
- (defonce ^ExecutorService mixed-executor
466
- (Executors/newCachedThreadPool (conc/counted-thread-factory " async-mixed-%d" true )))
467
-
468
- (defonce ^ExecutorService io-executor
469
- (Executors/newCachedThreadPool (conc/counted-thread-factory " async-io-%d" true )))
470
-
471
- (defonce ^ExecutorService compute-executor
472
- (Executors/newCachedThreadPool (conc/counted-thread-factory " async-compute-%d" true )))
465
+ (defn- best-fit-thread-call
466
+ [f exec]
467
+ (let [c (chan 1 )
468
+ ^ExecutorService e (case exec
469
+ :compute threadp/compute-executor
470
+ :io threadp/io-executor
471
+ threadp/mixed-executor)]
472
+ (let [binds (Var/getThreadBindingFrame )]
473
+ (.execute e
474
+ (fn []
475
+ (Var/resetThreadBindingFrame binds)
476
+ (try
477
+ (let [ret (f )]
478
+ (when-not (nil? ret)
479
+ (>!! c ret)))
480
+ (finally
481
+ (close! c))))))
482
+ c))
473
483
474
484
(defn thread-call
475
485
" Executes f in another thread, returning immediately to the calling
476
486
thread. Returns a channel which will receive the result of calling
477
- f when completed, then close."
478
- ([f] (thread-call f :mixed ))
479
- ([f exec]
480
- (let [c (chan 1 )
481
- ^ExecutorService e (case exec
482
- :compute compute-executor
483
- :io io-executor
484
- mixed-executor)]
485
- (let [binds (Var/getThreadBindingFrame )]
486
- (.execute e
487
- (fn []
488
- (Var/resetThreadBindingFrame binds)
489
- (try
490
- (let [ret (f )]
491
- (when-not (nil? ret)
492
- (>!! c ret)))
493
- (finally
494
- (close! c))))))
495
- c)))
487
+ f when completed, then close. exec is a keyword that describes the
488
+ nature of f's workload, one of :mixed (default) :io or :compute
489
+ whereby core.async may be able to choose a best fit thread type."
490
+ [f]
491
+ (best-fit-thread-call f :mixed ))
496
492
497
493
(defmacro io-thread
498
494
" Executes the body in a thread intended for blocking I/O workloads,
499
495
returning immediately to the calling thread. The body must not do
500
496
extended computation (if so, use 'thread' instead). Returns a channel
501
497
which will receive the result of the body when completed, then close."
502
498
[& body]
503
- `(thread-call (^:once fn* [] ~@body) :io ))
499
+ `(#'best-fit- thread-call (^:once fn* [] ~@body) :io ))
504
500
505
501
(defmacro thread
506
502
" Executes the body in another thread, returning immediately to the
0 commit comments