Nice TWiki > Doc > TcbBenchExample ( vs. r1.1) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
 <<O>>  Difference Topic TcbBenchExample (r1.1 - 02 Sep 2003 - DuncanLissett)
Added:
>
>

%META:TOPICINFO{author="DuncanLissett" date="1062534476" format="1.0" version="1.1"}% %META:TOPICPARENT{name="CodeExamples"}%

package bench;

let Tcb NoTcb = new Tcb(link: NoTcb, id: Idle, pri: 0, wkq: NoPacket);

// Named bit masks to access 3 bit state value.
let int RUNNABLE = 1;
let int SUSPENDED = 2;
let int HELD = 4;
let int SUSPENDED_RUNNABLE = SUSPENDED | RUNNABLE;
let int NOT_RUNNABLE = ~RUNNABLE;
let int NOT_SUSPENDED = ~SUSPENDED;
let int NOT_HELD = ~HELD;


/**
 * Each simulated task has a task control block Tcb.
 */
class Tcb {

// initializer
   {   
      if (wkq != NoPacket) state = SUSPENDED_RUNNABLE;
   }

   // Variables from spec
   private Tcb link;     // pointer to another tcb
   private PacketId id;  // identifier (a small integer)
   private int pri;      // priority (a positive integer)
   private Packet wkq;   // list of Packets in the tasks work queue
   private int state = SUSPENDED;  // a 3 bit value giving the state
   private ?ISchedulerTask task = null;


   Tcb getLink() = link;

   PacketId getId() = id;

   int getPriority() = pri;


   Tcb checkPriorityAdd(Tcb task, Packet packet) {
      if (wkq == NoPacket ) {
         wkq = packet;
         this.setIsRunnable(true);
         if (pri > task.getPriority) { return this; }
      }
      else {
         wkq = packet.addTo(queue: wkq);
      }
      return task;
   }


   Tcb run(){
      Packet packet;
      if (this.getIsSuspendedRunnable()) {
         packet = wkq;
         wkq = packet.getLink;
         if (wkq == NoPacket )
            state = 0; //setRunning;
         else
            this.setRunnable;
      }
      else {
         packet = NoPacket;
      }
      return (notNull(task)).run(packet);
   }


   void initializeState() =
      if (wkq != NoPacket) state = SUSPENDED_RUNNABLE;

   void setRunning() = 
      state = 0;

   private void setRunnable() = 
      state = RUNNABLE;

   private boolean getIsRunnable() = 
      (state & RUNNABLE) != 0;

   private void setIsRunnable(boolean value) = 
      state = value ? state | RUNNABLE : state & NOT_RUNNABLE;

   boolean getIsSuspended() =
      (state & SUSPENDED) != 0;

   void setIsSuspended(boolean value) =
      state = value ? state | SUSPENDED : state & NOT_SUSPENDED;

   boolean getIsHeld() =
      (state & HELD) != 0;

   void setIsHeld(boolean value) =
      state = value ? state | HELD : state & NOT_HELD;

   boolean getIsHeldOrSuspended() = 
      (state & HELD) != 0 || (state == SUSPENDED);

   boolean getIsSuspendedRunnable() =
      state == SUSPENDED_RUNNABLE;
}
-- DuncanLissett - 02 Sep 2003

Topic TcbBenchExample . { View | Diffs | r1.1 | More }
Revision -
Revision r1.1 - 02 Sep 2003 - 20:27 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.