[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>
|
---|
| 10 | #include "pexceptions.h"
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 | namespace SOPHYA {
|
---|
| 14 |
|
---|
| 15 | typedef void (* ZThreadAction) (void *);
|
---|
[2212] | 16 |
|
---|
| 17 | /*!
|
---|
| 18 | \class SOPHYA::ZThreadExc
|
---|
| 19 | \ingroup SysTools
|
---|
| 20 | This exception class is used by the classes implementing the interface
|
---|
| 21 | to the POSIX threads (\b ZThread ...)
|
---|
| 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();
|
---|
| 34 | inline void stop() { cancel(); }
|
---|
| 35 |
|
---|
| 36 | void join();
|
---|
| 37 | virtual void run();
|
---|
| 38 |
|
---|
[2212] | 39 | //! Sets the return code for the thread object
|
---|
[1612] | 40 | inline void setRC(int rc) { _rc = rc; }
|
---|
[2212] | 41 | //! Return the value of the return code for the thread object
|
---|
[1612] | 42 | inline int getRC(int rc) { return(_rc);
|
---|
| 43 | }
|
---|
| 44 | inline void setAction(ZThreadAction act, void * usp=NULL)
|
---|
| 45 | { _act = act; _usp = usp; }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE)
|
---|
| 49 | { int ocs; pthread_setcancelstate(st, &ocs); }
|
---|
| 50 | // PTHREAD_CANCEL_ENABLE ou PTHREAD_CANCEL_DISABLE
|
---|
| 51 | static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED)
|
---|
| 52 | { int oct; pthread_setcanceltype(ct, &oct); }
|
---|
| 53 | // PTHREAD_CANCEL_DEFERRED ou PTHREAD_CANCEL_ASYNCHRONOUS
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | // ---- Attribute variables ----
|
---|
| 57 | pthread_t _thr;
|
---|
| 58 | size_t _ssize;
|
---|
| 59 | int _rc;
|
---|
| 60 | bool _initok;
|
---|
| 61 |
|
---|
| 62 | ZThreadAction _act;
|
---|
| 63 | void * _usp;
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | class ZMutex {
|
---|
| 68 | public:
|
---|
| 69 | explicit ZMutex();
|
---|
| 70 | virtual ~ZMutex();
|
---|
[2212] | 71 | //! Locks the mutex object
|
---|
[1612] | 72 | inline void lock()
|
---|
| 73 | { pthread_mutex_lock(_mutx); }
|
---|
[2212] | 74 | //! Unlocks the mutex object
|
---|
[1612] | 75 | inline void unlock()
|
---|
[2212] | 76 | { pthread_mutex_unlock(_mutx); }
|
---|
| 77 | //! Waits for a condition change
|
---|
[1612] | 78 | inline void wait()
|
---|
| 79 | { pthread_cond_wait(_condv, _mutx); }
|
---|
[2212] | 80 | //! Signal a condition change on the mutex object
|
---|
[1612] | 81 | inline void signal()
|
---|
| 82 | { pthread_cond_signal(_condv); }
|
---|
[2212] | 83 | //! Broadcasts a condition change on the mutex object
|
---|
[1612] | 84 | inline void broadcast()
|
---|
| 85 | { pthread_cond_broadcast(_condv); }
|
---|
| 86 |
|
---|
| 87 | // Attributes
|
---|
| 88 | pthread_mutex_t * _mutx;
|
---|
| 89 | pthread_cond_t * _condv;
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | class ZSync {
|
---|
| 94 | public:
|
---|
[2488] | 95 | explicit inline ZSync(ZMutex & mtx, int sigbr=0)
|
---|
| 96 | {_mtx = &mtx; _sigbr = sigbr; mtx.lock(); }
|
---|
| 97 | inline ~ZSync()
|
---|
| 98 | {
|
---|
| 99 | if (_mtx) {
|
---|
| 100 | if (_sigbr == 1) _mtx->signal();
|
---|
| 101 | else if (_sigbr == 2) _mtx->broadcast();
|
---|
[2496] | 102 | _mtx->unlock();
|
---|
[2488] | 103 | }
|
---|
| 104 | }
|
---|
[1612] | 105 |
|
---|
| 106 | private:
|
---|
| 107 | ZMutex * _mtx;
|
---|
[2488] | 108 | int _sigbr;
|
---|
| 109 | inline ZSync() {_mtx = NULL; _sigbr = 0; }
|
---|
[1612] | 110 |
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | } // namespace SOPHYA
|
---|
| 114 |
|
---|
| 115 | #endif
|
---|