home *** CD-ROM | disk | FTP | other *** search
-
- //
- // Thread Q stuff
- //
-
-
- //
- // ThreadQUnlocked should be used when the threadq is already
- // associated with a locked data structuire (such as a synchronization
- // object). We save having to test for locking.
- //
-
- class ThreadQUnlocked : public Oqueue {
- int tq_neededstate;
- int tq_length;
- public:
- inline ThreadQUnlocked(int neededstate, Thread *t = 0);
- inline ~ThreadQUnlocked();
- inline Thread* get();
- inline Thread* lookat();
- inline void append(Thread* t);
- inline void prepend(Thread* t);
- inline void remove(Thread* t);
- inline int length()
- { return tq_length; }
- virtual void print(ostream& = cout); // in threads.c
- };
-
- class ThreadQ : public Oqueue {
- int tq_neededstate; // sanity check
- int tq_length;
- Spinlock *tq_lock;
- inline void lock();
- inline void unlock();
- public:
- inline ThreadQ(int neededstate, Thread *t = 0);
- inline ~ThreadQ();
- inline Thread* get();
- inline Thread* lookat();
- inline void append(Thread* t);
- inline void prepend(Thread* t);
- inline void remove(Thread* t);
- inline int length()
- { return tq_length; }
- virtual void print(ostream& = cout); // in threads.c
- };
-
-
-
- //
- // Unlocked thread queues.
- //
-
- inline
- ThreadQUnlocked::ThreadQUnlocked(int neededstate, Thread *t = 0) :(t)
- {
- tq_neededstate = neededstate;
- tq_length = 0;
- }
-
-
- inline
- ThreadQUnlocked::~ThreadQUnlocked()
- {
- Thread *t;
- while (t = get()) {
- t->setstate(TS_DELETE);
- delete (Thread*)t;
- }
- }
-
-
- inline
- Thread*
- ThreadQUnlocked::get()
- {
- Thread* t = (Thread*)(Oqueue::get());
- if (t) tq_length--;
- if (t==0 || t->t_state&tq_neededstate)
- return t;
- else
- t->error("bad state - threadqunlocked get");
- }
-
- inline
- Thread*
- ThreadQUnlocked::lookat()
- {
- Thread* t = (Thread*)(Oqueue::lookat());
- if (t==0 || t->t_state&tq_neededstate)
- return t;
- else
- t->error("bad state - threadqunlocked lookat");
- }
-
-
- inline
- void
- ThreadQUnlocked::append(Thread* t)
- {
- if (t && t->t_state&tq_neededstate) {
- Oqueue::append(t);
- tq_length++;
-
- // if (t==0 || t->t_state&tq_neededstate) {
- // Oqueue::append(t);
- // if (t) tq_length++;
-
- } else
- t->error("Bad state - threadqunlocked append");
- }
-
-
-
- inline
- void
- ThreadQUnlocked::prepend(Thread* t)
- {
- if (t && t->t_state&tq_neededstate) {
- Oqueue::prepend(t);
- tq_length++;
-
- // if (t==0 || t->t_state&tq_neededstate) {
- // Oqueue::prepend(t);
- // tq_length++;
-
- } else
- t->error("Bad state - threadqunlocked prepend");
- }
-
-
- inline
- void
- ThreadQUnlocked::remove(Thread *t)
- {
- if (t && t->t_state&tq_neededstate) {
- Oqueue::remove(t);
- tq_length--;
-
- // if (t==0 || t->t_state&tq_neededstate) {
- // Oqueue::remove(t);
- // tq_length--;
-
- } else
- t->error("Bad state - threadqunlocked remove");
- }
-
-
- //
- // Locked threadq's
- //
-
- inline ThreadQ::ThreadQ(int neededstate, Thread *t = 0) : (t)
- {
- tq_lock = new Spinlock;
- tq_neededstate = neededstate;
- tq_length = 0;
- }
-
-
- //
- // Delete a threadQ and all of the threads sitting on it.
- // We do not bother to return the threads to the reclaimq.
- // (in fact, we don't even bother to invoke the threads potentially
- // virtual constructor becuase the derived thread class may have
- // already seen this thread destroyed. Memory management needs to be
- // separated from object cleanup.
- //
- inline ThreadQ::~ThreadQ()
- {
- Thread *t;
- while (t = get()) {
- t->setstate(TS_DELETE);
- delete (Thread*)t;
- }
- }
-
- inline
- void ThreadQ::lock()
- {
- register Spinlock *sp = tq_lock;
- sp->lock();
- }
-
- inline
- void ThreadQ::unlock()
- {
- register Spinlock *sp = tq_lock;
- sp->unlock();
- }
-
-
-
- inline
- Thread*
- ThreadQ::get()
- {
- lock();
- Thread* t = (Thread*)(Oqueue::get());
- if (t)
- tq_length--;
- unlock();
-
- if (t==0 || t->t_state&tq_neededstate)
- return t;
- else
- t->error("bad state - threadq get");
- }
-
- inline
- Thread*
- ThreadQ::lookat()
- {
- Thread* t = (Thread*)(Oqueue::lookat());
- if (t==0 || t->t_state&tq_neededstate)
- return t;
- else
- t->error("bad state - threadq lookat");
- }
-
-
- inline
- void
- ThreadQ::append(Thread* t)
- {
- if (t && t->t_state&tq_neededstate) {
- lock();
- Oqueue::append(t);
- tq_length++;
- unlock();
- } else
- t->error("Bad state - threadq append");
- }
-
-
-
- inline
- void
- ThreadQ::prepend(Thread* t)
- {
- if (t && t->t_state&tq_neededstate) {
- lock();
- Oqueue::prepend(t);
- tq_length++;
- unlock();
- } else
- t->error("Bad state - threadq prepend");
- }
-
- inline
- void
- ThreadQ::remove(Thread *t)
- {
- if (t && t->t_state&tq_neededstate) {
- lock();
- Oqueue::remove(t);
- tq_length--;
- unlock();
- } else
- t->error("Bad state - threadq remove");
- }
-
-
-
-
-
-
-
-