diff options
author | Rich Hickey <richhickey@gmail.com> | 2007-12-03 19:04:26 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2007-12-03 19:04:26 +0000 |
commit | 0822f76b347fa29643ad8de5f4b5e15434145fab (patch) | |
tree | fb9625a54b746e82f6db862cb11570bba94e9ed6 | |
parent | dacf426c70b836581ab6a505e6ae45355582122f (diff) |
agents
-rw-r--r-- | src/boot.clj | 18 | ||||
-rw-r--r-- | src/jvm/clojure/lang/LockingTransaction.java | 34 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Ref.java (renamed from src/jvm/clojure/lang/TRef.java) | 10 |
3 files changed, 31 insertions, 31 deletions
diff --git a/src/boot.clj b/src/boot.clj index 01be8e60..7860ed00 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -365,22 +365,22 @@ (defn clear-agent-errors [#^clojure.lang.Agent a] (. a (clearErrors))) -(defn tref [x] - (new clojure.lang.TRef x)) +(defn ref [x] + (new clojure.lang.Ref x)) (defn deref [#^clojure.lang.IRef ref] (. ref (get))) -(defn commute [#^clojure.lang.TRef ref fun & args] +(defn commute [#^clojure.lang.Ref ref fun & args] (. ref (commute fun args))) -(defn alter [#^clojure.lang.TRef ref fun & args] +(defn alter [#^clojure.lang.Ref ref fun & args] (. ref (alter fun args))) -(defn set [#^clojure.lang.TRef ref val] +(defn set [#^clojure.lang.Ref ref val] (. ref (set val))) -(defn ensure [#^clojure.lang.TRef ref] +(defn ensure [#^clojure.lang.Ref ref] (. ref (touch)) (. ref (get))) @@ -728,8 +728,8 @@ ([f coll] (let [nthreads (.. Runtime (getRuntime) (availableProcessors)) exec (. Executors (newFixedThreadPool nthreads)) - todo (tref (seq coll)) - out (tref 0) + todo (ref (seq coll)) + out (ref 0) q (new LinkedBlockingQueue) produce (fn [] (let [job (sync nil @@ -780,7 +780,7 @@ rseq sym name namespace locking .. -> defmulti defmethod remove-method binding find-var - tref deref commute alter set ensure sync ! + ref deref commute alter set ensure sync ! agent agent-of agent-errors clear-agent-errors reduce reverse comp appl every not-every any not-any diff --git a/src/jvm/clojure/lang/LockingTransaction.java b/src/jvm/clojure/lang/LockingTransaction.java index ccc8102d..50afb99c 100644 --- a/src/jvm/clojure/lang/LockingTransaction.java +++ b/src/jvm/clojure/lang/LockingTransaction.java @@ -98,13 +98,13 @@ long startPoint; long startTime; final RetryException retryex = new RetryException(); final ArrayList<Agent.Action> actions = new ArrayList<Agent.Action>(); -final HashMap<TRef, Object> vals = new HashMap<TRef, Object>(); -final HashSet<TRef> sets = new HashSet<TRef>(); -final TreeMap<TRef, ArrayList<CFn>> commutes = new TreeMap<TRef, ArrayList<CFn>>(); +final HashMap<Ref, Object> vals = new HashMap<Ref, Object>(); +final HashSet<Ref> sets = new HashSet<Ref>(); +final TreeMap<Ref, ArrayList<CFn>> commutes = new TreeMap<Ref, ArrayList<CFn>>(); //returns the most recent val -Object lock(TRef ref){ +Object lock(Ref ref){ boolean unlocked = false; try { @@ -201,7 +201,7 @@ static public Object runInTransaction(IFn fn) throws Exception{ Object run(IFn fn) throws Exception{ boolean done = false; Object ret = null; - ArrayList<TRef> locked = new ArrayList<TRef>(); + ArrayList<Ref> locked = new ArrayList<Ref>(); for(int i = 0; !done && i < RETRY_LIMIT; i++) { @@ -218,9 +218,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<TRef, ArrayList<CFn>> e : commutes.entrySet()) + for(Map.Entry<Ref, ArrayList<CFn>> e : commutes.entrySet()) { - TRef ref = e.getKey(); + Ref ref = e.getKey(); ref.lock.writeLock().lock(); locked.add(ref); Info refinfo = ref.tinfo; @@ -237,7 +237,7 @@ Object run(IFn fn) throws Exception{ vals.put(ref, f.fn.applyTo(RT.cons(vals.get(ref), f.args))); } } - for(TRef ref : sets) + for(Ref ref : sets) { if(!commutes.containsKey(ref)) { @@ -250,16 +250,16 @@ Object run(IFn fn) throws Exception{ //no more client code to be called long msecs = System.currentTimeMillis(); long commitPoint = getCommitPoint(); - for(Map.Entry<TRef, Object> e : vals.entrySet()) + for(Map.Entry<Ref, Object> e : vals.entrySet()) { - TRef ref = e.getKey(); + Ref ref = e.getKey(); if(ref.tvals == null) { - ref.tvals = new TRef.TVal(e.getValue(), commitPoint, msecs); + ref.tvals = new Ref.TVal(e.getValue(), commitPoint, msecs); } else if(ref.faults.get() > 0) { - ref.tvals = new TRef.TVal(e.getValue(), commitPoint, msecs, ref.tvals); + ref.tvals = new Ref.TVal(e.getValue(), commitPoint, msecs, ref.tvals); ref.faults.set(0); } else @@ -301,7 +301,7 @@ public void enqueue(Agent.Action action){ actions.add(action); } -Object doGet(TRef ref){ +Object doGet(Ref ref){ if(!info.running()) throw retryex; if(vals.containsKey(ref)) @@ -311,7 +311,7 @@ Object doGet(TRef ref){ ref.lock.readLock().lock(); if(ref.tvals == null) throw new IllegalStateException(ref.toString() + " is unbound."); - TRef.TVal ver = ref.tvals; + Ref.TVal ver = ref.tvals; do { if(ver.point <= readPoint) @@ -328,7 +328,7 @@ Object doGet(TRef ref){ } -Object doSet(TRef ref, Object val){ +Object doSet(Ref ref, Object val){ if(!info.running()) throw retryex; if(commutes.containsKey(ref)) @@ -342,13 +342,13 @@ Object doSet(TRef ref, Object val){ return val; } -void doTouch(TRef ref){ +void doTouch(Ref ref){ if(!info.running()) throw retryex; lock(ref); } -Object doCommute(TRef ref, IFn fn, ISeq args) throws Exception{ +Object doCommute(Ref ref, IFn fn, ISeq args) throws Exception{ if(!info.running()) throw retryex; if(!vals.containsKey(ref)) diff --git a/src/jvm/clojure/lang/TRef.java b/src/jvm/clojure/lang/Ref.java index af1d0b1d..8b175c45 100644 --- a/src/jvm/clojure/lang/TRef.java +++ b/src/jvm/clojure/lang/Ref.java @@ -16,9 +16,9 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.UUID; -public class TRef implements IFn, Comparable<TRef>, IRef{ +public class Ref implements IFn, Comparable<Ref>, IRef{ -public int compareTo(TRef o){ +public int compareTo(Ref o){ return uuid.compareTo(o.uuid); } @@ -55,7 +55,7 @@ final ReentrantReadWriteLock lock; LockingTransaction.Info tinfo; final UUID uuid; -public TRef(){ +public Ref(){ this.tvals = null; this.tinfo = null; this.faults = new AtomicInteger(); @@ -63,7 +63,7 @@ public TRef(){ this.uuid = UUID.randomUUID(); } -public TRef(Object initVal){ +public Ref(Object initVal){ this(); tvals = new TVal(initVal, 0, System.currentTimeMillis()); } @@ -71,7 +71,7 @@ public TRef(Object initVal){ //note - makes no attempt to ensure there is no other Ref with same UUID //use only with a cache/registry -public TRef(UUID uuid, Object initVal){ +public Ref(UUID uuid, Object initVal){ tvals = new TVal(initVal, 0, System.currentTimeMillis()); this.tinfo = null; this.faults = new AtomicInteger(); |