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