Skip to content

Commit

Permalink
set-executer! and set-thread-executor! have been added to set the def…
Browse files Browse the repository at this point in the history
…ault executors
  • Loading branch information
lxsameer committed May 8, 2018
1 parent 082c93f commit 5cfa3ac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/main/clojure/clojure/core/async.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
(ns clojure.core.async
"Facilities for async programming and communication.
go blocks are dispatched over an internal thread pool, which
defaults to 8 threads. The size of this pool can be modified using
the Java system property `clojure.core.async.pool-size`."
go blocks are dispatched over an internal thread pool, which
defaults to 8 threads. The size of this pool can be modified using
the Java system property `clojure.core.async.pool-size`."
(:refer-clojure :exclude [reduce transduce into merge map take partition
partition-by bounded-count])
(:require [clojure.core.async.impl.protocols :as impl]
Expand All @@ -22,6 +22,7 @@ the Java system property `clojure.core.async.pool-size`."
[clojure.core.async.impl.ioc-macros :as ioc]
[clojure.core.async.impl.mutex :as mutex]
[clojure.core.async.impl.concurrent :as conc]
[clojure.core.async.impl.exec.threadpool :as tp]
)
(:import [java.util.concurrent.locks Lock]
[java.util.concurrent Executors Executor ThreadLocalRandom]
Expand Down Expand Up @@ -424,17 +425,14 @@ the Java system property `clojure.core.async.pool-size`."
(ioc/run-state-machine-wrapped state#))))
c#)))

(defonce ^:private ^Executor thread-macro-executor
(Executors/newCachedThreadPool (conc/counted-thread-factory "async-thread-macro-%d" true)))

(defn thread-call
"Executes f in another thread, returning immediately to the calling
thread. Returns a channel which will receive the result of calling
f when completed, then close."
[f]
(let [c (chan 1)]
(let [binds (clojure.lang.Var/getThreadBindingFrame)]
(.execute thread-macro-executor
(.execute @tp/thread-macro-executor
(fn []
(clojure.lang.Var/resetThreadBindingFrame binds)
(try
Expand Down
1 change: 1 addition & 0 deletions src/main/clojure/clojure/core/async/impl/dispatch.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

(defonce executor (delay (tp/thread-pool-executor)))


(defn run
"Runs Runnable r in a thread pool thread"
[^Runnable r]
Expand Down
21 changes: 18 additions & 3 deletions src/main/clojure/clojure/core/async/impl/exec/threadpool.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@
(Long/parseLong prop))
8)))

(def main-executor
(atom (Executors/newFixedThreadPool
@pool-size
(conc/counted-thread-factory "async-dispatch-%d" true))))

(def thread-macro-executor
(atom (Executors/newCachedThreadPool
(conc/counted-thread-factory "async-thread-macro-%d" true))))

(defn set-executor!
[executor]
(reset! main-executor executor))

(defn set-thread-executor!
[executor]
(reset! thread-macro-executor executor))

(defn thread-pool-executor
[]
(let [executor-svc (Executors/newFixedThreadPool
@pool-size
(conc/counted-thread-factory "async-dispatch-%d" true))]
(let [executor-svc @main-executor]
(reify impl/Executor
(impl/exec [this r]
(.execute executor-svc ^Runnable r)))))

0 comments on commit 5cfa3ac

Please sign in to comment.