diff options
author | Rich Hickey <richhickey@gmail.com> | 2007-12-02 15:55:47 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2007-12-02 15:55:47 +0000 |
commit | 89bf1ae672b543ad15789533e18954603df2e25b (patch) | |
tree | 9687fb2555111fcf1cbf3340a1df4a5885925798 /src | |
parent | b2bd83819314795cf43defcd361d77271467ca8d (diff) |
renamed Ref to TRef
Diffstat (limited to 'src')
-rw-r--r-- | src/boot.clj | 18 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Actor.java | 11 | ||||
-rw-r--r-- | src/jvm/clojure/lang/LockingTransaction.java | 35 | ||||
-rw-r--r-- | src/jvm/clojure/lang/TRef.java (renamed from src/jvm/clojure/lang/Ref.java) | 10 |
4 files changed, 36 insertions, 38 deletions
diff --git a/src/boot.clj b/src/boot.clj index d5e21f13..2c55a518 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -363,23 +363,23 @@ (. clojure.lang.Var (find sym))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Refs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defn ref [x] - (new clojure.lang.Ref x)) +(defn tref [x] + (new clojure.lang.TRef x)) (defn deref [#^clojure.lang.IRef ref] (. ref (get))) -(defn deref! [#^clojure.lang.Ref ref] +(defn deref! [#^clojure.lang.TRef ref] (. ref (currentVal))) -(defn commute [#^clojure.lang.Ref ref fun] +(defn commute [#^clojure.lang.TRef ref fun] (. ref (commute fun))) (defn set - ([#^clojure.lang.Ref ref] + ([#^clojure.lang.TRef ref] (. ref (touch)) (. ref (get))) - ([#^clojure.lang.Ref ref val] + ([#^clojure.lang.TRef ref val] (. ref (set val)))) (defmacro sync [flags-ignored-for-now & body] @@ -726,8 +726,8 @@ ([f coll] (let [nthreads (.. Runtime (getRuntime) (availableProcessors)) exec (. Executors (newFixedThreadPool nthreads)) - todo (ref (seq coll)) - out (ref 0) + todo (tref (seq coll)) + out (tref 0) q (new LinkedBlockingQueue) produce (fn [] (let [job (sync nil @@ -778,7 +778,7 @@ rseq sym name namespace locking .. -> defmulti defmethod remove-method binding find-var - ref deref deref! commute set sync + tref deref deref! commute set sync reduce reverse comp appl every not-every any not-any map pmap mapcat filter take take-while drop drop-while diff --git a/src/jvm/clojure/lang/Actor.java b/src/jvm/clojure/lang/Actor.java index 62c984c7..e0cdb08f 100644 --- a/src/jvm/clojure/lang/Actor.java +++ b/src/jvm/clojure/lang/Actor.java @@ -16,7 +16,6 @@ import java.util.Queue; import java.util.LinkedList; import java.util.concurrent.Executor; import java.util.concurrent.Executors; -import java.util.concurrent.LinkedBlockingQueue; public class Actor implements IRef{ volatile Object state; @@ -48,7 +47,7 @@ static class Action implements Runnable{ boolean hadError = false; try { - actor.commute(fn, args); + actor.doAlter(fn, args); } catch(Exception e) { @@ -110,7 +109,7 @@ public void clearErrors(){ errors = null; } -synchronized void commute(IFn fn, ISeq args) throws Exception{ +synchronized void doAlter(IFn fn, ISeq args) throws Exception{ try { commuting = true; @@ -122,7 +121,7 @@ synchronized void commute(IFn fn, ISeq args) throws Exception{ } } -public Object change(IFn fn, ISeq args) throws Exception{ +public Object alter(IFn fn, ISeq args) throws Exception{ if(errors != null) { throw new Exception("Actor has errors", (Exception) RT.first(errors)); @@ -139,7 +138,7 @@ public Object change(IFn fn, ISeq args) throws Exception{ try { inChange.set(this); - commute(fn, args); + doAlter(fn, args); } finally { @@ -149,7 +148,7 @@ public Object change(IFn fn, ISeq args) throws Exception{ return this; } -public Object send(IFn fn, ISeq args) throws Exception{ +public Object commute(IFn fn, ISeq args) throws Exception{ if(errors != null) { throw new Exception("Actor has errors", (Exception) RT.first(errors)); diff --git a/src/jvm/clojure/lang/LockingTransaction.java b/src/jvm/clojure/lang/LockingTransaction.java index 21baac6a..c3e81e1a 100644 --- a/src/jvm/clojure/lang/LockingTransaction.java +++ b/src/jvm/clojure/lang/LockingTransaction.java @@ -13,7 +13,6 @@ package clojure.lang; import java.util.*; -import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @SuppressWarnings({"SynchronizeOnNonFinalField"}) @@ -89,13 +88,13 @@ long startPoint; long startTime; final RetryException retryex = new RetryException(); final ArrayList<Actor.Action> actions = new ArrayList<Actor.Action>(); -final HashMap<Ref, Object> vals = new HashMap<Ref, Object>(); -final HashSet<Ref> sets = new HashSet<Ref>(); -final TreeMap<Ref, ArrayList<IFn>> commutes = new TreeMap<Ref, ArrayList<IFn>>(); +final HashMap<TRef, Object> vals = new HashMap<TRef, Object>(); +final HashSet<TRef> sets = new HashSet<TRef>(); +final TreeMap<TRef, ArrayList<IFn>> commutes = new TreeMap<TRef, ArrayList<IFn>>(); //returns the most recent val -Object lock(Ref ref){ +Object lock(TRef ref){ boolean unlocked = false; try { @@ -192,7 +191,7 @@ static public Object runInTransaction(IFn fn) throws Exception{ Object run(IFn fn) throws Exception{ boolean done = false; Object ret = null; - ArrayList<Ref> locked = new ArrayList<Ref>(); + ArrayList<TRef> locked = new ArrayList<TRef>(); for(int i = 0; !done && i < RETRY_LIMIT; i++) { @@ -209,9 +208,9 @@ Object run(IFn fn) throws Exception{ //make sure no one has killed us before this point, and can't from now on if(info.status.compareAndSet(RUNNING, COMMITTING)) { - for(Map.Entry<Ref, ArrayList<IFn>> e : commutes.entrySet()) + for(Map.Entry<TRef, ArrayList<IFn>> e : commutes.entrySet()) { - Ref ref = e.getKey(); + TRef ref = e.getKey(); ref.lock.writeLock().lock(); locked.add(ref); Info refinfo = ref.tinfo; @@ -228,7 +227,7 @@ Object run(IFn fn) throws Exception{ vals.put(ref, f.invoke(vals.get(ref))); } } - for(Ref ref : sets) + for(TRef ref : sets) { if(!commutes.containsKey(ref)) { @@ -241,16 +240,16 @@ Object run(IFn fn) throws Exception{ //no more client code to be called long msecs = System.currentTimeMillis(); long commitPoint = getCommitPoint(); - for(Map.Entry<Ref, Object> e : vals.entrySet()) + for(Map.Entry<TRef, Object> e : vals.entrySet()) { - Ref ref = e.getKey(); + TRef ref = e.getKey(); if(ref.tvals == null) { - ref.tvals = new Ref.TVal(e.getValue(), commitPoint, msecs); + ref.tvals = new TRef.TVal(e.getValue(), commitPoint, msecs); } else if(ref.faults.get() > 0) { - ref.tvals = new Ref.TVal(e.getValue(), commitPoint, msecs, ref.tvals); + ref.tvals = new TRef.TVal(e.getValue(), commitPoint, msecs, ref.tvals); ref.faults.set(0); } else @@ -292,7 +291,7 @@ public void enqueue(Actor.Action action){ actions.add(action); } -Object doGet(Ref ref){ +Object doGet(TRef ref){ if(!info.running()) throw retryex; if(vals.containsKey(ref)) @@ -302,7 +301,7 @@ Object doGet(Ref ref){ ref.lock.readLock().lock(); if(ref.tvals == null) throw new IllegalStateException(ref.toString() + " is unbound."); - Ref.TVal ver = ref.tvals; + TRef.TVal ver = ref.tvals; do { if(ver.point <= readPoint) @@ -319,7 +318,7 @@ Object doGet(Ref ref){ } -Object doSet(Ref ref, Object val){ +Object doSet(TRef ref, Object val){ if(!info.running()) throw retryex; if(commutes.containsKey(ref)) @@ -333,13 +332,13 @@ Object doSet(Ref ref, Object val){ return val; } -void doTouch(Ref ref){ +void doTouch(TRef ref){ if(!info.running()) throw retryex; lock(ref); } -Object doCommute(Ref ref, IFn fn) throws Exception{ +Object doCommute(TRef ref, IFn fn) throws Exception{ if(!info.running()) throw retryex; if(!vals.containsKey(ref)) diff --git a/src/jvm/clojure/lang/Ref.java b/src/jvm/clojure/lang/TRef.java index cbbfe5a3..fa81aa1d 100644 --- a/src/jvm/clojure/lang/Ref.java +++ b/src/jvm/clojure/lang/TRef.java @@ -16,9 +16,9 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.UUID; -public class Ref implements IFn, Comparable<Ref>, IRef{ +public class TRef implements IFn, Comparable<TRef>, IRef{ -public int compareTo(Ref o){ +public int compareTo(TRef o){ return uuid.compareTo(o.uuid); } @@ -55,7 +55,7 @@ final ReentrantReadWriteLock lock; LockingTransaction.Info tinfo; final UUID uuid; -public Ref(){ +public TRef(){ this.tvals = null; this.tinfo = null; this.faults = new AtomicInteger(); @@ -63,7 +63,7 @@ public Ref(){ this.uuid = UUID.randomUUID(); } -public Ref(Object initVal){ +public TRef(Object initVal){ this(); tvals = new TVal(initVal, 0, System.currentTimeMillis()); } @@ -71,7 +71,7 @@ public Ref(Object initVal){ //note - makes no attempt to ensure there is no other Ref with same UUID //use only with a cache/registry -public Ref(UUID uuid, Object initVal){ +public TRef(UUID uuid, Object initVal){ tvals = new TVal(initVal, 0, System.currentTimeMillis()); this.tinfo = null; this.faults = new AtomicInteger(); |