| 20 | import java.util.concurrent.atomic.AtomicReference; |
| 21 | |
| 22 | public class Agent extends ARef { |
| 23 | |
| 24 | static class ActionQueue { |
| 25 | public final IPersistentStack q; |
| 26 | public final Throwable error; // non-null indicates fail state |
| 27 | static final ActionQueue EMPTY = new ActionQueue(PersistentQueue.EMPTY, null); |
| 28 | |
| 29 | public ActionQueue( IPersistentStack q, Throwable error ) |
| 30 | { |
| 31 | this.q = q; |
| 32 | this.error = error; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | static final Keyword CONTINUE = Keyword.intern(null, "continue"); |
| 37 | static final Keyword FAIL = Keyword.intern(null, "fail"); |
| 38 | |
| 39 | volatile Object state; |
| 40 | AtomicReference<ActionQueue> aq = new AtomicReference<ActionQueue>(ActionQueue.EMPTY); |
| 41 | |
| 42 | volatile Keyword errorMode = CONTINUE; |
| 43 | volatile IFn errorHandler = null; |
| 44 | |
| 45 | final private static AtomicLong sendThreadPoolCounter = new AtomicLong(0); |
| 46 | |
| 47 | final private static AtomicLong sendOffThreadPoolCounter = new AtomicLong(0); |
| 48 | |
| 49 | volatile public static ExecutorService pooledExecutor = |
| 50 | Executors.newFixedThreadPool(2 + Runtime.getRuntime().availableProcessors(), |
| 51 | createThreadFactory("clojure-agent-send-pool-%d", sendThreadPoolCounter)); |
| 52 | |
| 53 | volatile public static ExecutorService soloExecutor = Executors.newCachedThreadPool( |
| 54 | createThreadFactory("clojure-agent-send-off-pool-%d", sendOffThreadPoolCounter)); |
| 55 | |
| 56 | final static ThreadLocal<IPersistentVector> nested = new ThreadLocal<IPersistentVector>(); |
| 57 | |
| 58 | private static ThreadFactory createThreadFactory(final String format, final AtomicLong threadPoolCounter) { |
| 59 | return new ThreadFactory() { |
| 60 | public Thread newThread(Runnable runnable) { |
| 61 | Thread thread = new Thread(runnable); |
| 62 | thread.setName(String.format(format, threadPoolCounter.getAndIncrement())); |
| 63 | return thread; |
| 64 | } |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | public static void shutdown(){ |
| 69 | soloExecutor.shutdown(); |
| 70 | pooledExecutor.shutdown(); |
| 71 | } |
| 72 | |
| 73 | static class Action implements Runnable{ |
| 74 | final Agent agent; |
| 75 | final IFn fn; |
| 76 | final ISeq args; |
| 77 | final Executor exec; |
| 78 | |
| 79 |
nothing calls this directly
no test coverage detected