@@ -18,7 +18,33 @@ to validate go blocks do not invoke core.async blocking operations.
1818Property is read once, at namespace load time. Recommended for use
1919primarily during development. Invalid blocking calls will throw in
2020go 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."
2248 (:refer-clojure :exclude [reduce transduce into merge map take partition
2349 partition-by bounded-count])
2450 (:require [clojure.core.async.impl.protocols :as impl]
@@ -29,7 +55,6 @@ to catch and handle."
2955 [clojure.core.async.impl.ioc-macros :as ioc]
3056 clojure.core.async.impl.go ; ; TODO: make conditional
3157 [clojure.core.async.impl.mutex :as mutex]
32- [clojure.core.async.impl.concurrent :as conc]
3358 )
3459 (:import [java.util.concurrent.atomic AtomicLong]
3560 [java.util.concurrent.locks Lock]
@@ -468,33 +493,42 @@ to catch and handle."
468493 [& body]
469494 (#'clojure.core.async.impl.go/go-impl &env body))
470495
471- (defonce ^:private ^Executor thread-macro-executor
472- (Executors/newCachedThreadPool (conc/counted-thread-factory " async-thread-macro-%d" true )))
473-
474496(defn thread-call
475497 " Executes f in another thread, returning immediately to the calling
476498 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)))
491517
492518(defmacro thread
493519 " Executes the body in another thread, returning immediately to the
494520 calling thread. Returns a channel which will receive the result of
495521 the body when completed, then close."
496522 [& 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 ))
498532
499533; ;;;;;;;;;;;;;;;;;;; ops ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
500534
0 commit comments