| [1612] | 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> | 
|---|
| [2955] | 10 | #include <signal.h> | 
|---|
| [1612] | 11 | #include "pexceptions.h" | 
|---|
|  | 12 |  | 
|---|
|  | 13 |  | 
|---|
|  | 14 | namespace SOPHYA { | 
|---|
|  | 15 |  | 
|---|
|  | 16 | typedef void (* ZThreadAction) (void *); | 
|---|
| [2212] | 17 |  | 
|---|
|  | 18 | /*! | 
|---|
| [2598] | 19 | \class ZThreadExc | 
|---|
| [2212] | 20 | \ingroup SysTools | 
|---|
| [2598] | 21 | \brief Exception class used by ZThread and ZMutex classes. | 
|---|
| [2212] | 22 | */ | 
|---|
| [1612] | 23 | class ZThreadExc : public PException { | 
|---|
|  | 24 | public: | 
|---|
|  | 25 | explicit ZThreadExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
|  | 26 | }; | 
|---|
|  | 27 |  | 
|---|
|  | 28 | class ZThread { | 
|---|
|  | 29 | public: | 
|---|
|  | 30 | explicit      ZThread(size_t stacksize=0); | 
|---|
|  | 31 | virtual       ~ZThread(); | 
|---|
|  | 32 | void          start(); | 
|---|
|  | 33 | void          cancel(); | 
|---|
| [2943] | 34 | void          kill(int sig); | 
|---|
| [1612] | 35 |  | 
|---|
|  | 36 | void          join(); | 
|---|
|  | 37 | virtual void  run(); | 
|---|
|  | 38 |  | 
|---|
| [2955] | 39 | //! Returns true if the thread has been created / started | 
|---|
|  | 40 | bool IfStarted() { return ((_status > 0) ? true : false); } | 
|---|
|  | 41 | //! Returns true if the thread is running | 
|---|
|  | 42 | bool IfRunning() { return ((_status == 1) ? true : false); } | 
|---|
|  | 43 | //! Returns true if the thread has ended normally | 
|---|
|  | 44 | bool IfEnded() { return ((_status == 2) ? true : false); } | 
|---|
|  | 45 | //! Returns true if the thread has been stopped (killed/canceled) | 
|---|
|  | 46 | bool IfStopped() { return ((_status == 3) ? true : false); } | 
|---|
|  | 47 |  | 
|---|
| [2212] | 48 | //! Sets the return code for the thread object | 
|---|
| [1612] | 49 | inline void   setRC(int rc) { _rc = rc; } | 
|---|
| [2212] | 50 | //! Return the value of the return code for the thread object | 
|---|
| [2671] | 51 | inline int    getRC() { return(_rc); | 
|---|
| [1612] | 52 | } | 
|---|
| [2955] | 53 | //! Defines the function which is executed by the default run() method | 
|---|
| [1612] | 54 | inline void   setAction(ZThreadAction act, void * usp=NULL) | 
|---|
|  | 55 | {  _act = act;  _usp = usp; } | 
|---|
|  | 56 |  | 
|---|
| [2955] | 57 | //! Call this method to activate the stop/exit handler on signal \b sig | 
|---|
|  | 58 | static void ActivateExitOnSignal(int sig=SIGUSR1); | 
|---|
| [1612] | 59 |  | 
|---|
| [2955] | 60 | /*! | 
|---|
|  | 61 | \brief Set/changes the current thread cancelability state | 
|---|
|  | 62 | see pthread_setcancelstate() | 
|---|
|  | 63 | \b st = PTHREAD_CANCEL_ENABLE  or PTHREAD_CANCEL_DISABLE | 
|---|
|  | 64 | */ | 
|---|
| [1612] | 65 | static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE) | 
|---|
|  | 66 | { int ocs; pthread_setcancelstate(st, &ocs); } | 
|---|
|  | 67 | // PTHREAD_CANCEL_ENABLE  ou PTHREAD_CANCEL_DISABLE | 
|---|
| [2955] | 68 |  | 
|---|
|  | 69 | /*! | 
|---|
|  | 70 | \brief Set/changes the current thread cancelability state | 
|---|
|  | 71 | see pthread_setcanceltype() | 
|---|
|  | 72 | \b ct = PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS | 
|---|
|  | 73 | */ | 
|---|
| [1612] | 74 | static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED) | 
|---|
|  | 75 | { int oct; pthread_setcanceltype(ct, &oct); } | 
|---|
|  | 76 | // PTHREAD_CANCEL_DEFERRED  ou  PTHREAD_CANCEL_ASYNCHRONOUS | 
|---|
|  | 77 |  | 
|---|
| [2955] | 78 | virtual void  run_p(); | 
|---|
| [1612] | 79 |  | 
|---|
|  | 80 | // ---- Attribute variables ---- | 
|---|
|  | 81 | pthread_t _thr; | 
|---|
|  | 82 | size_t _ssize; | 
|---|
|  | 83 | int _rc; | 
|---|
| [2955] | 84 | int _status; // 0 : not started, 1: running, 2: ended, 3:stopped | 
|---|
| [1612] | 85 |  | 
|---|
|  | 86 | ZThreadAction _act; | 
|---|
|  | 87 | void * _usp; | 
|---|
|  | 88 | }; | 
|---|
|  | 89 |  | 
|---|
|  | 90 |  | 
|---|
|  | 91 | class ZMutex { | 
|---|
|  | 92 | public: | 
|---|
| [3219] | 93 | explicit      ZMutex(bool fgd=false); | 
|---|
| [1612] | 94 | virtual       ~ZMutex(); | 
|---|
| [2212] | 95 | //! Locks the mutex object | 
|---|
| [1612] | 96 | inline void   lock() | 
|---|
|  | 97 | { pthread_mutex_lock(_mutx); } | 
|---|
| [2212] | 98 | //! Unlocks the mutex object | 
|---|
| [1612] | 99 | inline void   unlock() | 
|---|
| [2212] | 100 | { pthread_mutex_unlock(_mutx); } | 
|---|
|  | 101 | //! Waits for a condition change | 
|---|
| [1612] | 102 | inline void   wait() | 
|---|
|  | 103 | { pthread_cond_wait(_condv, _mutx); } | 
|---|
| [2759] | 104 | //! Signal a condition change on the mutex object. | 
|---|
| [1612] | 105 | inline void   signal() | 
|---|
|  | 106 | { pthread_cond_signal(_condv); } | 
|---|
| [2212] | 107 | //! Broadcasts a condition change on the mutex object | 
|---|
| [1612] | 108 | inline void   broadcast() | 
|---|
|  | 109 | { pthread_cond_broadcast(_condv); } | 
|---|
|  | 110 |  | 
|---|
| [3217] | 111 | protected: | 
|---|
|  | 112 | ZMutex(ZMutex const & a) | 
|---|
|  | 113 | { throw ForbiddenError("ZMutex::ZMutex(ZMutex const & ) forbidden"); } | 
|---|
|  | 114 | ZMutex& operator = (ZMutex const & a) | 
|---|
|  | 115 | { throw ForbiddenError("ZMutex:: operator = (ZMutex const & a) forbidden"); } | 
|---|
|  | 116 |  | 
|---|
| [1612] | 117 | // Attributes | 
|---|
| [3217] | 118 | pthread_mutex_t *  _mutx; | 
|---|
|  | 119 | pthread_cond_t *  _condv; | 
|---|
| [3219] | 120 | bool _fgd; | 
|---|
| [1612] | 121 | }; | 
|---|
|  | 122 |  | 
|---|
|  | 123 |  | 
|---|
|  | 124 | class ZSync { | 
|---|
|  | 125 | public: | 
|---|
| [2598] | 126 | /*! | 
|---|
|  | 127 | Constructor. Locks the associated ZMutex. | 
|---|
|  | 128 |  | 
|---|
| [2759] | 129 | - <tt> sigbr==0 </tt> No calls to signal() or broadcast() | 
|---|
|  | 130 | in destructor | 
|---|
| [2598] | 131 | - <tt> sigbr==1 </tt> destructor calls \c signal on | 
|---|
|  | 132 | the associated ZMutex | 
|---|
|  | 133 | - <tt> sigbr==2 </tt> destructor calls \c broadcast on | 
|---|
|  | 134 | the associated ZMutex | 
|---|
|  | 135 | */ | 
|---|
| [2488] | 136 | explicit inline  ZSync(ZMutex & mtx, int sigbr=0) | 
|---|
| [2759] | 137 | {_mtx = &mtx; _sigbr = sigbr; _mtx->lock(); } | 
|---|
|  | 138 |  | 
|---|
|  | 139 | /*! | 
|---|
|  | 140 | Constructor from ZMutex pointer. | 
|---|
|  | 141 | Locks the associated ZMutex object if non null pointer | 
|---|
|  | 142 | */ | 
|---|
|  | 143 | explicit inline  ZSync(ZMutex * mtxp, int sigbr=0) | 
|---|
|  | 144 | {_mtx = mtxp; _sigbr = sigbr; if (_mtx) _mtx->lock(); } | 
|---|
| [2488] | 145 | inline           ~ZSync() | 
|---|
|  | 146 | { | 
|---|
|  | 147 | if (_mtx) { | 
|---|
|  | 148 | if (_sigbr == 1) _mtx->signal(); | 
|---|
|  | 149 | else  if (_sigbr == 2) _mtx->broadcast(); | 
|---|
| [2496] | 150 | _mtx->unlock(); | 
|---|
| [2488] | 151 | } | 
|---|
|  | 152 | } | 
|---|
| [2759] | 153 | //! Calls the wait method on the associated ZMutex object | 
|---|
|  | 154 | inline void wait() { if (_mtx) _mtx->wait(); } | 
|---|
| [2753] | 155 | //! To avoid warnings about unused variables | 
|---|
|  | 156 | inline int NOp() { return _sigbr; } | 
|---|
| [1612] | 157 |  | 
|---|
|  | 158 | private: | 
|---|
|  | 159 | ZMutex * _mtx; | 
|---|
| [2488] | 160 | int _sigbr; | 
|---|
|  | 161 | inline        ZSync() {_mtx = NULL; _sigbr = 0; } | 
|---|
| [1612] | 162 |  | 
|---|
|  | 163 | }; | 
|---|
|  | 164 |  | 
|---|
|  | 165 | } // namespace SOPHYA | 
|---|
|  | 166 |  | 
|---|
|  | 167 | #endif | 
|---|