Skip to content

Commit 66f985c

Browse files
committed
rearranged to avoid internal require
1 parent 10fb0a3 commit 66f985c

File tree

4 files changed

+40
-49
lines changed

4 files changed

+40
-49
lines changed

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

+20-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ to catch and handle."
3434
(:import [java.util.concurrent.atomic AtomicLong]
3535
[java.util.concurrent.locks Lock]
3636
[java.util.concurrent Executors Executor ThreadLocalRandom]
37-
[java.util Arrays ArrayList]))
37+
[java.util Arrays ArrayList]
38+
[clojure.lang Var]))
3839

3940
(alias 'core 'clojure.core)
4041

@@ -462,29 +463,43 @@ to catch and handle."
462463
[& body]
463464
(#'clojure.core.async.impl.go/go-impl &env body))
464465

465-
(require '[clojure.core.async.impl.exec.services :as exec-services])
466+
(defn thread-impl
467+
[f workload]
468+
(let [c (chan 1)]
469+
(let [binds (Var/getThreadBindingFrame)]
470+
(dispatch/executor-service-call
471+
(fn []
472+
(Var/resetThreadBindingFrame binds)
473+
(try
474+
(let [ret (f)]
475+
(when-not (nil? ret)
476+
(>!! c ret)))
477+
(finally
478+
(close! c))))
479+
workload))
480+
c))
466481

467482
(defn thread-call
468483
"Executes f in another thread, returning immediately to the calling
469484
thread. Returns a channel which will receive the result of calling
470485
f when completed, then close."
471486
[f]
472-
(exec-services/thread-call f :mixed))
487+
(thread-impl f :mixed))
473488

474489
(defmacro thread
475490
"Executes the body in another thread, returning immediately to the
476491
calling thread. Returns a channel which will receive the result of
477492
the body when completed, then close."
478493
[& body]
479-
`(thread-call (^:once fn* [] ~@body)))
494+
`(thread-impl (^:once fn* [] ~@body) :mixed))
480495

481496
(defmacro io-thread
482497
"Executes the body in a thread intended for blocking I/O workloads,
483498
returning immediately to the calling thread. The body must not do
484499
extended computation (if so, use 'thread' instead). Returns a channel
485500
which will receive the result of the body when completed, then close."
486501
[& body]
487-
`(exec-services/thread-call (^:once fn* [] ~@body) :io))
502+
`(thread-impl (^:once fn* [] ~@body) :io))
488503

489504
;;;;;;;;;;;;;;;;;;;; ops ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
490505

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
(ns ^{:skip-wiki true}
1010
clojure.core.async.impl.dispatch
1111
(:require [clojure.core.async.impl.protocols :as impl]
12-
[clojure.core.async.impl.exec.threadpool :as tp]))
12+
[clojure.core.async.impl.exec.threadpool :as tp])
13+
(:import [java.util.concurrent ExecutorService]))
1314

1415
(set! *warn-on-reflection* true)
1516

@@ -43,3 +44,11 @@
4344
(if (-> r meta :on-caller?)
4445
(try (.run r) (catch Throwable t (ex-handler t)))
4546
(impl/exec @executor r)))
47+
48+
(defn executor-service-call
49+
[f exec]
50+
(let [^ExecutorService e (case exec
51+
:compute tp/compute-executor
52+
:io tp/io-executor
53+
tp/mixed-executor)]
54+
(.execute e f)))

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

-42
This file was deleted.

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(ns clojure.core.async.impl.exec.threadpool
1010
(:require [clojure.core.async.impl.protocols :as impl]
1111
[clojure.core.async.impl.concurrent :as conc])
12-
(:import [java.util.concurrent Executors]))
12+
(:import [java.util.concurrent Executors ExecutorService]))
1313

1414
(set! *warn-on-reflection* true)
1515

@@ -30,3 +30,12 @@
3030
(reify impl/Executor
3131
(impl/exec [_ r]
3232
(.execute executor-svc ^Runnable r))))))
33+
34+
(defonce ^ExecutorService mixed-executor
35+
(Executors/newCachedThreadPool (conc/counted-thread-factory "async-mixed-%d" true)))
36+
37+
(defonce ^ExecutorService io-executor
38+
(Executors/newCachedThreadPool (conc/counted-thread-factory "async-io-%d" true)))
39+
40+
(defonce ^ExecutorService compute-executor
41+
(Executors/newCachedThreadPool (conc/counted-thread-factory "async-compute-%d" true)))

0 commit comments

Comments
 (0)