OpendTect  6.3
thread.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: K. Tingdahl
8  Date: 9-3-1999
9 ________________________________________________________________________
10 
11 */
12 
13 #include "basicmod.h"
14 #include "commondefs.h"
15 #include "plftypes.h"
16 
17 mFDQtclass(QThread)
18 mFDQtclass(QMutex)
19 mFDQtclass(QWaitCondition)
20 
21 class CallBack;
22 
30 namespace Threads
31 {
32 
33 typedef void* ThreadID;
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  ThreadID 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  bool isLocked() const { return status_ != 0;}
149  bool isLockedForWrite() const { return status_ < 0; }
150  bool isLockedForRead() const { return status_ > 0; }
151 
152  void readLock();
154  bool tryReadLock();
156  void writeLock();
158  bool tryWriteLock();
160  void permissiveWriteLock();
165  void readUnLock();
166  void writeUnLock();
167  void permissiveWriteUnLock();
168 
169  bool convReadToWriteLock();
174  void convWriteToReadLock();
176 
177  void convPermissiveToWriteLock();
178  void convWriteToPermissive();
179 
180 protected:
182  char status_;
183  //0 not writelocked, -2 write lock, -1 permissive lock
184  //>0, number of readers
186 };
187 
188 
203 #define mLockerClassImpl( mod, clssnm, clss, lockfn, unlockfn, trylockfn ) \
204 mExpClass(mod) clssnm \
205 { \
206 public: \
207  clssnm( clss& thelock, bool wait=true ) \
208  : lock_( thelock ) \
209  , islocked_( true ) \
210  { \
211  if ( wait ) thelock.lockfn; \
212  else islocked_ = thelock.trylockfn; \
213  } \
214  \
215  ~clssnm() { if ( islocked_ ) lock_.unlockfn; } \
216  bool isLocked() const { return islocked_; } \
217  \
218  void unLock() { islocked_ = false; lock_.unlockfn; } \
219  \
221  void lock() { islocked_ = true; lock_.lockfn; } \
222  \
225  \
226 protected: \
227  \
228  clss& lock_; \
229  bool islocked_; \
230 };
231 
232 mLockerClassImpl( Basic, MutexLocker, Mutex, lock(), unLock(), tryLock() )
234  readLock(), readUnLock(), tryReadLock() )
235 mLockerClassImpl( Basic, WriteLockLocker, ReadWriteLock,
236  writeLock(), writeUnLock(), tryWriteLock() )
237 
238 
245 {
246 public:
247  Barrier(int nrthreads=-1,bool immediatrelease=true);
248  void setNrThreads(int);
249  int nrThreads() const { return nrthreads_; }
250 
251  bool waitForAll(bool unlock=true);
261  void releaseAll();
263  void releaseAllNoLock();
266  Mutex& mutex() { return condvar_; }
267 
268 protected:
269  void releaseAllInternal();
270 
275 
277 };
278 
279 
291 {
292 public:
293 
294  Thread(void (*)(void*),const char* nm);
295  Thread(const CallBack&,const char* nm);
296  virtual ~Thread();
297 
298  ThreadID threadID() const;
299 
300  void waitForFinish();
304  const char* getName() const;
305 
306 
307 protected:
308 
309  mQtclass(QThread*) thread_;
310 };
311 
317 mGlobal(Basic) int getSystemNrProcessors();
318 mGlobal(Basic) int getNrProcessors();
319 mGlobal(Basic) ThreadID currentThread();
320 mGlobal(Basic) void setCurrentThreadProcessorAffinity( int cpu );
325 mGlobal(Basic) void sleep(double seconds);
326 
327 
328 } //namespace
#define mExpClass(module)
Definition: commondefs.h:157
Is an object that faciliates many threads to wait for something to happen.
Definition: thread.h:108
Mutex & mutex()
Definition: thread.h:266
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:249
Definition: thread.h:236
ThreadID currentThread()
#define mQtclass(cls)
Definition: commondefs.h:232
#define mGlobal(module)
Definition: commondefs.h:160
ConditionVar condvar_
Definition: thread.h:271
bool immediaterelease_
Definition: thread.h:276
Definition: i_layout.h:33
#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:203
bool dorelease_
Definition: thread.h:274
int nrthreads_
Definition: thread.h:272
int getNrProcessors()
bool isLockedForWrite() const
Definition: thread.h:149
interface to threads that should be portable.
Definition: atomic.h:24
bool isLockedForRead() const
Definition: thread.h:150
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:290
void * ThreadID
Definition: thread.h:33
int threadcount_
Definition: thread.h:273
int nrreaders_
Definition: thread.h:181
char status_
Definition: thread.h:182
Definition: thread.h:232
bool isLocked() const
Definition: thread.h:148
void sleep(double seconds)
#define mFDQtclass(cls)
Definition: commondefs.h:231
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:244
ConditionVar statuscond_
Definition: thread.h:185
Definition: thread.h:234
Is a lock that allows a thread to have exlusive rights to something.
Definition: thread.h:43
void setCurrentThreadProcessorAffinity(int cpu)
CallBacks object-oriented (object + method).
Definition: callback.h:62

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