diff options
Diffstat (limited to 'src/jvm')
-rw-r--r-- | src/jvm/clojure/lang/Agent.java | 23 | ||||
-rw-r--r-- | src/jvm/clojure/lang/FJTask.java | 51 |
2 files changed, 71 insertions, 3 deletions
diff --git a/src/jvm/clojure/lang/Agent.java b/src/jvm/clojure/lang/Agent.java index 310c826b..df06acd8 100644 --- a/src/jvm/clojure/lang/Agent.java +++ b/src/jvm/clojure/lang/Agent.java @@ -12,6 +12,8 @@ package clojure.lang; +import jsr166y.ForkJoinPool; + import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicReference; import java.util.Map; @@ -22,13 +24,28 @@ volatile Object state; volatile ISeq errors = null; -final public static ExecutorService pooledExecutor = - Executors.newFixedThreadPool(2 + Runtime.getRuntime().availableProcessors()); - +//these are public for diagnostic reasons, don't mess with them otherwise +volatile public static ExecutorService pooledExecutor; final public static ExecutorService soloExecutor = Executors.newCachedThreadPool(); final static ThreadLocal<IPersistentVector> nested = new ThreadLocal<IPersistentVector>(); +static + { + boolean gotfj = true; + try + { + Class fjpool = Class.forName("jsr166y.ForkJoinPool"); + pooledExecutor = (ExecutorService) fjpool.newInstance(); + ((ForkJoinPool)pooledExecutor).setAsyncMode(true); + } + catch(Exception e) + { + gotfj = false; + } + if(!gotfj) + pooledExecutor = Executors.newFixedThreadPool(2 + Runtime.getRuntime().availableProcessors()); + } public static void shutdown(){ soloExecutor.shutdown(); diff --git a/src/jvm/clojure/lang/FJTask.java b/src/jvm/clojure/lang/FJTask.java new file mode 100644 index 00000000..c9f33789 --- /dev/null +++ b/src/jvm/clojure/lang/FJTask.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) + * which can be found in the file epl-v10.html at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * You must not remove this notice, or any other, from this software. + **/ + +/* rich Jul 21, 2009 */ + +package clojure.lang; + +import jsr166y.ForkJoinTask; + +import java.util.concurrent.Callable; + +public class FJTask extends ForkJoinTask<Object>{ +final Callable f; +Object result; + +public FJTask(Callable f){ + this.f = f; +} + +public Object compute(){ + try + { + return f.call(); + } + catch(Exception e) + { + throw new RuntimeException(e); + } +} + +public final Object getRawResult(){ + return result; +} + +protected final void setRawResult(Object value){ + result = value; +} + +protected final boolean exec(){ + result = compute(); + return true; +} + +} |