Preemption Next: Event Presence Test Up: Event Based Instructions Previous: Waiting for Events

Preemption

Until  extends BinaryInstruction and implements preemption: execution of the left instruction, called the ``body'', is aborted when an event configuration becomes true. One says then that left is ``preempted'' by the configuration and, in this case, control goes to right which is called the ``handler''.

The preemption implemented by Until is ``weak'': left is not prevented to react at the very instant of preemption.

 
public class Until extends BinaryInstruction
{
  private Config config;
  private boolean activeHandle = false;
  private boolean resumeBody = true;

  public Until(Config config,Instruction body,
                             Instruction handler){ 
    this.config = config; left = body; right = handler;
  }

  public Until(Config config,Instruction body){ 
    this.config = config; left = body; right = new Nothing();
  }

  public void reset()
  {
    super.reset();
    activeHandle = false;
    resumeBody = true;
  }

  final public boolean equals(Instruction inst){
    return  super.equals(inst) 
           && config.equals(((Until)inst).config);
  }

  final public String toString(){ 
    if (right instanceof Nothing) 
        return "do "+left+" until "+config;
    return "do "+left+" until "+config+" actual "+right+" end"; 
  }

  final protected byte activation(Machine machine) 
  { 
    if (activeHandle) return right.activ(machine);
    if (resumeBody){ // body is to be executed
      byte res = left.activ(machine); // weak preemption !
      if (res != STOP) return res;
      resumeBody = false; 
    }
    if (!config.fixed(machine)) return SUSP; 
    if (config.evaluate(machine)){ // actual preemption
      activeHandle = true;
      if (machine.isEndOfInstant()) return STOP; 
      return right.activ(machine);
    }
    resumeBody = true; // to re-execute the body at next instant
    return STOP;
  }
}