[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:
|
---|
| 93 | explicit ZMutex();
|
---|
| 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 |
|
---|
| 111 | // Attributes
|
---|
| 112 | pthread_mutex_t * _mutx;
|
---|
| 113 | pthread_cond_t * _condv;
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | class ZSync {
|
---|
| 118 | public:
|
---|
[2598] | 119 | /*!
|
---|
| 120 | Constructor. Locks the associated ZMutex.
|
---|
| 121 |
|
---|
[2759] | 122 | - <tt> sigbr==0 </tt> No calls to signal() or broadcast()
|
---|
| 123 | in destructor
|
---|
[2598] | 124 | - <tt> sigbr==1 </tt> destructor calls \c signal on
|
---|
| 125 | the associated ZMutex
|
---|
| 126 | - <tt> sigbr==2 </tt> destructor calls \c broadcast on
|
---|
| 127 | the associated ZMutex
|
---|
| 128 | */
|
---|
[2488] | 129 | explicit inline ZSync(ZMutex & mtx, int sigbr=0)
|
---|
[2759] | 130 | {_mtx = &mtx; _sigbr = sigbr; _mtx->lock(); }
|
---|
| 131 |
|
---|
| 132 | /*!
|
---|
| 133 | Constructor from ZMutex pointer.
|
---|
| 134 | Locks the associated ZMutex object if non null pointer
|
---|
| 135 | */
|
---|
| 136 | explicit inline ZSync(ZMutex * mtxp, int sigbr=0)
|
---|
| 137 | {_mtx = mtxp; _sigbr = sigbr; if (_mtx) _mtx->lock(); }
|
---|
[2488] | 138 | inline ~ZSync()
|
---|
| 139 | {
|
---|
| 140 | if (_mtx) {
|
---|
| 141 | if (_sigbr == 1) _mtx->signal();
|
---|
| 142 | else if (_sigbr == 2) _mtx->broadcast();
|
---|
[2496] | 143 | _mtx->unlock();
|
---|
[2488] | 144 | }
|
---|
| 145 | }
|
---|
[2759] | 146 | //! Calls the wait method on the associated ZMutex object
|
---|
| 147 | inline void wait() { if (_mtx) _mtx->wait(); }
|
---|
[2753] | 148 | //! To avoid warnings about unused variables
|
---|
| 149 | inline int NOp() { return _sigbr; }
|
---|
[1612] | 150 |
|
---|
| 151 | private:
|
---|
| 152 | ZMutex * _mtx;
|
---|
[2488] | 153 | int _sigbr;
|
---|
| 154 | inline ZSync() {_mtx = NULL; _sigbr = 0; }
|
---|
[1612] | 155 |
|
---|
| 156 | };
|
---|
| 157 |
|
---|
| 158 | } // namespace SOPHYA
|
---|
| 159 |
|
---|
| 160 | #endif
|
---|