00001 #ifndef MTMGR_HEADER
00002 #define MTMGR_HEADER
00003
00004
00005 #if defined(__linux) || defined(__alpha)
00006 #ifndef _REENTRANT
00007 #error Macro _REENTRANT must be defined for POSIX threads!
00008 #endif
00009 #endif
00010
00011 #ifdef __sgi
00012 #if !defined(_POSIX_C_SOURCE) || !defined(_PTHREADS)
00013 #error Macros _POSIX_C_SOURCE=199506L and _PTHREADS are needed for POSIX threads under IRIX!
00014 #endif
00015 #endif
00016
00017
00018
00019
00025
00026
00027
00028
00029 #include <pthread.h>
00030 #include <stdlib.h>
00031 #include <errno.h>
00032
00033 #ifdef _STANDARD_C_PLUS_PLUS
00034 #include <list>
00035 using namespace std;
00036 #else
00037 #include <list.h>
00038 #endif
00039
00040
00041
00042 #include "Utilsexc.hh"
00043
00044
00045
00046 namespace RazorBack {
00047
00052 typedef void *(*Threadstartfn_)(void *);
00053
00054
00055
00062 class MTmgrbase_
00063 {
00064
00065
00066 protected:
00067
00068 list<pthread_t> Threads;
00069
00071 public:
00072
00074 MTmgrbase_(): Threads() {}
00075
00077 virtual ~MTmgrbase_() {}
00078
00080
00087 virtual size_t launch(size_t Threadno, Threadstartfn_ Startfn, void *Arg) =0;
00088
00094 virtual void reap() =0;
00095 virtual void reap(pthread_t ID) =0;
00096
00098
00103 static size_t get_cpuno();
00104
00106 bool empty() const { return Threads.empty(); }
00107
00109 size_t thread_no() const { return Threads.size(); }
00110
00115 bool is_running(pthread_t ID) const;
00116
00121 const list<pthread_t>& run_list() const { return Threads; }
00122
00123 private:
00124 MTmgrbase_(const MTmgrbase_&);
00125 MTmgrbase_& operator=(const MTmgrbase_&);
00126 };
00127
00128
00135 class MTmgrjoin_: public MTmgrbase_
00136 {
00137
00138 public:
00139
00141 MTmgrjoin_(): MTmgrbase_() {}
00142
00144 virtual ~MTmgrjoin_() { reap(); }
00145
00153 virtual size_t launch(size_t Threadno, Threadstartfn_ Startfn, void *Arg);
00154
00163 virtual void reap();
00164
00170 virtual void reap(pthread_t ID);
00171
00172 private:
00173 MTmgrjoin_(const MTmgrjoin_&);
00174 MTmgrjoin_& operator=(const MTmgrjoin_&);
00175 };
00176
00177
00184 class MTmgrdetach_: public MTmgrbase_
00185 {
00186
00187 private:
00188
00189 pthread_attr_t Attr;
00190
00191
00192 public:
00193
00195 MTmgrdetach_(): MTmgrbase_()
00196 {
00197 pthread_attr_init(&Attr);
00198 pthread_attr_setdetachstate(&Attr, PTHREAD_CREATE_DETACHED);
00199 }
00200
00202 virtual ~MTmgrdetach_() { reap(); pthread_attr_destroy(&Attr); }
00203
00212 size_t launch(size_t Threadno, Threadstartfn_ Startfn, void *Arg);
00213
00223 void reap();
00224
00233 void reap(pthread_t ID);
00234
00235 private:
00236 MTmgrdetach_(const MTmgrdetach_&);
00237 MTmgrdetach_& operator=(const MTmgrdetach_&);
00238 };
00239
00240
00246 class MTmgrexc_: public Utilsexc_
00247 {
00248
00249 private:
00250
00251 int Errno;
00252 int Threadno;
00253
00254
00255 public:
00256
00263 explicit MTmgrexc_(const char *Thnm, int Tno, int Eno, bool F=false);
00264
00266 int thread_idx() const { return Threadno; }
00267
00269 int err_no() const { return Errno; }
00270
00271 private:
00272 MTmgrexc_();
00273 };
00274
00275
00276 }
00277
00278
00279
00280 #endif // MTMGR_HEADER