OpendTect  6.6
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  RCS: $Id$
10 ________________________________________________________________________
11 
12 */
13 
14 #include "basicmod.h"
15 #include "commondefs.h"
16 #include "plftypes.h"
17 
18 mFDQtclass(QThread)
19 mFDQtclass(QMutex)
20 mFDQtclass(QWaitCondition)
21 
22 class CallBack;
23 
31 namespace Threads
32 {
33 
34 typedef void* ThreadID;
35 
45 {
46 public:
47  Mutex( bool recursive=false );
48  /*\If recursive, mutex can be locked
49  multiple times from the same thread without deadlock.
50  It will be unlock when unLock has been called the same
51  number of times as lock(). */
52  Mutex(const Mutex&);
53  virtual ~Mutex();
54 
55  void lock();
56  void unLock();
57 
58  bool tryLock();
65 protected:
66 
67  mQtclass(QMutex*) qmutex_;
68 
71  int count_;
73 public:
74  int getCount() const { return count_; }
75 };
76 
77 
78 
109 mExpClass(Basic) ConditionVar : public Mutex
110 {
111 public:
115 
116  void wait();
117  bool wait(unsigned long timeout);
121  void signal(bool all);
128 protected:
129 
130 #ifndef OD_NO_QT
131  mQtclass(QWaitCondition*) cond_;
132 #endif
133 };
134 
135 
143 {
144 public:
147  virtual ~ReadWriteLock();
148 
149  void readLock();
151  bool tryReadLock();
153  void writeLock();
155  bool tryWriteLock();
162  void readUnLock();
163  void writeUnLock();
165 
173 
176 
177 protected:
179  char status_;
180  //0 not writelocked, -2 write lock, -1 permissive lock
181  //>0, number of readers
183 };
184 
185 
200 #define mLockerClassImpl( mod, clssnm, clss, lockfn, unlockfn, trylockfn ) \
201 mExpClass(mod) clssnm \
202 { \
203 public: \
204  clssnm( clss& thelock, bool wait=true ) \
205  : lock_( thelock ) \
206  , islocked_( true ) \
207  { \
208  if ( wait ) thelock.lockfn; \
209  else islocked_ = thelock.trylockfn; \
210  } \
211  \
212  ~clssnm() { if ( islocked_ ) lock_.unlockfn; } \
213  bool isLocked() const { return islocked_; } \
214  \
215  void unLock() { islocked_ = false; lock_.unlockfn; } \
216  \
218  void lock() { islocked_ = true; lock_.lockfn; } \
219  \
222  \
223 protected: \
224  \
225  clss& lock_; \
226  bool islocked_; \
227 };
228 
229 mLockerClassImpl( Basic, MutexLocker, Mutex, lock(), unLock(), tryLock() )
231  readLock(), readUnLock(), tryReadLock() )
233  writeLock(), writeUnLock(), tryWriteLock() )
234 
235 
242 {
243 public:
244  Barrier(int nrthreads=-1,bool immediatrelease=true);
245  void setNrThreads(int);
246  int nrThreads() const { return nrthreads_; }
247 
248  bool waitForAll(bool unlock=true);
258  void releaseAll();
260  void releaseAllNoLock();
263  Mutex& mutex() { return condvar_; }
264 
265 protected:
266  void releaseAllInternal();
267 
268  ConditionVar condvar_;
269  int nrthreads_;
270  int threadcount_;
271  bool dorelease_;
272 
273  bool immediaterelease_;
274 };
275 
276 
284 mExpClass(Basic) Thread
285 {
286 public:
287 
288  Thread(void (*)(void*),const char* nm=nullptr);
289  Thread(const CallBack&,const char* nm=nullptr);
290  virtual ~Thread();
291 
292  ThreadID threadID() const;
293 
294  void waitForFinish();
298  const char* getName() const;
299 
300 
301 protected:
302 
303  mQtclass(QThread*) thread_;
304 };
305 
312 mGlobal(Basic) int getNrProcessors();
314 mGlobal(Basic) void setCurrentThreadProcessorAffinity( int cpu );
319 mGlobal(Basic) void sleep(double seconds);
320 
321 
322 } //namespace
323 
Threads::currentThread
ThreadID currentThread()
Threads::Mutex::lock
void lock()
Threads::ReadWriteLock::convWriteToReadLock
void convWriteToReadLock()
Lock MUST be writeLocked when calling.
Threads::ReadWriteLock::readUnLock
void readUnLock()
Threads::Mutex
Is a lock that allows a thread to have exlusive rights to something.
Definition: thread.h:45
mGlobal
#define mGlobal(module)
Definition: commondefs.h:180
Threads::Mutex::lockingthread_
ThreadID lockingthread_
Only set in debug-mode.
Definition: thread.h:69
Threads::ReadWriteLock::convReadToWriteLock
bool convReadToWriteLock()
commondefs.h
visBase::writeLock
writeLock()
Threads::ConditionVar::wait
bool wait(unsigned long timeout)
Threads::ReadWriteLock
Lock that permits multiple readers to lock the object at the same time, but it will not allow any rea...
Definition: thread.h:143
Threads::ReadWriteLock::ReadWriteLock
ReadWriteLock()
mFDQtclass
#define mFDQtclass(cls)
Definition: commondefs.h:233
mExpClass
#define mExpClass(module)
Definition: commondefs.h:177
mLockerClassImpl
#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:200
plftypes.h
Threads::Mutex::getCount
int getCount() const
Definition: thread.h:74
Threads::ReadWriteLock::readLock
void readLock()
No writers will be active.
Threads::Mutex::Mutex
Mutex(bool recursive=false)
Threads::getSystemNrProcessors
int getSystemNrProcessors()
Threads::ConditionVar::ConditionVar
ConditionVar()
Threads::ReadWriteLock::tryReadLock
bool tryReadLock()
No writers will be active.
Threads::ThreadID
void * ThreadID
Definition: thread.h:34
Threads::Mutex::Mutex
Mutex(const Mutex &)
CallBack
CallBacks object-oriented (object + method).
Definition: callback.h:62
Threads::ConditionVar::~ConditionVar
~ConditionVar()
Threads::ConditionVar::signal
void signal(bool all)
Threads::Mutex::~Mutex
virtual ~Mutex()
Threads::ReadWriteLock::statuscond_
ConditionVar statuscond_
Definition: thread.h:182
Threads::ConditionVar::wait
void wait()
Threads::WriteLockLocker
Definition: thread.h:230
Threads::Mutex::count_
int count_
Only set in debug-mode.
Definition: thread.h:71
Threads::ReadWriteLock::convPermissiveToWriteLock
void convPermissiveToWriteLock()
Threads::sleep
void sleep(double seconds)
Threads::ConditionVar
Is an object that faciliates many threads to wait for something to happen.
Definition: thread.h:110
all
@ all
Definition: i_layout.h:35
Threads::getNrProcessors
int getNrProcessors()
Threads::Mutex::tryLock
bool tryLock()
Threads::ReadWriteLock::permissiveWriteUnLock
void permissiveWriteUnLock()
Threads::ConditionVar::ConditionVar
ConditionVar(const ConditionVar &)
Threads::ReadLockLocker
Definition: thread.h:228
Threads::setCurrentThreadProcessorAffinity
void setCurrentThreadProcessorAffinity(int cpu)
Threads::ReadWriteLock::nrreaders_
int nrreaders_
Definition: thread.h:178
Threads::ReadWriteLock::convWriteToPermissive
void convWriteToPermissive()
Threads::Barrier
Waits for a number of threads to reach a certain point (i.e. the call to Barrier::waitForAll)....
Definition: thread.h:239
Threads::ReadWriteLock::status_
char status_
Definition: thread.h:179
Threads::ReadWriteLock::writeUnLock
void writeUnLock()
mQtclass
#define mQtclass(cls)
Definition: commondefs.h:234
Threads::ReadWriteLock::~ReadWriteLock
virtual ~ReadWriteLock()
Threads::Thread
Is the base class for all threads. Start it by creating it and give it the function or CallBack to ex...
Definition: thread.h:282
Threads::ReadWriteLock::ReadWriteLock
ReadWriteLock(const ReadWriteLock &)
Threads::ReadWriteLock::permissiveWriteLock
void permissiveWriteLock()
Threads::MutexLocker
Definition: thread.h:226
Threads::Mutex::qmutex_
::QMutex * qmutex_
Definition: thread.h:67
Threads::Mutex::unLock
void unLock()
Threads::ReadWriteLock::tryWriteLock
bool tryWriteLock()
No readers will be active.
Threads
interface to threads that should be portable.
Definition: atomic.h:23
visBase::writeUnLock
writeUnLock()
Threads::ConditionVar::cond_
::QWaitCondition * cond_
Definition: thread.h:131
Threads::ReadWriteLock::writeLock
void writeLock()
No readers will be active.

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