OpendTect-6_4  6.4
threadlock.h
Go to the documentation of this file.
1 #ifndef threadlock_h
2 #define threadlock_h
3 
4 /*
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Bert
9  Date: June 2013
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 */
14 
15 #include "basicmod.h"
16 #include "commondefs.h"
17 #include "plftypes.h"
18 
19 
43 namespace Threads
44 {
45 class Mutex;
46 class SpinLock;
47 class ReadWriteLock;
48 
54 {
55 public:
56 
57  enum Type { BigWork, SmallWork, MultiRead };
58 
59  Lock(bool for_just_a_few_operations=false);
60  Lock(Type);
61  Lock(const Lock&);
62  Lock& operator =(const Lock&);
63  virtual ~Lock();
64 
65 
66  // For thread-specialists:
67  inline bool isMutex() const { return (bool)mutex_; }
68  inline bool isSpinLock() const { return (bool)splock_;}
69  inline bool isRWLock() const { return (bool)rwlock_;}
70  inline Mutex& mutex() { return *mutex_; }
71  inline SpinLock& spinLock() { return *splock_; }
72  inline ReadWriteLock& readWriteLock() { return *rwlock_; }
73 
74 protected:
75 
79 
80 };
81 
82 
86 {
87 public:
88 
89  enum WaitType { WaitIfLocked, DontWaitForLock };
91  enum RWType { ReadLock, WriteLock };
93 
94  // default = WaitIfLocked / ReadLock
95  Locker(Lock&);
96  Locker(Lock&,WaitType);
97  Locker(Lock&,RWType);
98  Locker(Lock&,WaitType,RWType);
99  Locker(Lock&,RWType,WaitType);
100  virtual ~Locker() { unlockNow(); }
101 
102  inline bool isLocked() const { return needunlock_; }
103  //<! only useful if DontWaitForLock
104  void unlockNow();
105  //<! to explicitly release earlier than the
107  void reLock(WaitType wt=WaitIfLocked);
108  bool convertToWriteLock();
109  //<! only interesting for MultiRead
110 
111 protected:
112 
115  bool isread_;
116 
117 private:
118 
119  Locker(const Locker&);
121  Locker& operator =(const Locker&);
123 
124 };
125 
126 
134 mGlobal(Basic) bool lockSimpleSpinWaitLock(volatile int& lock);
135 mGlobal(Basic) bool lockSimpleSpinLock(volatile int& lock,Locker::WaitType);
136 mGlobal(Basic) void unlockSimpleSpinLock(volatile int& lock);
137 
138 
139 } // namespace Threads
140 
141 
142 #endif
#define mExpClass(module)
Definition: commondefs.h:160
Mutex * mutex_
Definition: threadlock.h:76
Locks the lock, shutting out access from other threads if needed.
Definition: threadlock.h:85
Lock that permits multiple readers to lock the object at the same time, but it will not allow any rea...
Definition: thread.h:141
WaitType
Definition: threadlock.h:89
Mutex & mutex()
Definition: threadlock.h:70
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class...
Definition: threadlock.h:53
Lock & lock_
Definition: threadlock.h:113
bool needunlock_
Definition: threadlock.h:114
ReadWriteLock * rwlock_
Definition: threadlock.h:78
#define mGlobal(module)
Definition: commondefs.h:163
bool lockSimpleSpinLock(volatile int &lock, Locker::WaitType)
bool isread_
Definition: threadlock.h:115
SpinLock * splock_
Definition: threadlock.h:77
RWType
Definition: threadlock.h:91
bool isLocked() const
Definition: threadlock.h:102
interface to threads that should be portable.
Definition: atomic.h:28
Is an alternative to Mutex. It is a lock which causes a thread trying to acquire it to simply wait in...
Definition: atomic.h:200
SpinLock & spinLock()
Definition: threadlock.h:71
Type
Definition: threadlock.h:57
bool isMutex() const
Definition: threadlock.h:67
Is a lock that allows a thread to have exlusive rights to something.
Definition: thread.h:43
bool isSpinLock() const
Definition: threadlock.h:68
bool isRWLock() const
Definition: threadlock.h:69
virtual ~Locker()
Definition: threadlock.h:100
void unlockSimpleSpinLock(volatile int &lock)
bool lockSimpleSpinWaitLock(volatile int &lock)
ReadWriteLock & readWriteLock()
Definition: threadlock.h:72

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