Skip to content

Commit cfee9b4

Browse files
committed
merged concurrent and threadpool into dispatch
1 parent bfec841 commit cfee9b4

File tree

9 files changed

+58
-89
lines changed

9 files changed

+58
-89
lines changed

build.clj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
(b/delete {:path "target"})
1111
(b/compile-clj {:basis basis, :src-dirs ["src/main/clojure"], :class-dir class-dir,
1212
:filter-nses '[clojure.core.async]
13-
:ns-compile '[clojure.core.async.impl.exec.threadpool
14-
clojure.core.async.impl.protocols
13+
:ns-compile '[clojure.core.async.impl.protocols
1514
clojure.core.async.impl.mutex
16-
clojure.core.async.impl.concurrent
1715
clojure.core.async.impl.dispatch
1816
clojure.core.async.impl.ioc-macros
1917
clojure.core.async.impl.buffers

deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{:paths ["src/main/clojure"]
22
:deps
3-
{org.clojure/tools.analyzer.jvm {:mvn/version "1.3.2"}}
3+
{org.clojure/tools.analyzer.jvm {:mvn/version "1.3.1"}}
44
:aliases
55
{:cljs-test {:extra-deps {org.clojure/clojurescript {:mvn/version "1.11.132"}}
66
:extra-paths ["src/main/clojure/cljs" "src/test/cljs"]}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<dependency>
4545
<groupId>org.clojure</groupId>
4646
<artifactId>tools.analyzer.jvm</artifactId>
47-
<version>1.3.2</version>
47+
<version>1.3.1</version>
4848
</dependency>
4949
</dependencies>
5050

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
66
:parent [org.clojure/pom.contrib "1.2.0"]
77
:dependencies [[org.clojure/clojure "1.11.4"]
8-
[org.clojure/tools.analyzer.jvm "1.3.2"]
8+
[org.clojure/tools.analyzer.jvm "1.3.1"]
99
[org.clojure/clojurescript "1.11.132" :scope "provided"]]
1010
:global-vars {*warn-on-reflection* true}
1111
:source-paths ["src/main/clojure"]

src/main/clojure/clojure/core/async.clj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ to catch and handle."
2929
[clojure.core.async.impl.ioc-macros :as ioc]
3030
clojure.core.async.impl.go ;; TODO: make conditional
3131
[clojure.core.async.impl.mutex :as mutex]
32-
[clojure.core.async.impl.concurrent :as conc]
3332
)
3433
(:import [java.util.concurrent.atomic AtomicLong]
3534
[java.util.concurrent.locks Lock]

src/main/clojure/clojure/core/async/impl/concurrent.clj

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/clojure/clojure/core/async/impl/dispatch.clj

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,58 @@
88

99
(ns ^{:skip-wiki true}
1010
clojure.core.async.impl.dispatch
11-
(:require [clojure.core.async.impl.protocols :as impl]
12-
[clojure.core.async.impl.exec.threadpool :as tp])
13-
(:import [java.util.concurrent Executors ExecutorService]))
11+
(:require [clojure.core.async.impl.protocols :as impl])
12+
(:import [java.util.concurrent Executors ExecutorService ThreadFactory]))
1413

1514
(set! *warn-on-reflection* true)
1615

16+
(defn counted-thread-factory
17+
"Create a ThreadFactory that maintains a counter for naming Threads.
18+
name-format specifies thread names - use %d to include counter
19+
daemon is a flag for whether threads are daemons or not
20+
opts is an options map:
21+
init-fn - function to run when thread is created"
22+
([name-format daemon]
23+
(counted-thread-factory name-format daemon nil))
24+
([name-format daemon {:keys [init-fn] :as opts}]
25+
(let [counter (atom 0)]
26+
(reify
27+
ThreadFactory
28+
(newThread [_this runnable]
29+
(let [body (if init-fn
30+
(fn [] (init-fn) (.run ^Runnable runnable))
31+
runnable)
32+
t (Thread. ^Runnable body)]
33+
(doto t
34+
(.setName (format name-format (swap! counter inc)))
35+
(.setDaemon daemon))))))))
36+
37+
(defonce
38+
^{:doc "Number of processors reported by the JVM"}
39+
processors (.availableProcessors (Runtime/getRuntime)))
40+
41+
(def ^:private pool-size
42+
"Value is set via clojure.core.async.pool-size system property; defaults to 8; uses a
43+
delay so property can be set from code after core.async namespace is loaded but before
44+
any use of the async thread pool."
45+
(delay (or (Long/getLong "clojure.core.async.pool-size") 8)))
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+
1759
(defonce ^:private in-dispatch (ThreadLocal.))
1860

1961
(defonce executor
20-
(delay (tp/thread-pool-executor #(.set ^ThreadLocal in-dispatch true))))
62+
(delay (thread-pool-executor #(.set ^ThreadLocal in-dispatch true))))
2163

2264
(defn in-dispatch-thread?
2365
"Returns true if the current thread is a go block dispatch pool thread"
@@ -39,13 +81,13 @@
3981
nil)
4082

4183
(defonce ^ExecutorService mixed-executor
42-
(Executors/newCachedThreadPool (conc/counted-thread-factory "async-mixed-%d" true)))
84+
(Executors/newCachedThreadPool (counted-thread-factory "async-mixed-%d" true)))
4385

4486
(defonce ^ExecutorService io-executor
45-
(Executors/newCachedThreadPool (conc/counted-thread-factory "async-io-%d" true)))
87+
(Executors/newCachedThreadPool (counted-thread-factory "async-io-%d" true)))
4688

4789
(defonce ^ExecutorService compute-executor
48-
(Executors/newCachedThreadPool (conc/counted-thread-factory "async-compute-%d" true)))
90+
(Executors/newCachedThreadPool (counted-thread-factory "async-compute-%d" true)))
4991

5092
(defn run
5193
"Runs Runnable r on current thread when :on-caller? meta true, else in a thread pool thread."
@@ -57,7 +99,7 @@
5799
(defn exec
58100
[f exec]
59101
(let [^ExecutorService e (case exec
60-
:compute tp/compute-executor
61-
:io tp/io-executor
62-
tp/mixed-executor)]
102+
:compute compute-executor
103+
:io io-executor
104+
mixed-executor)]
63105
(.execute e f)))

src/main/clojure/clojure/core/async/impl/exec/threadpool.clj

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/test/clojure/clojure/core/async/concurrent_test.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
(ns clojure.core.async.concurrent-test
1010
(:require [clojure.test :refer :all]
11-
[clojure.core.async.impl.concurrent :as conc])
11+
[clojure.core.async.impl.dispatch :as dispatch])
1212
(:import [java.util.concurrent ThreadFactory]))
1313

1414
(deftest test-counted-thread-factory
1515
(testing "Creates numbered threads"
16-
(let [^ThreadFactory factory (conc/counted-thread-factory "foo-%d" true)
16+
(let [^ThreadFactory factory (dispatch/counted-thread-factory "foo-%d" true)
1717
threads (repeatedly 3 #(.newThread factory (constantly nil)))]
1818
(is (= ["foo-1" "foo-2" "foo-3"] (map #(.getName ^Thread %) threads))))))
1919

0 commit comments

Comments
 (0)