| [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 | /*!
 | 
|---|
| [2598] | 18 |   \class ZThreadExc
 | 
|---|
| [2212] | 19 |   \ingroup SysTools
 | 
|---|
| [2598] | 20 |   \brief Exception class used by ZThread and ZMutex classes.
 | 
|---|
| [2212] | 21 | */  
 | 
|---|
| [1612] | 22 | class ZThreadExc : public PException {
 | 
|---|
 | 23 | public:
 | 
|---|
 | 24 |   explicit ZThreadExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
 | 25 | };
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | class ZThread {
 | 
|---|
 | 28 | public:
 | 
|---|
 | 29 |   explicit      ZThread(size_t stacksize=0);
 | 
|---|
 | 30 |   virtual       ~ZThread();
 | 
|---|
 | 31 |   void          start();
 | 
|---|
 | 32 |   void          cancel();
 | 
|---|
 | 33 |   inline void   stop() { cancel(); }
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 |   void          join();
 | 
|---|
 | 36 |   virtual void  run();
 | 
|---|
 | 37 | 
 | 
|---|
| [2212] | 38 |   //! Sets the return code for the thread object
 | 
|---|
| [1612] | 39 |   inline void   setRC(int rc) { _rc = rc; }
 | 
|---|
| [2212] | 40 |   //! Return the value of the return code for the thread object
 | 
|---|
| [2671] | 41 |   inline int    getRC() { return(_rc);
 | 
|---|
| [1612] | 42 |  }
 | 
|---|
 | 43 |   inline void   setAction(ZThreadAction act, void * usp=NULL)
 | 
|---|
 | 44 |        {  _act = act;  _usp = usp; }
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 |   static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE) 
 | 
|---|
 | 48 |        { int ocs; pthread_setcancelstate(st, &ocs); }
 | 
|---|
 | 49 |   // PTHREAD_CANCEL_ENABLE  ou PTHREAD_CANCEL_DISABLE
 | 
|---|
 | 50 |   static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED) 
 | 
|---|
 | 51 |        { int oct; pthread_setcanceltype(ct, &oct); }
 | 
|---|
 | 52 |   // PTHREAD_CANCEL_DEFERRED  ou  PTHREAD_CANCEL_ASYNCHRONOUS
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |   // ---- Attribute variables ----
 | 
|---|
 | 56 |   pthread_t _thr;
 | 
|---|
 | 57 |   size_t _ssize;
 | 
|---|
 | 58 |   int _rc;
 | 
|---|
 | 59 |   bool _initok;
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |   ZThreadAction _act;
 | 
|---|
 | 62 |   void * _usp;
 | 
|---|
 | 63 | };
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 | class ZMutex {
 | 
|---|
 | 67 | public:
 | 
|---|
 | 68 |   explicit      ZMutex();
 | 
|---|
 | 69 |   virtual       ~ZMutex();
 | 
|---|
| [2212] | 70 |   //! Locks the mutex object
 | 
|---|
| [1612] | 71 |   inline void   lock()
 | 
|---|
 | 72 |     { pthread_mutex_lock(_mutx); } 
 | 
|---|
| [2212] | 73 |   //! Unlocks the mutex object
 | 
|---|
| [1612] | 74 |   inline void   unlock()
 | 
|---|
| [2212] | 75 |     { pthread_mutex_unlock(_mutx); }
 | 
|---|
 | 76 |   //! Waits for a condition change
 | 
|---|
| [1612] | 77 |   inline void   wait()
 | 
|---|
 | 78 |     { pthread_cond_wait(_condv, _mutx); }
 | 
|---|
| [2212] | 79 |   //! Signal a condition change on the mutex object
 | 
|---|
| [1612] | 80 |   inline void   signal()
 | 
|---|
 | 81 |   { pthread_cond_signal(_condv); }
 | 
|---|
| [2212] | 82 |   //! Broadcasts a condition change on the mutex object
 | 
|---|
| [1612] | 83 |   inline void   broadcast()
 | 
|---|
 | 84 |   { pthread_cond_broadcast(_condv); }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |   // Attributes
 | 
|---|
 | 87 |   pthread_mutex_t * _mutx;
 | 
|---|
 | 88 |   pthread_cond_t * _condv;
 | 
|---|
 | 89 | };
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 | class ZSync {
 | 
|---|
 | 93 | public:
 | 
|---|
| [2598] | 94 | /*!
 | 
|---|
 | 95 |   Constructor. Locks the associated ZMutex. 
 | 
|---|
 | 96 |   
 | 
|---|
 | 97 |   - <tt> sigbr==1 </tt> destructor calls \c signal on 
 | 
|---|
 | 98 |   the associated ZMutex 
 | 
|---|
 | 99 |   - <tt> sigbr==2 </tt> destructor calls \c broadcast on 
 | 
|---|
 | 100 |   the associated ZMutex 
 | 
|---|
 | 101 | */
 | 
|---|
| [2488] | 102 |   explicit inline  ZSync(ZMutex & mtx, int sigbr=0) 
 | 
|---|
 | 103 |   {_mtx = &mtx; _sigbr = sigbr; mtx.lock(); }
 | 
|---|
 | 104 |   inline           ~ZSync() 
 | 
|---|
 | 105 |   { 
 | 
|---|
 | 106 |     if (_mtx) { 
 | 
|---|
 | 107 |       if (_sigbr == 1) _mtx->signal(); 
 | 
|---|
 | 108 |       else  if (_sigbr == 2) _mtx->broadcast(); 
 | 
|---|
| [2496] | 109 |       _mtx->unlock(); 
 | 
|---|
| [2488] | 110 |     }
 | 
|---|
 | 111 |   }
 | 
|---|
| [1612] | 112 | 
 | 
|---|
 | 113 | private:
 | 
|---|
 | 114 |   ZMutex * _mtx;
 | 
|---|
| [2488] | 115 |   int _sigbr;
 | 
|---|
 | 116 |   inline        ZSync() {_mtx = NULL; _sigbr = 0; }
 | 
|---|
| [1612] | 117 | 
 | 
|---|
 | 118 | };
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 | } // namespace SOPHYA
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | #endif
 | 
|---|