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

Last change on this file since 2483 was 2212, checked in by ansari, 23 years ago

Mise a jour de la documentation SysTools, Reza 15/10/2002

File size: 2.5 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 "pexceptions.h"
11
12
13namespace SOPHYA {
14
15typedef void (* ZThreadAction) (void *);
16
17/*!
18 \class SOPHYA::ZThreadExc
19 \ingroup SysTools
20 This exception class is used by the classes implementing the interface
21 to the POSIX threads (\b ZThread ...)
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 inline void stop() { cancel(); }
35
36 void join();
37 virtual void run();
38
39 //! Sets the return code for the thread object
40 inline void setRC(int rc) { _rc = rc; }
41 //! Return the value of the return code for the thread object
42 inline int getRC(int rc) { return(_rc);
43 }
44 inline void setAction(ZThreadAction act, void * usp=NULL)
45 { _act = act; _usp = usp; }
46
47
48 static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE)
49 { int ocs; pthread_setcancelstate(st, &ocs); }
50 // PTHREAD_CANCEL_ENABLE ou PTHREAD_CANCEL_DISABLE
51 static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED)
52 { int oct; pthread_setcanceltype(ct, &oct); }
53 // PTHREAD_CANCEL_DEFERRED ou PTHREAD_CANCEL_ASYNCHRONOUS
54
55
56 // ---- Attribute variables ----
57 pthread_t _thr;
58 size_t _ssize;
59 int _rc;
60 bool _initok;
61
62 ZThreadAction _act;
63 void * _usp;
64};
65
66
67class ZMutex {
68public:
69 explicit ZMutex();
70 virtual ~ZMutex();
71 //! Locks the mutex object
72 inline void lock()
73 { pthread_mutex_lock(_mutx); }
74 //! Unlocks the mutex object
75 inline void unlock()
76 { pthread_mutex_unlock(_mutx); }
77 //! Waits for a condition change
78 inline void wait()
79 { pthread_cond_wait(_condv, _mutx); }
80 //! Signal a condition change on the mutex object
81 inline void signal()
82 { pthread_cond_signal(_condv); }
83 //! Broadcasts a condition change on the mutex object
84 inline void broadcast()
85 { pthread_cond_broadcast(_condv); }
86
87 // Attributes
88 pthread_mutex_t * _mutx;
89 pthread_cond_t * _condv;
90};
91
92
93class ZSync {
94public:
95 explicit inline ZSync(ZMutex & mtx) {_mtx = &mtx; mtx.lock(); }
96 inline ~ZSync() { if (_mtx) _mtx->unlock(); }
97
98private:
99 ZMutex * _mtx;
100 inline ZSync() {_mtx = NULL; }
101
102};
103
104} // namespace SOPHYA
105
106#endif
Note: See TracBrowser for help on using the repository browser.