home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_11 / 1011026a < prev    next >
Text File  |  1992-09-08  |  2KB  |  93 lines

  1. echo Extracting msm.h...
  2. cat <<WRAP_EOF >msm.h
  3. /* MSM.h, Copyright (C) 1992 by Jon Shemitz.
  4.  
  5.    Declarations and inline code for a MSM engine
  6.    and not much else -- this code has been slashed
  7.    to minimize magazine space:  Features and *error
  8.    checking* have been stripped out and made
  9.    "exercises for the reader".
  10.  
  11.    Code is inlined for convenience and to save
  12.    magazine space, not because of any analysis
  13.    of time-space tradeoffs.
  14. */
  15.  
  16. typedef unsigned int Word;
  17. typedef unsigned char Byte;
  18.  
  19. const
  20.   StackSize = 25,
  21.   NTasks    = 50;
  22.   // Must be <= 255, so a TaskNumber can be a Byte
  23.  
  24. typedef void (*Handler) (); // A pointer to a function
  25. typedef Handler States;
  26.     // Makes it easier to add token-threading
  27.  
  28. typedef Byte TaskToken;
  29. typedef Byte Priorities;
  30.  
  31. template <class DataType, int StackSize>
  32. class SimpleStack {
  33.       Word Top;
  34.       DataType Data[StackSize];
  35.  
  36.       public:
  37.       SimpleStack()            {Top = 0;}
  38.       void Push(DataType Item) {Data[Top++] = Item;}
  39.       DataType Pop()           {return Data[--Top];}
  40. };
  41.  
  42. class Tasks {
  43.       TaskToken    Number;
  44.       States       State;
  45.       Word       Waits;
  46.       Priorities   Priority;
  47.       SimpleStack <States, StackSize>
  48.                Stack;
  49.       public:
  50.         Tasks(Handler S, Word W, Priorities P);
  51.         ~Tasks();
  52.         void SetState(States S)  {State = S;}
  53.         States GetState()        {return State;}
  54.  
  55.         void PushState(States S) {Stack.Push(S);}
  56.         void PopState()
  57.       {SetState(Stack.Pop());} // `return'
  58.  
  59.         void Sleep(Word BitMap)
  60.           {Waits |= BitMap; Deactivate();}
  61.         void Wake(Word BitMap)
  62.           {Waits &= (~BitMap); Activate();}
  63.  
  64.         void SetPriority(Priorities P);
  65.         Priorities GetPriority() {return Priority;}
  66.  
  67.       private:
  68.         void Activate();
  69.     // Move to an active task list
  70.         void Deactivate();
  71.         // Remove from an active task list
  72. };
  73.  
  74. typedef Tasks * TaskPtr;
  75.  
  76. extern TaskPtr ThisTask; /* The current task
  77.  
  78.        Any handler may use this pointer to read
  79.        and write its task record.
  80. */
  81.  
  82.  
  83. // There's only 1 engine, so it shouldn't be an object
  84.  
  85. void MSM_Run();  // Run the 'engine'
  86.  
  87. void MSM_Stop(); // Handlers can call to stop engine
  88.  
  89. TaskPtr Task(TaskToken TT);
  90.  
  91.  
  92.  
  93.