@@ -18,7 +18,33 @@ to validate go blocks do not invoke core.async blocking operations.
18
18
Property is read once, at namespace load time. Recommended for use
19
19
primarily during development. Invalid blocking calls will throw in
20
20
go block threads - use Thread.setDefaultUncaughtExceptionHandler()
21
- to catch and handle."
21
+ to catch and handle.
22
+
23
+ Use the Java system property `clojure.core.async.executor-factory`
24
+ to specify a function that will provide ExecutorServices for
25
+ application-wide use by core.async in lieu of its defaults. The
26
+ property value should name a fully qualified var. The function
27
+ will be passed a keyword indicating the context of use of the
28
+ executor, and should return either an ExecutorService, or nil to
29
+ use the default. Results per keyword will be cached and used for
30
+ the remainder of the application. Possible context arguments are:
31
+
32
+ :io - used in async/io-thread, for :io workloads in flow/process,
33
+ and for dispatch handling if no explicit dispatch handler is
34
+ provided (see below)
35
+
36
+ :mixed - used by async/thread and for :mixed workloads in
37
+ flow/process
38
+
39
+ :compute - used for :compute workloads in flow/process
40
+
41
+ :core-async-dispatch - used for completion fn handling (e.g. in put!
42
+ and take!, as well as go block IOC thunk processing) throughout
43
+ core.async. If not supplied the ExecutorService for :io will be
44
+ used instead.
45
+
46
+ The set of contexts may grow in the future so the function should
47
+ return nil for unexpected contexts."
22
48
(:refer-clojure :exclude [reduce transduce into merge map take partition
23
49
partition-by bounded-count])
24
50
(:require [clojure.core.async.impl.protocols :as impl]
@@ -29,7 +55,6 @@ to catch and handle."
29
55
[clojure.core.async.impl.ioc-macros :as ioc]
30
56
clojure.core.async.impl.go ; ; TODO: make conditional
31
57
[clojure.core.async.impl.mutex :as mutex]
32
- [clojure.core.async.impl.concurrent :as conc]
33
58
)
34
59
(:import [java.util.concurrent.atomic AtomicLong]
35
60
[java.util.concurrent.locks Lock]
@@ -468,33 +493,42 @@ to catch and handle."
468
493
[& body]
469
494
(#'clojure.core.async.impl.go/go-impl &env body))
470
495
471
- (defonce ^:private ^Executor thread-macro-executor
472
- (Executors/newCachedThreadPool (conc/counted-thread-factory " async-thread-macro-%d" true )))
473
-
474
496
(defn thread-call
475
497
" Executes f in another thread, returning immediately to the calling
476
498
thread. Returns a channel which will receive the result of calling
477
- f when completed, then close."
478
- [f]
479
- (let [c (chan 1 )]
480
- (let [binds (Var/getThreadBindingFrame )]
481
- (.execute thread-macro-executor
482
- (fn []
483
- (Var/resetThreadBindingFrame binds)
484
- (try
485
- (let [ret (f )]
486
- (when-not (nil? ret)
487
- (>!! c ret)))
488
- (finally
489
- (close! c))))))
490
- c))
499
+ f when completed, then close. workload is a keyword that describes
500
+ the work performed by f, where:
501
+
502
+ :io - may do blocking I/O but must not do extended computation
503
+ :compute - must not ever block
504
+ :mixed - anything else (default)
505
+
506
+ when workload not supplied, defaults to :mixed"
507
+ ([f] (thread-call f :mixed ))
508
+ ([f workload]
509
+ (let [c (chan 1 )
510
+ returning-to-chan (fn [bf]
511
+ #(try
512
+ (when-some [ret (bf )]
513
+ (>!! c ret))
514
+ (finally (close! c))))]
515
+ (-> f bound-fn* returning-to-chan (dispatch/exec workload))
516
+ c)))
491
517
492
518
(defmacro thread
493
519
" Executes the body in another thread, returning immediately to the
494
520
calling thread. Returns a channel which will receive the result of
495
521
the body when completed, then close."
496
522
[& body]
497
- `(thread-call (^:once fn* [] ~@body)))
523
+ `(thread-call (^:once fn* [] ~@body) :mixed ))
524
+
525
+ (defmacro io-thread
526
+ " Executes the body in a thread, returning immediately to the calling
527
+ thread. The body may do blocking I/O but must not do extended computation.
528
+ Returns a channel which will receive the result of the body when completed,
529
+ then close."
530
+ [& body]
531
+ `(thread-call (^:once fn* [] ~@body) :io ))
498
532
499
533
; ;;;;;;;;;;;;;;;;;;; ops ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
500
534
0 commit comments