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

Last change on this file since 3664 was 3586, checked in by ansari, 17 years ago

Adaptation suite modif exceptions SOPHYA heritant de std::exception , Reza 05/03/2009

File size: 4.7 KB
Line 
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
14namespace SOPHYA {
15
16typedef void (* ZThreadAction) (void *);
17
18/*!
19 \class ZThreadExc
20 \ingroup SysTools
21 \brief Exception class used by ZThread and ZMutex classes.
22*/
23class ZThreadExc : public PException {
24public:
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) {}
27};
28
29class ZThread {
30public:
31 explicit ZThread(size_t stacksize=0);
32 virtual ~ZThread();
33 void start();
34 void cancel();
35 void kill(int sig);
36
37 void join();
38 virtual void run();
39
40 //! Returns true if the thread has been created / started
41 bool IfStarted() { return ((_status > 0) ? true : false); }
42 //! Returns true if the thread is running
43 bool IfRunning() { return ((_status == 1) ? true : false); }
44 //! Returns true if the thread has ended normally
45 bool IfEnded() { return ((_status == 2) ? true : false); }
46 //! Returns true if the thread has been stopped (killed/canceled)
47 bool IfStopped() { return ((_status == 3) ? true : false); }
48
49 //! Sets the return code for the thread object
50 inline void setRC(int rc) { _rc = rc; }
51 //! Return the value of the return code for the thread object
52 inline int getRC() { return(_rc);
53 }
54 //! Defines the function which is executed by the default run() method
55 inline void setAction(ZThreadAction act, void * usp=NULL)
56 { _act = act; _usp = usp; }
57
58 //! Call this method to activate the stop/exit handler on signal \b sig
59 static void ActivateExitOnSignal(int sig=SIGUSR1);
60
61 /*!
62 \brief Set/changes the current thread cancelability state
63 see pthread_setcancelstate()
64 \b st = PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE
65 */
66 static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE)
67 { int ocs; pthread_setcancelstate(st, &ocs); }
68 // PTHREAD_CANCEL_ENABLE ou PTHREAD_CANCEL_DISABLE
69
70 /*!
71 \brief Set/changes the current thread cancelability state
72 see pthread_setcanceltype()
73 \b ct = PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS
74 */
75 static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED)
76 { int oct; pthread_setcanceltype(ct, &oct); }
77 // PTHREAD_CANCEL_DEFERRED ou PTHREAD_CANCEL_ASYNCHRONOUS
78
79 virtual void run_p();
80
81 // ---- Attribute variables ----
82 pthread_t _thr;
83 size_t _ssize;
84 int _rc;
85 int _status; // 0 : not started, 1: running, 2: ended, 3:stopped
86
87 ZThreadAction _act;
88 void * _usp;
89};
90
91
92class ZMutex {
93public:
94 explicit ZMutex(bool fgd=false);
95 virtual ~ZMutex();
96 //! Locks the mutex object
97 inline void lock()
98 { pthread_mutex_lock(_mutx); }
99 //! Unlocks the mutex object
100 inline void unlock()
101 { pthread_mutex_unlock(_mutx); }
102 //! Waits for a condition change
103 inline void wait()
104 { pthread_cond_wait(_condv, _mutx); }
105 //! Signal a condition change on the mutex object.
106 inline void signal()
107 { pthread_cond_signal(_condv); }
108 //! Broadcasts a condition change on the mutex object
109 inline void broadcast()
110 { pthread_cond_broadcast(_condv); }
111
112protected:
113 ZMutex(ZMutex const & a)
114 { throw ForbiddenError("ZMutex::ZMutex(ZMutex const & ) forbidden"); }
115 ZMutex& operator = (ZMutex const & a)
116 { throw ForbiddenError("ZMutex:: operator = (ZMutex const & a) forbidden"); }
117
118 // Attributes
119 pthread_mutex_t * _mutx;
120 pthread_cond_t * _condv;
121 bool _fgd;
122};
123
124
125class ZSync {
126public:
127/*!
128 Constructor. Locks the associated ZMutex.
129
130 - <tt> sigbr==0 </tt> No calls to signal() or broadcast()
131 in destructor
132 - <tt> sigbr==1 </tt> destructor calls \c signal on
133 the associated ZMutex
134 - <tt> sigbr==2 </tt> destructor calls \c broadcast on
135 the associated ZMutex
136*/
137 explicit inline ZSync(ZMutex & mtx, int sigbr=0)
138 {_mtx = &mtx; _sigbr = sigbr; _mtx->lock(); }
139
140/*!
141 Constructor from ZMutex pointer.
142 Locks the associated ZMutex object if non null pointer
143*/
144 explicit inline ZSync(ZMutex * mtxp, int sigbr=0)
145 {_mtx = mtxp; _sigbr = sigbr; if (_mtx) _mtx->lock(); }
146 inline ~ZSync()
147 {
148 if (_mtx) {
149 if (_sigbr == 1) _mtx->signal();
150 else if (_sigbr == 2) _mtx->broadcast();
151 _mtx->unlock();
152 }
153 }
154//! Calls the wait method on the associated ZMutex object
155 inline void wait() { if (_mtx) _mtx->wait(); }
156//! To avoid warnings about unused variables
157 inline int NOp() { return _sigbr; }
158
159private:
160 ZMutex * _mtx;
161 int _sigbr;
162 inline ZSync() {_mtx = NULL; _sigbr = 0; }
163
164};
165
166} // namespace SOPHYA
167
168#endif
Note: See TracBrowser for help on using the repository browser.