Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added set-agent-send(-off)-executor! and send-via

  • Loading branch information...
commit f5f4faf95051f794c9bfa0315e4457b600c84cef 1 parent 5af28af
@richhickey richhickey authored
Showing with 33 additions and 13 deletions.
  1. +24 −2 src/clj/clojure/core.clj
  2. +9 −11 src/jvm/clojure/lang/Agent.java
View
26 src/clj/clojure/core.clj
@@ -1877,6 +1877,28 @@
(if (:error-handler opts) :continue :fail)))
a)))
+(defn set-agent-send-executor!
+ "Sets the ExecutorService to be used by send"
+ {:added "1.5"}
+ [executor]
+ (set! clojure.lang.Agent/pooledExecutor executor))
+
+(defn set-agent-send-off-executor!
+ "Sets the ExecutorService to be used by send-off"
+ {:added "1.5"}
+ [executor]
+ (set! clojure.lang.Agent/soloExecutor executor))
+
+(defn send-via
+ "Dispatch an action to an agent. Returns the agent immediately.
+ Subsequently, in a thread supplied by executor, the state of the agent
+ will be set to the value of:
+
+ (apply action-fn state-of-agent args)"
+ {:added "1.5"}
+ [executor ^clojure.lang.Agent a f & args]
+ (.dispatch a (binding [*agent* a] (binding-conveyor-fn f)) args executor))
+
(defn send
"Dispatch an action to an agent. Returns the agent immediately.
Subsequently, in a thread from a thread pool, the state of the agent
@@ -1886,7 +1908,7 @@
{:added "1.0"
:static true}
[^clojure.lang.Agent a f & args]
- (.dispatch a (binding [*agent* a] (binding-conveyor-fn f)) args false))
+ (apply send-via clojure.lang.Agent/pooledExecutor a f args))
(defn send-off
"Dispatch a potentially blocking action to an agent. Returns the
@@ -1897,7 +1919,7 @@
{:added "1.0"
:static true}
[^clojure.lang.Agent a f & args]
- (.dispatch a (binding [*agent* a] (binding-conveyor-fn f)) args true))
+ (apply send-via clojure.lang.Agent/soloExecutor a f args))
(defn release-pending-sends
"Normally, actions sent directly or indirectly during another action
View
20 src/jvm/clojure/lang/Agent.java
@@ -12,6 +12,7 @@
package clojure.lang;
+import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
@@ -45,11 +46,11 @@ public ActionQueue( IPersistentStack q, Throwable error )
final private static AtomicLong sendOffThreadPoolCounter = new AtomicLong(0);
-final public static ExecutorService pooledExecutor =
+volatile public static ExecutorService pooledExecutor =
Executors.newFixedThreadPool(2 + Runtime.getRuntime().availableProcessors(),
createThreadFactory("clojure-agent-send-pool-%d", sendThreadPoolCounter));
-final public static ExecutorService soloExecutor = Executors.newCachedThreadPool(
+volatile public static ExecutorService soloExecutor = Executors.newCachedThreadPool(
createThreadFactory("clojure-agent-send-off-pool-%d", sendOffThreadPoolCounter));
final static ThreadLocal<IPersistentVector> nested = new ThreadLocal<IPersistentVector>();
@@ -73,23 +74,20 @@ public static void shutdown(){
final Agent agent;
final IFn fn;
final ISeq args;
- final boolean solo;
+ final Executor exec;
- public Action(Agent agent, IFn fn, ISeq args, boolean solo){
+ public Action(Agent agent, IFn fn, ISeq args, Executor exec){
this.agent = agent;
this.args = args;
this.fn = fn;
- this.solo = solo;
+ this.exec = exec;
}
void execute(){
try
{
- if(solo)
- soloExecutor.execute(this);
- else
- pooledExecutor.execute(this);
+ exec.execute(this);
}
catch(Throwable error)
{
@@ -233,13 +231,13 @@ synchronized public Object restart(Object newState, boolean clearActions){
return newState;
}
-public Object dispatch(IFn fn, ISeq args, boolean solo) {
+public Object dispatch(IFn fn, ISeq args, Executor exec) {
Throwable error = getError();
if(error != null)
{
throw Util.runtimeException("Agent is failed, needs restart", error);
}
- Action action = new Action(this, fn, args, solo);
+ Action action = new Action(this, fn, args, exec);
dispatchAction(action);
return this;

0 comments on commit f5f4faf

Please sign in to comment.
Something went wrong with that request. Please try again.