home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / quartz / quartz10.lha / src / presto / threadq.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-02  |  4.8 KB  |  269 lines

  1.  
  2. //
  3. // Thread Q stuff
  4. //
  5.  
  6.  
  7. //
  8. // ThreadQUnlocked should be used when the threadq is already 
  9. // associated with a locked data structuire (such as a synchronization
  10. // object).  We save having to test for locking.
  11. //
  12.  
  13. class ThreadQUnlocked    : public Oqueue    {
  14.     int    tq_neededstate;
  15.         int     tq_length;
  16. public:
  17.         inline ThreadQUnlocked(int neededstate, Thread *t = 0);
  18.         inline ~ThreadQUnlocked();
  19.     inline Thread* get();
  20.     inline Thread*     lookat();
  21.     inline void        append(Thread* t);
  22.     inline void    prepend(Thread* t);
  23.     inline void     remove(Thread* t);
  24.         inline int      length()
  25.                                 { return tq_length; }
  26.     virtual void print(ostream& = cout);    // in threads.c
  27. };
  28.  
  29. class ThreadQ    : public Oqueue    {
  30.     int        tq_neededstate;        // sanity check
  31.     int        tq_length;
  32.     Spinlock     *tq_lock;
  33.     inline        void lock();
  34.     inline        void unlock();
  35. public:
  36.         inline ThreadQ(int neededstate, Thread *t = 0);
  37.         inline ~ThreadQ();
  38.     inline Thread*    get();
  39.     inline Thread*     lookat();
  40.     inline void        append(Thread* t);
  41.     inline void    prepend(Thread* t);
  42.     inline void     remove(Thread* t);
  43.     inline int    length()
  44.                 { return tq_length; }
  45.     virtual void print(ostream& = cout);    // in threads.c
  46. };
  47.  
  48.  
  49.  
  50. //
  51. //  Unlocked thread queues.
  52. //
  53.  
  54. inline
  55. ThreadQUnlocked::ThreadQUnlocked(int neededstate, Thread *t = 0)        :(t)
  56. {
  57.         tq_neededstate = neededstate;
  58.         tq_length = 0;
  59. }
  60.  
  61.  
  62. inline
  63. ThreadQUnlocked::~ThreadQUnlocked()
  64. {
  65.         Thread *t;
  66.         while (t = get())       {
  67.                 t->setstate(TS_DELETE);
  68.                 delete (Thread*)t;
  69.         }
  70. }
  71.  
  72.  
  73. inline
  74. Thread*
  75. ThreadQUnlocked::get()
  76. {  
  77.     Thread* t = (Thread*)(Oqueue::get());
  78.         if (t) tq_length--;
  79.     if (t==0 || t->t_state&tq_neededstate) 
  80.         return t;
  81.     else
  82.                 t->error("bad state - threadqunlocked get");
  83. }
  84.  
  85. inline
  86. Thread*
  87. ThreadQUnlocked::lookat()
  88. {
  89.     Thread* t = (Thread*)(Oqueue::lookat());
  90.     if (t==0 || t->t_state&tq_neededstate)
  91.            return t;
  92.     else
  93.                 t->error("bad state - threadqunlocked lookat");
  94. }
  95.  
  96.  
  97. inline
  98. void
  99. ThreadQUnlocked::append(Thread* t)
  100. {
  101.     if (t && t->t_state&tq_neededstate)    {
  102.         Oqueue::append(t); 
  103.         tq_length++;
  104.  
  105. //    if (t==0 || t->t_state&tq_neededstate)    {
  106. //        Oqueue::append(t); 
  107. //                if (t) tq_length++;
  108.  
  109.     } else
  110.                 t->error("Bad state - threadqunlocked append");
  111. }
  112.  
  113.  
  114.  
  115. inline
  116. void
  117. ThreadQUnlocked::prepend(Thread* t)
  118. {
  119.     if (t && t->t_state&tq_neededstate)    {
  120.         Oqueue::prepend(t);
  121.         tq_length++; 
  122.  
  123. //    if (t==0 || t->t_state&tq_neededstate)    {
  124. //        Oqueue::prepend(t); 
  125. //              tq_length++;
  126.  
  127.     }  else
  128.                 t->error("Bad state - threadqunlocked prepend");
  129. }
  130.  
  131.  
  132. inline
  133. void
  134. ThreadQUnlocked::remove(Thread *t)
  135.     if (t && t->t_state&tq_neededstate)    {
  136.         Oqueue::remove(t); 
  137.         tq_length--;
  138.  
  139. //    if (t==0 || t->t_state&tq_neededstate)    {
  140. //        Oqueue::remove(t); 
  141. //              tq_length--;
  142.  
  143.     } else
  144.                 t->error("Bad state - threadqunlocked remove");
  145. }
  146.  
  147.  
  148. //
  149. // Locked threadq's
  150. //
  151.  
  152. inline ThreadQ::ThreadQ(int neededstate, Thread *t = 0) : (t)
  153. {
  154.         tq_lock = new Spinlock;
  155.         tq_neededstate = neededstate;
  156.         tq_length = 0;
  157. }
  158.  
  159.  
  160. //
  161. // Delete a threadQ and all of the threads sitting on it.
  162. // We do not bother to return the threads to the reclaimq.
  163. // (in fact, we don't even bother to invoke the threads potentially
  164. // virtual constructor becuase the derived thread class may have
  165. // already seen this thread destroyed.  Memory management needs to be
  166. // separated from object cleanup.
  167. //
  168. inline ThreadQ::~ThreadQ()
  169. {
  170.         Thread *t;
  171.         while (t = get())       {
  172.                 t->setstate(TS_DELETE);
  173.                 delete (Thread*)t;
  174.         }
  175. }
  176.  
  177. inline
  178. void ThreadQ::lock()
  179. {
  180.     register Spinlock *sp = tq_lock; 
  181.     sp->lock(); 
  182. }
  183.  
  184. inline
  185. void ThreadQ::unlock()
  186. {
  187.     register Spinlock *sp = tq_lock; 
  188.     sp->unlock(); 
  189. }
  190.  
  191.  
  192.  
  193. inline
  194. Thread*
  195. ThreadQ::get()
  196. {  
  197.     lock();
  198.     Thread* t = (Thread*)(Oqueue::get());
  199.     if (t)
  200.         tq_length--;
  201.     unlock();
  202.     
  203.     if (t==0 || t->t_state&tq_neededstate) 
  204.         return t;
  205.     else
  206.                 t->error("bad state - threadq get");
  207. }
  208.             
  209. inline
  210. Thread*
  211. ThreadQ::lookat()
  212. {
  213.     Thread* t = (Thread*)(Oqueue::lookat());
  214.     if (t==0 || t->t_state&tq_neededstate)
  215.            return t;
  216.     else
  217.                 t->error("bad state - threadq lookat");
  218. }
  219.     
  220.  
  221. inline
  222. void
  223. ThreadQ::append(Thread* t)
  224. {
  225.     if (t && t->t_state&tq_neededstate)    {
  226.         lock();
  227.         Oqueue::append(t); 
  228.         tq_length++;
  229.         unlock();
  230.     } else
  231.                 t->error("Bad state - threadq append");
  232. }
  233.  
  234.  
  235.  
  236. inline
  237. void
  238. ThreadQ::prepend(Thread* t)
  239. {
  240.     if (t && t->t_state&tq_neededstate)    {
  241.         lock();
  242.         Oqueue::prepend(t);
  243.         tq_length++; 
  244.         unlock();
  245.     }  else
  246.                 t->error("Bad state - threadq prepend");
  247. }
  248.  
  249. inline
  250. void
  251. ThreadQ::remove(Thread *t)
  252.     if (t && t->t_state&tq_neededstate)    {
  253.         lock();
  254.         Oqueue::remove(t); 
  255.         tq_length--;
  256.         unlock();
  257.     } else
  258.                 t->error("Bad state - threadq remove");
  259. }
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.