| 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 | /*! | 
|---|
| 18 | \class ZThreadExc | 
|---|
| 19 | \ingroup SysTools | 
|---|
| 20 | \brief Exception class used by ZThread and ZMutex classes. | 
|---|
| 21 | */ | 
|---|
| 22 | class ZThreadExc : public PException { | 
|---|
| 23 | public: | 
|---|
| 24 | explicit ZThreadExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 25 | }; | 
|---|
| 26 |  | 
|---|
| 27 | class ZThread { | 
|---|
| 28 | public: | 
|---|
| 29 | explicit      ZThread(size_t stacksize=0); | 
|---|
| 30 | virtual       ~ZThread(); | 
|---|
| 31 | void          start(); | 
|---|
| 32 | void          cancel(); | 
|---|
| 33 | inline void   stop() { cancel(); } | 
|---|
| 34 |  | 
|---|
| 35 | void          join(); | 
|---|
| 36 | virtual void  run(); | 
|---|
| 37 |  | 
|---|
| 38 | //! Sets the return code for the thread object | 
|---|
| 39 | inline void   setRC(int rc) { _rc = rc; } | 
|---|
| 40 | //! Return the value of the return code for the thread object | 
|---|
| 41 | inline int    getRC() { return(_rc); | 
|---|
| 42 | } | 
|---|
| 43 | inline void   setAction(ZThreadAction act, void * usp=NULL) | 
|---|
| 44 | {  _act = act;  _usp = usp; } | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE) | 
|---|
| 48 | { int ocs; pthread_setcancelstate(st, &ocs); } | 
|---|
| 49 | // PTHREAD_CANCEL_ENABLE  ou PTHREAD_CANCEL_DISABLE | 
|---|
| 50 | static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED) | 
|---|
| 51 | { int oct; pthread_setcanceltype(ct, &oct); } | 
|---|
| 52 | // PTHREAD_CANCEL_DEFERRED  ou  PTHREAD_CANCEL_ASYNCHRONOUS | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | // ---- Attribute variables ---- | 
|---|
| 56 | pthread_t _thr; | 
|---|
| 57 | size_t _ssize; | 
|---|
| 58 | int _rc; | 
|---|
| 59 | bool _initok; | 
|---|
| 60 |  | 
|---|
| 61 | ZThreadAction _act; | 
|---|
| 62 | void * _usp; | 
|---|
| 63 | }; | 
|---|
| 64 |  | 
|---|
| 65 |  | 
|---|
| 66 | class ZMutex { | 
|---|
| 67 | public: | 
|---|
| 68 | explicit      ZMutex(); | 
|---|
| 69 | virtual       ~ZMutex(); | 
|---|
| 70 | //! Locks the mutex object | 
|---|
| 71 | inline void   lock() | 
|---|
| 72 | { pthread_mutex_lock(_mutx); } | 
|---|
| 73 | //! Unlocks the mutex object | 
|---|
| 74 | inline void   unlock() | 
|---|
| 75 | { pthread_mutex_unlock(_mutx); } | 
|---|
| 76 | //! Waits for a condition change | 
|---|
| 77 | inline void   wait() | 
|---|
| 78 | { pthread_cond_wait(_condv, _mutx); } | 
|---|
| 79 | //! Signal a condition change on the mutex object | 
|---|
| 80 | inline void   signal() | 
|---|
| 81 | { pthread_cond_signal(_condv); } | 
|---|
| 82 | //! Broadcasts a condition change on the mutex object | 
|---|
| 83 | inline void   broadcast() | 
|---|
| 84 | { pthread_cond_broadcast(_condv); } | 
|---|
| 85 |  | 
|---|
| 86 | // Attributes | 
|---|
| 87 | pthread_mutex_t * _mutx; | 
|---|
| 88 | pthread_cond_t * _condv; | 
|---|
| 89 | }; | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 | class ZSync { | 
|---|
| 93 | public: | 
|---|
| 94 | /*! | 
|---|
| 95 | Constructor. Locks the associated ZMutex. | 
|---|
| 96 |  | 
|---|
| 97 | - <tt> sigbr==1 </tt> destructor calls \c signal on | 
|---|
| 98 | the associated ZMutex | 
|---|
| 99 | - <tt> sigbr==2 </tt> destructor calls \c broadcast on | 
|---|
| 100 | the associated ZMutex | 
|---|
| 101 | */ | 
|---|
| 102 | explicit inline  ZSync(ZMutex & mtx, int sigbr=0) | 
|---|
| 103 | {_mtx = &mtx; _sigbr = sigbr; mtx.lock(); } | 
|---|
| 104 | inline           ~ZSync() | 
|---|
| 105 | { | 
|---|
| 106 | if (_mtx) { | 
|---|
| 107 | if (_sigbr == 1) _mtx->signal(); | 
|---|
| 108 | else  if (_sigbr == 2) _mtx->broadcast(); | 
|---|
| 109 | _mtx->unlock(); | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | private: | 
|---|
| 114 | ZMutex * _mtx; | 
|---|
| 115 | int _sigbr; | 
|---|
| 116 | inline        ZSync() {_mtx = NULL; _sigbr = 0; } | 
|---|
| 117 |  | 
|---|
| 118 | }; | 
|---|
| 119 |  | 
|---|
| 120 | } // namespace SOPHYA | 
|---|
| 121 |  | 
|---|
| 122 | #endif | 
|---|