| 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
| 2 | // Interface C++ aux Thread POSIX  -   R. Ansari 02/2001 | 
|---|
| 3 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA | 
|---|
| 4 |  | 
|---|
| 5 | #ifndef ZTHREAD_SEEN | 
|---|
| 6 | #define ZTHREAD_SEEN | 
|---|
| 7 |  | 
|---|
| 8 | #include "machdefs.h" | 
|---|
| 9 | #include <pthread.h> | 
|---|
| 10 | #include "pexceptions.h" | 
|---|
| 11 |  | 
|---|
| 12 |  | 
|---|
| 13 | namespace SOPHYA { | 
|---|
| 14 |  | 
|---|
| 15 | typedef void (* ZThreadAction) (void *); | 
|---|
| 16 |  | 
|---|
| 17 | class ZThreadExc : public PException { | 
|---|
| 18 | public: | 
|---|
| 19 | explicit ZThreadExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 20 | }; | 
|---|
| 21 |  | 
|---|
| 22 | class ZThread { | 
|---|
| 23 | public: | 
|---|
| 24 | explicit      ZThread(size_t stacksize=0); | 
|---|
| 25 | virtual       ~ZThread(); | 
|---|
| 26 | void          start(); | 
|---|
| 27 | void          cancel(); | 
|---|
| 28 | inline void   stop() { cancel(); } | 
|---|
| 29 |  | 
|---|
| 30 | void          join(); | 
|---|
| 31 | virtual void  run(); | 
|---|
| 32 |  | 
|---|
| 33 | inline void   setRC(int rc) { _rc = rc; } | 
|---|
| 34 | inline int    getRC(int rc) { return(_rc); | 
|---|
| 35 | } | 
|---|
| 36 | inline void   setAction(ZThreadAction act, void * usp=NULL) | 
|---|
| 37 | {  _act = act;  _usp = usp; } | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE) | 
|---|
| 41 | { int ocs; pthread_setcancelstate(st, &ocs); } | 
|---|
| 42 | // PTHREAD_CANCEL_ENABLE  ou PTHREAD_CANCEL_DISABLE | 
|---|
| 43 | static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED) | 
|---|
| 44 | { int oct; pthread_setcanceltype(ct, &oct); } | 
|---|
| 45 | // PTHREAD_CANCEL_DEFERRED  ou  PTHREAD_CANCEL_ASYNCHRONOUS | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | // ---- Attribute variables ---- | 
|---|
| 49 | pthread_t _thr; | 
|---|
| 50 | size_t _ssize; | 
|---|
| 51 | int _rc; | 
|---|
| 52 | bool _initok; | 
|---|
| 53 |  | 
|---|
| 54 | ZThreadAction _act; | 
|---|
| 55 | void * _usp; | 
|---|
| 56 | }; | 
|---|
| 57 |  | 
|---|
| 58 |  | 
|---|
| 59 | class ZMutex { | 
|---|
| 60 | public: | 
|---|
| 61 | explicit      ZMutex(); | 
|---|
| 62 | virtual       ~ZMutex(); | 
|---|
| 63 | inline void   lock() | 
|---|
| 64 | { pthread_mutex_lock(_mutx); } | 
|---|
| 65 | inline void   unlock() | 
|---|
| 66 | { pthread_mutex_unlock(_mutx); } | 
|---|
| 67 | inline void   wait() | 
|---|
| 68 | { pthread_cond_wait(_condv, _mutx); } | 
|---|
| 69 | inline void   signal() | 
|---|
| 70 | { pthread_cond_signal(_condv); } | 
|---|
| 71 | inline void   broadcast() | 
|---|
| 72 | { pthread_cond_broadcast(_condv); } | 
|---|
| 73 |  | 
|---|
| 74 | // Attributes | 
|---|
| 75 | pthread_mutex_t * _mutx; | 
|---|
| 76 | pthread_cond_t * _condv; | 
|---|
| 77 | }; | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 | class ZSync { | 
|---|
| 81 | public: | 
|---|
| 82 | explicit inline  ZSync(ZMutex & mtx) {_mtx = &mtx; mtx.lock(); } | 
|---|
| 83 | inline           ~ZSync() { _mtx->unlock(); } | 
|---|
| 84 |  | 
|---|
| 85 | private: | 
|---|
| 86 | ZMutex * _mtx; | 
|---|
| 87 | inline        ZSync() {_mtx = NULL; } | 
|---|
| 88 |  | 
|---|
| 89 | }; | 
|---|
| 90 |  | 
|---|
| 91 | } // namespace SOPHYA | 
|---|
| 92 |  | 
|---|
| 93 | #endif | 
|---|