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, exec); dispatchAction(action); return this; }
static void dispatchAction(Action action){ LockingTransaction trans = LockingTransaction.getRunning(); if(trans != null) trans.enqueue(action); else if(nested.get() != null) { nested.set(nested.get().cons(action)); } else action.agent.enqueue(action); }
public Agent(Object state, IPersistentMap meta) { super(meta); setState(state); }
synchronized public Object restart(Object newState, boolean clearActions){ if(getError() == null) { throw Util.runtimeException("Agent does not need a restart"); } validate(newState); state = newState; if(clearActions) aq.set(ActionQueue.EMPTY); else { boolean restarted = false; ActionQueue prior = null; while(!restarted) { prior = aq.get(); restarted = aq.compareAndSet(prior, new ActionQueue(prior.q, null)); } if(prior.q.count() > 0) ((Action) prior.q.peek()).execute(); } return newState; }
action.agent.setState(newval); action.agent.notifyWatches(oldval,newval); releasePendingSends();
boolean setState(Object newState) { validate(newState); boolean ret = state != newState; state = newState; return ret; }
Agent.dispatchAction(action);
action.agent.setState(newval); action.agent.notifyWatches(oldval,newval); releasePendingSends();
synchronized public Object restart(Object newState, boolean clearActions){ if(getError() == null) { throw Util.runtimeException("Agent does not need a restart"); } validate(newState); state = newState; if(clearActions) aq.set(ActionQueue.EMPTY); else { boolean restarted = false; ActionQueue prior = null; while(!restarted) { prior = aq.get(); restarted = aq.compareAndSet(prior, new ActionQueue(prior.q, null)); } if(prior.q.count() > 0) ((Action) prior.q.peek()).execute(); } return newState; }
boolean setState(Object newState) { validate(newState); boolean ret = state != newState; state = newState; return ret; }
Agent.dispatchAction(action);
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, exec); dispatchAction(action); return this; }
static public int releasePendingSends(){ IPersistentVector sends = nested.get(); if(sends == null) return 0; for(int i=0;i<sends.count();i++) { Action a = (Action) sends.valAt(i); a.agent.enqueue(a); } nested.set(PersistentVector.EMPTY); return sends.count(); } }
public Agent(Object state, IPersistentMap meta) { super(meta); setState(state); }
static void dispatchAction(Action action){ LockingTransaction trans = LockingTransaction.getRunning(); if(trans != null) trans.enqueue(action); else if(nested.get() != null) { nested.set(nested.get().cons(action)); } else action.agent.enqueue(action); }
static public int releasePendingSends(){ IPersistentVector sends = nested.get(); if(sends == null) return 0; for(int i=0;i<sends.count();i++) { Action a = (Action) sends.valAt(i); a.agent.enqueue(a); } nested.set(PersistentVector.EMPTY); return sends.count(); } }