| 1 | // | 
|---|
| 2 | // thread.h | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 1997 Limit Point Systems, Inc. | 
|---|
| 5 | // | 
|---|
| 6 | // Author: Edward Seidl <seidl@janed.com> | 
|---|
| 7 | // Maintainer: LPS | 
|---|
| 8 | // | 
|---|
| 9 | // This file is part of the SC Toolkit. | 
|---|
| 10 | // | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 14 | // any later version. | 
|---|
| 15 | // | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 19 | // GNU Library General Public License for more details. | 
|---|
| 20 | // | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 24 | // | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
| 26 | // | 
|---|
| 27 |  | 
|---|
| 28 | #ifndef _util_group_thread_h | 
|---|
| 29 | #define _util_group_thread_h | 
|---|
| 30 |  | 
|---|
| 31 | #ifdef __GNUC__ | 
|---|
| 32 | #pragma interface | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include <util/class/class.h> | 
|---|
| 36 |  | 
|---|
| 37 | namespace sc { | 
|---|
| 38 |  | 
|---|
| 39 | /** The ThreadLock abstract class provides mutex locks to be used in | 
|---|
| 40 | conjunction with ThreadGrp's.  ThreadLock objects should be | 
|---|
| 41 | locked and unlocked with ThreadLockHolder objects to provide | 
|---|
| 42 | exception safety. | 
|---|
| 43 | */ | 
|---|
| 44 | class ThreadLock : public RefCount { | 
|---|
| 45 | public: | 
|---|
| 46 | ThreadLock(); | 
|---|
| 47 | virtual ~ThreadLock(); | 
|---|
| 48 |  | 
|---|
| 49 | /// Obtain the lock. | 
|---|
| 50 | virtual void lock() =0; | 
|---|
| 51 | /// Release the lock. | 
|---|
| 52 | virtual void unlock() =0; | 
|---|
| 53 | }; | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | /** Acquire a lock on creation and release it on destruction. | 
|---|
| 57 | This should be used to lock and unlock ThreadLock objects | 
|---|
| 58 | to provide exception safety. | 
|---|
| 59 | */ | 
|---|
| 60 | class ThreadLockHolder { | 
|---|
| 61 | Ref<ThreadLock> lock_; | 
|---|
| 62 | bool locked_; | 
|---|
| 63 | public: | 
|---|
| 64 | /// Acquires the lock. | 
|---|
| 65 | ThreadLockHolder(const Ref<ThreadLock> &l): lock_(l) { | 
|---|
| 66 | lock_->lock(); | 
|---|
| 67 | locked_ = true; | 
|---|
| 68 | } | 
|---|
| 69 | /// Release the lock before the DTOR is called, if it is still held. | 
|---|
| 70 | void unlock() { if (locked_) { lock_->unlock(); locked_ = false; } } | 
|---|
| 71 | /// Acquire the lock once more. | 
|---|
| 72 | void lock() { if (!locked_) { lock_->lock(); locked_ = true; } } | 
|---|
| 73 | /// Releases the lock if it is still held. | 
|---|
| 74 | ~ThreadLockHolder() { unlock(); } | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | /** The Thread abstract class defines an interface which must be | 
|---|
| 78 | implemented by classes wishing to be run as threads. */ | 
|---|
| 79 | class Thread { | 
|---|
| 80 | public: | 
|---|
| 81 | Thread(); | 
|---|
| 82 | virtual ~Thread(); | 
|---|
| 83 |  | 
|---|
| 84 | static void *run_Thread_run(void*thread); | 
|---|
| 85 |  | 
|---|
| 86 | /// This is called with the Thread is run from a ThreadGrp. | 
|---|
| 87 | virtual void run() =0; | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 | /** The ThreadGrp abstract class provides a means to manage separate | 
|---|
| 91 | threads of control. */ | 
|---|
| 92 | class ThreadGrp: public DescribedClass { | 
|---|
| 93 | protected: | 
|---|
| 94 | Thread** threads_; | 
|---|
| 95 | int nthread_; | 
|---|
| 96 |  | 
|---|
| 97 | public: | 
|---|
| 98 | ThreadGrp(); | 
|---|
| 99 | ThreadGrp(const Ref<KeyVal>&); | 
|---|
| 100 | ThreadGrp(const ThreadGrp&, int nthread = -1); | 
|---|
| 101 | virtual ~ThreadGrp(); | 
|---|
| 102 |  | 
|---|
| 103 | /** Assigns a Thread object to each thread.  If 0 is assigned to | 
|---|
| 104 | a thread, then that thread will be skipped. */ | 
|---|
| 105 | virtual void add_thread(int threadnum, Thread* thread); | 
|---|
| 106 | /** Like add_thread(threadnum, thread), but assign a priority that the | 
|---|
| 107 | thread is to use.  The member is primarily for experimentation, the | 
|---|
| 108 | priority argument is currently not well defined and ignored.  */ | 
|---|
| 109 | virtual void add_thread(int threadnum, Thread* thread, int priority); | 
|---|
| 110 | /// The number of threads that will be run by start_thread. | 
|---|
| 111 | int nthread() const { return nthread_; } | 
|---|
| 112 |  | 
|---|
| 113 | void delete_threads(); | 
|---|
| 114 |  | 
|---|
| 115 | /** Starts the threads running.  Thread 0 will be run by the | 
|---|
| 116 | thread that calls start_threads. */ | 
|---|
| 117 | virtual int start_threads() =0; | 
|---|
| 118 | /** Wait for all the threads to complete.  This must be called | 
|---|
| 119 | before start_threads is called again or the object is destroyed. */ | 
|---|
| 120 | virtual int wait_threads() =0; | 
|---|
| 121 | /// Return a local object. | 
|---|
| 122 | virtual Ref<ThreadLock> new_lock() =0; | 
|---|
| 123 |  | 
|---|
| 124 | /** Create a ThreadGrp like the current one.  If nthread is given, the | 
|---|
| 125 | new ThreadGrp will attempt to support that number of threads, but | 
|---|
| 126 | the actual number supported may be less.  If nthread is -1, the | 
|---|
| 127 | number of threads in the current group will be used. */ | 
|---|
| 128 | virtual ThreadGrp* clone(int nthread = -1); | 
|---|
| 129 |  | 
|---|
| 130 | static void set_default_threadgrp(const Ref<ThreadGrp>&); | 
|---|
| 131 | static ThreadGrp * get_default_threadgrp(); | 
|---|
| 132 | static ThreadGrp * initial_threadgrp(int &argc, char ** argv); | 
|---|
| 133 | }; | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 | /** The ProcThreadGrp class privides a concrete thread group | 
|---|
| 137 | appropriate for an environment where there is only one thread. | 
|---|
| 138 | */ | 
|---|
| 139 | class ProcThreadGrp: public ThreadGrp { | 
|---|
| 140 | public: | 
|---|
| 141 | ProcThreadGrp(); | 
|---|
| 142 | ProcThreadGrp(const Ref<KeyVal>&); | 
|---|
| 143 | ~ProcThreadGrp(); | 
|---|
| 144 |  | 
|---|
| 145 | int start_threads(); | 
|---|
| 146 | int wait_threads(); | 
|---|
| 147 |  | 
|---|
| 148 | Ref<ThreadLock> new_lock(); | 
|---|
| 149 |  | 
|---|
| 150 | ThreadGrp* clone(int nthread = -1); | 
|---|
| 151 | }; | 
|---|
| 152 |  | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | extern "C" { | 
|---|
| 156 | // a C linkage interface to run_Thread_run | 
|---|
| 157 | void *Thread__run_Thread_run(void*thread); | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | #endif | 
|---|
| 161 |  | 
|---|
| 162 | // Local Variables: | 
|---|
| 163 | // mode: c++ | 
|---|
| 164 | // c-file-style: "ETS" | 
|---|
| 165 | // End: | 
|---|