TWiki . Doc . SchedulerBenchExample

package bench;

let int MaxTasks = 6;

class Scheduler {
   private Tcb[] table = new Tcb[MaxTasks].fill(int index => NoTcb);
   private Tcb list = NoTcb;
   private Tcb currentTcb = NoTcb;
   private PacketId currentId = Idle;

   /**v1 & v2 are simulated machine registers
   *   They could be defined as global variables
   *   var Object v1; and used as v1()
   *
   *   Each Tcb function will load it's own datatypes into
   *   the registers so define them in the most general way.
   */

   public int queueCount = 0;
   public int holdCount = 0;


   void addIdleTask(PacketId id, int priority, Packet wkq, int count) {
      this.addRunningTask(
         id,
         priority,
         wkq,
         new IdleTask(scheduler: this, v1: 1, v2: count)
      );
   }


   void addWorkerTask(PacketId id, int priority, Packet wkq) {
      this.addTask(
         id,
         priority,
         wkq,
         new WorkerTask(scheduler: this, v1: HandlerA, v2: 0)
      );
   }


   void addHandlerTask(PacketId id, int priority, Packet wkq) {
      this.addTask(
         id,
         priority,
         wkq,
         new HandlerTask(scheduler: this, v1: NoPacket, v2: NoPacket)
      );
   }


   void addDeviceTask(PacketId id, int priority, Packet wkq) {
      this.addTask(
         id,
         priority,
         wkq,
         new DeviceTask(scheduler: this, v1: NoPacket)
      );
   }


   private void addTask(PacketId id, int priority, 
         Packet wkq, ISchedulerTask task) {
      currentTcb = new Tcb(link: list, id: id, pri: priority,
         wkq: wkq, task: task);
      list = currentTcb;
      table[id.hashCode] = currentTcb;
   }


   private void addRunningTask(PacketId id, int priority, 
         Packet wkq, ISchedulerTask task) {
      this.addTask(id, priority, wkq, task);
      currentTcb.setRunning;
   }


   void schedule(){
      currentTcb = list;
      while (currentTcb != NoTcb) {
         if (currentTcb.getIsHeldOrSuspended)
            currentTcb = currentTcb.getLink;
         else {
            currentId = currentTcb.getId;
            if (traceOn) trace( char(int('0') + currentId.hashCode + 1));

            let nextTcb = currentTcb.run;
            currentTcb = nextTcb;
         }
      }
   }


   private Tcb find(PacketId id) {
      try { return table[id.hashCode]; }
      catch (ArrayIndexOutOfBoundsException e){ return NoTcb; }
   }


   private Tcb queue(Packet packet) {
      let t = this.find(id: packet.getId);
      if (t == NoTcb) return t;
      queueCount++;
      packet.setLink(NoPacket);
      packet.setId(currentId);
      return t.checkPriorityAdd(currentTcb, packet);
   }


   private Tcb release(PacketId id){
      let t = this.find(id);
      if (t == NoTcb) return t;
      t.setIsHeld(false);
      if (t.getPriority > currentTcb.getPriority)
         return t;
      else
         return currentTcb;
   }


   private Tcb holdCurrent(){
      ++holdCount;
      currentTcb.setIsHeld(true);
      return currentTcb.getLink;
   }


   private Tcb suspendCurrent() {
      currentTcb.setIsSuspended(true);
      return currentTcb;
   }
}
-- DuncanLissett - 02 Sep 2003

----- Revision r1.1 - 02 Sep 2003 - 20:29 GMT - DuncanLissett
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.