source: Sophya/trunk/SophyaLib/SysTools/zthread.h@ 2856

Last change on this file since 2856 was 2759, checked in by ansari, 20 years ago

Amelioration de l'interface ZSync() + ajout commentaires - Reza 24/05/2005

File size: 3.3 KB
RevLine 
[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
13namespace SOPHYA {
14
15typedef 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]22class ZThreadExc : public PException {
23public:
24 explicit ZThreadExc(const string& m, int id=0) : PException(m,id) {}
25};
26
27class ZThread {
28public:
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
66class ZMutex {
67public:
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); }
[2759]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
92class ZSync {
93public:
[2598]94/*!
95 Constructor. Locks the associated ZMutex.
96
[2759]97 - <tt> sigbr==0 </tt> No calls to signal() or broadcast()
98 in destructor
[2598]99 - <tt> sigbr==1 </tt> destructor calls \c signal on
100 the associated ZMutex
101 - <tt> sigbr==2 </tt> destructor calls \c broadcast on
102 the associated ZMutex
103*/
[2488]104 explicit inline ZSync(ZMutex & mtx, int sigbr=0)
[2759]105 {_mtx = &mtx; _sigbr = sigbr; _mtx->lock(); }
106
107/*!
108 Constructor from ZMutex pointer.
109 Locks the associated ZMutex object if non null pointer
110*/
111 explicit inline ZSync(ZMutex * mtxp, int sigbr=0)
112 {_mtx = mtxp; _sigbr = sigbr; if (_mtx) _mtx->lock(); }
[2488]113 inline ~ZSync()
114 {
115 if (_mtx) {
116 if (_sigbr == 1) _mtx->signal();
117 else if (_sigbr == 2) _mtx->broadcast();
[2496]118 _mtx->unlock();
[2488]119 }
120 }
[2759]121//! Calls the wait method on the associated ZMutex object
122 inline void wait() { if (_mtx) _mtx->wait(); }
[2753]123//! To avoid warnings about unused variables
124 inline int NOp() { return _sigbr; }
[1612]125
126private:
127 ZMutex * _mtx;
[2488]128 int _sigbr;
129 inline ZSync() {_mtx = NULL; _sigbr = 0; }
[1612]130
131};
132
133} // namespace SOPHYA
134
135#endif
Note: See TracBrowser for help on using the repository browser.