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