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