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

Last change on this file since 2955 was 2955, checked in by ansari, 19 years ago

Ajout gestionnaire signal ds ZThread et correction/adaptation gestion des threads (killthr) dans Commander - Reza 29 mai 2006

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