OpendTect-6_4  6.4
thread.h
Go to the documentation of this file.
1 #ifndef thread_h
2 #define thread_h
3 
4 /*
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: K. Tingdahl
9  Date: 9-3-1999
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 */
14 
15 #include "basicmod.h"
16 #include "commondefs.h"
17 #include "plftypes.h"
18 
19 mFDQtclass(QThread)
20 mFDQtclass(QMutex)
21 mFDQtclass(QWaitCondition)
22 
23 class CallBack;
24 
32 namespace Threads
33 {
34 
44 {
45 public:
46  Mutex( bool recursive=false );
47  /*\If recursive, mutex can be locked
48  multiple times from the same thread without deadlock.
49  It will be unlock when unLock has been called the same
50  number of times as lock(). */
51  Mutex(const Mutex&);
52  virtual ~Mutex();
53 
54  void lock();
55  void unLock();
56 
57  bool tryLock();
64 protected:
65 
66  mQtclass(QMutex*) qmutex_;
67 
68  const void* lockingthread_;
70  int count_;
72 public:
73  int getCount() const { return count_; }
74 };
75 
76 
77 
108 mExpClass(Basic) ConditionVar : public Mutex
109 {
110 public:
111  ConditionVar();
112  ConditionVar(const ConditionVar&);
113  ~ConditionVar();
114 
115  void wait();
116  bool wait(unsigned long timeout);
120  void signal(bool all);
127 protected:
128 
129 #ifndef OD_NO_QT
130  mQtclass(QWaitCondition*) cond_;
131 #endif
132 };
133 
134 
142 {
143 public:
144  ReadWriteLock();
145  ReadWriteLock(const ReadWriteLock&);
146  virtual ~ReadWriteLock();
147 
148  void readLock();
150  bool tryReadLock();
152  void writeLock();
154  bool tryWriteLock();
156  void permissiveWriteLock();
161  void readUnLock();
162  void writeUnLock();
163  void permissiveWriteUnLock();
164 
165  bool convReadToWriteLock();
170  void convWriteToReadLock();
172 
173  void convPermissiveToWriteLock();
174  void convWriteToPermissive();
175 
176 protected:
178  char status_;
179  //0 not writelocked, -2 write lock, -1 permissive lock
180  //>0, number of readers
182 };
183 
184 
199 #define mLockerClassImpl( mod, clssnm, clss, lockfn, unlockfn, trylockfn ) \
200 mExpClass(mod) clssnm \
201 { \
202 public: \
203  clssnm( clss& thelock, bool wait=true ) \
204  : lock_( thelock ) \
205  , islocked_( true ) \
206  { \
207  if ( wait ) thelock.lockfn; \
208  else islocked_ = thelock.trylockfn; \
209  } \
210  \
211  ~clssnm() { if ( islocked_ ) lock_.unlockfn; } \
212  bool isLocked() const { return islocked_; } \
213  \
214  void unLock() { islocked_ = false; lock_.unlockfn; } \
215  \
217  void lock() { islocked_ = true; lock_.lockfn; } \
218  \
221  \
222 protected: \
223  \
224  clss& lock_; \
225  bool islocked_; \
226 };
227 
228 mLockerClassImpl( Basic, MutexLocker, Mutex, lock(), unLock(), tryLock() )
230  readLock(), readUnLock(), tryReadLock() )
231 mLockerClassImpl( Basic, WriteLockLocker, ReadWriteLock,
232  writeLock(), writeUnLock(), tryWriteLock() )
233 
234 
241 {
242 public:
243  Barrier(int nrthreads=-1,bool immediatrelease=true);
244  void setNrThreads(int);
245  int nrThreads() const { return nrthreads_; }
246 
247  bool waitForAll(bool unlock=true);
257  void releaseAll();
259  void releaseAllNoLock();
262  Mutex& mutex() { return condvar_; }
263 
264 protected:
265  void releaseAllInternal();
266 
271 
273 };
274 
275 
284 {
285 public:
286 
287  Thread(void (*)(void*),const char* nm=0);
288  Thread(const CallBack&,const char* nm=0);
289  virtual ~Thread();
290 
291  const void* threadID() const;
292 
293  void waitForFinish();
297  const char* getName() const;
298 
299 
300 protected:
301 
302  mQtclass(QThread*) thread_;
303 };
304 
310 mGlobal(Basic) int getSystemNrProcessors();
311 mGlobal(Basic) int getNrProcessors();
312 mGlobal(Basic) const void* currentThread();
313 mGlobal(Basic) void setCurrentThreadProcessorAffinity( int cpu );
318 mGlobal(Basic) void sleep(double seconds);
319 
320 
321 } //namespace
322 
323 #endif
#define mExpClass(module)
Definition: commondefs.h:160
Is an object that faciliates many threads to wait for something to happen.
Definition: thread.h:108
Mutex & mutex()
Definition: thread.h:262
Lock that permits multiple readers to lock the object at the same time, but it will not allow any rea...
Definition: thread.h:141
int nrThreads() const
Definition: thread.h:245
Definition: thread.h:232
#define mQtclass(cls)
Definition: commondefs.h:229
#define mGlobal(module)
Definition: commondefs.h:163
ConditionVar condvar_
Definition: thread.h:267
bool immediaterelease_
Definition: thread.h:272
Definition: i_layout.h:35
#define mLockerClassImpl(mod, clssnm, clss, lockfn, unlockfn, trylockfn)
Is an object that is convenient to use when a mutex should be locked and unlocked automatically when ...
Definition: thread.h:199
bool dorelease_
Definition: thread.h:270
int nrthreads_
Definition: thread.h:268
int getNrProcessors()
interface to threads that should be portable.
Definition: atomic.h:28
int getSystemNrProcessors()
Is the base class for all threads. Start it by creating it and give it the function or CallBack to ex...
Definition: thread.h:283
int threadcount_
Definition: thread.h:269
int nrreaders_
Definition: thread.h:177
char status_
Definition: thread.h:178
Definition: thread.h:228
void sleep(double seconds)
#define mFDQtclass(cls)
Definition: commondefs.h:228
Waits for a number of threads to reach a certain point (i.e. the call to Barrier::waitForAll). Once everyone has arrived, everyone is released.
Definition: thread.h:240
ConditionVar statuscond_
Definition: thread.h:181
Definition: thread.h:230
Is a lock that allows a thread to have exlusive rights to something.
Definition: thread.h:43
void setCurrentThreadProcessorAffinity(int cpu)
const void * currentThread()
CallBacks object-oriented (object + method).
Definition: callback.h:57

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019