@@ -462,41 +462,42 @@ to catch and handle."
462462 [& body]
463463 (#'clojure.core.async.impl.go/go-impl &env body))
464464
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)))))
472-
473465(defn thread-call
474466 " Executes f in another thread, returning immediately to the calling
475467 thread. Returns a channel which will receive the result of calling
476- f when completed, then close."
477- [f]
478- (let [c (chan 1 )]
479- (-> f bound-fn* (returning-chan c) (dispatch/exec :mixed ))
480- c))
468+ f when completed, then close. workload is a keyword that describes
469+ the expected profile of the work performed by f, where:
470+
471+ :io - may do blocking I/O but must not do extended computation
472+ :compute - must not ever block
473+ :mixed - anything else
474+
475+ Calls without a workload are assumed :mixed."
476+ ([f] (thread-call f :mixed ))
477+ ([f workload]
478+ (let [c (chan 1 )
479+ returning-to-chan (fn [bf]
480+ #(try
481+ (when-some [ret (bf )]
482+ (>!! c ret))
483+ (finally (close! c))))]
484+ (-> f bound-fn* returning-to-chan (dispatch/exec workload))
485+ c)))
481486
482487(defmacro thread
483488 " Executes the body in another thread, returning immediately to the
484489 calling thread. Returns a channel which will receive the result of
485490 the body when completed, then close."
486491 [& body]
487- `(let [c# (chan 1 )]
488- (-> (^:once fn* [] ~@body) bound-fn* (returning-chan c#) (dispatch/exec :mixed ))
489- c#))
492+ `(thread-call (^:once fn* [] ~@body) :mixed ))
490493
491494(defmacro io-thread
492- " Executes the body in a thread intended for blocking I/O workloads,
493- returning immediately to the calling thread. The body must not do
494- extended computation (if so, use 'thread' instead). Returns a channel
495- which will receive the result of the body when completed, then close."
495+ " Executes the body in a thread, returning immediately to the calling
496+ thread. The body may do blocking I/O but must not do extended computation.
497+ Returns a channel which will receive the result of the body when completed,
498+ then close."
496499 [& body]
497- `(let [c# (chan 1 )]
498- (-> (^:once fn* [] ~@body) bound-fn* (returning-chan c#) (dispatch/exec :io ))
499- c#))
500+ `(thread-call (^:once fn* [] ~@body) :io ))
500501
501502; ;;;;;;;;;;;;;;;;;;; ops ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
502503
0 commit comments