| 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 <signal.h>
|
|---|
| 11 | #include "pexceptions.h"
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | namespace SOPHYA {
|
|---|
| 15 |
|
|---|
| 16 | typedef void (* ZThreadAction) (void *);
|
|---|
| 17 |
|
|---|
| 18 | /*!
|
|---|
| 19 | \class ZThreadExc
|
|---|
| 20 | \ingroup SysTools
|
|---|
| 21 | \brief Exception class used by ZThread and ZMutex classes.
|
|---|
| 22 | */
|
|---|
| 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();
|
|---|
| 34 | void kill(int sig);
|
|---|
| 35 |
|
|---|
| 36 | void join();
|
|---|
| 37 | virtual void run();
|
|---|
| 38 |
|
|---|
| 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 |
|
|---|
| 48 | //! Sets the return code for the thread object
|
|---|
| 49 | inline void setRC(int rc) { _rc = rc; }
|
|---|
| 50 | //! Return the value of the return code for the thread object
|
|---|
| 51 | inline int getRC() { return(_rc);
|
|---|
| 52 | }
|
|---|
| 53 | //! Defines the function which is executed by the default run() method
|
|---|
| 54 | inline void setAction(ZThreadAction act, void * usp=NULL)
|
|---|
| 55 | { _act = act; _usp = usp; }
|
|---|
| 56 |
|
|---|
| 57 | //! Call this method to activate the stop/exit handler on signal \b sig
|
|---|
| 58 | static void ActivateExitOnSignal(int sig=SIGUSR1);
|
|---|
| 59 |
|
|---|
| 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 | */
|
|---|
| 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
|
|---|
| 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 | */
|
|---|
| 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 |
|
|---|
| 78 | virtual void run_p();
|
|---|
| 79 |
|
|---|
| 80 | // ---- Attribute variables ----
|
|---|
| 81 | pthread_t _thr;
|
|---|
| 82 | size_t _ssize;
|
|---|
| 83 | int _rc;
|
|---|
| 84 | int _status; // 0 : not started, 1: running, 2: ended, 3:stopped
|
|---|
| 85 |
|
|---|
| 86 | ZThreadAction _act;
|
|---|
| 87 | void * _usp;
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | class ZMutex {
|
|---|
| 92 | public:
|
|---|
| 93 | explicit ZMutex(bool fgd=false);
|
|---|
| 94 | virtual ~ZMutex();
|
|---|
| 95 | //! Locks the mutex object
|
|---|
| 96 | inline void lock()
|
|---|
| 97 | { pthread_mutex_lock(_mutx); }
|
|---|
| 98 | //! Unlocks the mutex object
|
|---|
| 99 | inline void unlock()
|
|---|
| 100 | { pthread_mutex_unlock(_mutx); }
|
|---|
| 101 | //! Waits for a condition change
|
|---|
| 102 | inline void wait()
|
|---|
| 103 | { pthread_cond_wait(_condv, _mutx); }
|
|---|
| 104 | //! Signal a condition change on the mutex object.
|
|---|
| 105 | inline void signal()
|
|---|
| 106 | { pthread_cond_signal(_condv); }
|
|---|
| 107 | //! Broadcasts a condition change on the mutex object
|
|---|
| 108 | inline void broadcast()
|
|---|
| 109 | { pthread_cond_broadcast(_condv); }
|
|---|
| 110 |
|
|---|
| 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 |
|
|---|
| 117 | // Attributes
|
|---|
| 118 | pthread_mutex_t * _mutx;
|
|---|
| 119 | pthread_cond_t * _condv;
|
|---|
| 120 | bool _fgd;
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | class ZSync {
|
|---|
| 125 | public:
|
|---|
| 126 | /*!
|
|---|
| 127 | Constructor. Locks the associated ZMutex.
|
|---|
| 128 |
|
|---|
| 129 | - <tt> sigbr==0 </tt> No calls to signal() or broadcast()
|
|---|
| 130 | in destructor
|
|---|
| 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 | */
|
|---|
| 136 | explicit inline ZSync(ZMutex & mtx, int sigbr=0)
|
|---|
| 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(); }
|
|---|
| 145 | inline ~ZSync()
|
|---|
| 146 | {
|
|---|
| 147 | if (_mtx) {
|
|---|
| 148 | if (_sigbr == 1) _mtx->signal();
|
|---|
| 149 | else if (_sigbr == 2) _mtx->broadcast();
|
|---|
| 150 | _mtx->unlock();
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | //! Calls the wait method on the associated ZMutex object
|
|---|
| 154 | inline void wait() { if (_mtx) _mtx->wait(); }
|
|---|
| 155 | //! To avoid warnings about unused variables
|
|---|
| 156 | inline int NOp() { return _sigbr; }
|
|---|
| 157 |
|
|---|
| 158 | private:
|
|---|
| 159 | ZMutex * _mtx;
|
|---|
| 160 | int _sigbr;
|
|---|
| 161 | inline ZSync() {_mtx = NULL; _sigbr = 0; }
|
|---|
| 162 |
|
|---|
| 163 | };
|
|---|
| 164 |
|
|---|
| 165 | } // namespace SOPHYA
|
|---|
| 166 |
|
|---|
| 167 | #endif
|
|---|