|
44 | 44 | any use of the async thread pool."
|
45 | 45 | (delay (or (Long/getLong "clojure.core.async.pool-size") 8)))
|
46 | 46 |
|
47 |
| -(defn thread-pool-executor |
48 |
| - ([] |
49 |
| - (thread-pool-executor nil)) |
50 |
| - ([init-fn] |
51 |
| - (let [executor-svc (Executors/newFixedThreadPool |
52 |
| - @pool-size |
53 |
| - (counted-thread-factory "async-dispatch-%d" true |
54 |
| - {:init-fn init-fn}))] |
55 |
| - (reify impl/Executor |
56 |
| - (impl/exec [_ r] |
57 |
| - (.execute executor-svc ^Runnable r)))))) |
58 |
| - |
59 | 47 | (defonce ^:private in-dispatch (ThreadLocal.))
|
60 | 48 |
|
61 |
| -(defonce executor |
62 |
| - (delay (thread-pool-executor #(.set ^ThreadLocal in-dispatch true)))) |
63 |
| - |
64 | 49 | (defn in-dispatch-thread?
|
65 | 50 | "Returns true if the current thread is a go block dispatch pool thread"
|
66 | 51 | []
|
|
84 | 69 | [workflow]
|
85 | 70 | (Executors/newCachedThreadPool (counted-thread-factory (str "async-" (name workflow) "-%d") true)))
|
86 | 71 |
|
87 |
| -(defn- default-construct-executor |
88 |
| - [workload] |
89 |
| - (case workload |
90 |
| - :compute (executor-ctor :compute) |
91 |
| - :io (executor-ctor :io) |
92 |
| - :mixed (executor-ctor :mixed))) |
| 72 | +(def ^:private default-construct-executor |
| 73 | + (memoize |
| 74 | + (fn [workload] |
| 75 | + (case workload |
| 76 | + :compute (executor-ctor :compute) |
| 77 | + :io (executor-ctor :io) |
| 78 | + :mixed (executor-ctor :mixed) |
| 79 | + :core-async-dispatch (default-construct-executor :io))))) |
93 | 80 |
|
94 | 81 | (defn construct-executor
|
95 |
| - [workload] |
| 82 | + ^ExecutorService [workload] |
96 | 83 | (if-let [sysprop-ctor (when-let [esf (System/getProperty "clojure.core.async.executor-factory")]
|
97 | 84 | (requiring-resolve (symbol esf)))]
|
98 | 85 | (or (sysprop-ctor workload) (default-construct-executor workload))
|
|
108 | 95 | (let [^ExecutorService e (executor-for workload)]
|
109 | 96 | (.execute e r)))
|
110 | 97 |
|
| 98 | +(defonce executor |
| 99 | + (delay (let [executor-svc (construct-executor :core-async-dispatch)] |
| 100 | + (reify impl/Executor |
| 101 | + (impl/exec [_ r] |
| 102 | + (.execute executor-svc ^Runnable r)))))) |
| 103 | + |
111 | 104 | (defn run
|
112 | 105 | "Runs Runnable r on current thread when :on-caller? meta true, else in a thread pool thread."
|
113 | 106 | [^Runnable r]
|
|
0 commit comments