OpendTect  6.3
threadlock.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: Bert
8  Date: June 2013
9 ________________________________________________________________________
10 
11 */
12 
13 #include "basicmod.h"
14 #include "commondefs.h"
15 #include "plftypes.h"
16 
17 
41 namespace Threads
42 {
43 class Mutex;
44 class SpinLock;
45 class ReadWriteLock;
46 
52 {
53 public:
54 
55  enum Type { BigWork, SmallWork, MultiRead };
56 
57  Lock(bool for_just_a_few_operations=false);
58  Lock(Type);
59  Lock(const Lock&);
60  Lock& operator =(const Lock&);
61  virtual ~Lock();
62 
63 
64  // For thread-specialists:
65  inline bool isMutex() const { return (bool)mutex_; }
66  inline bool isSpinLock() const { return (bool)splock_;}
67  inline bool isRWLock() const { return (bool)rwlock_;}
68  inline Mutex& mutex() { return *mutex_; }
69  inline SpinLock& spinLock() { return *splock_; }
70  inline ReadWriteLock& readWriteLock() { return *rwlock_; }
71 
72 protected:
73 
77 
78 };
79 
80 
84 {
85 public:
86 
87  enum WaitType { WaitIfLocked, DontWaitForLock };
89  enum RWType { ReadLock, WriteLock };
91 
92  // default = WaitIfLocked / ReadLock
93  Locker(Lock&);
94  Locker(Lock&,WaitType);
95  Locker(Lock&,RWType);
96  Locker(Lock&,WaitType,RWType);
97  Locker(Lock&,RWType,WaitType);
98  virtual ~Locker() { unlockNow(); }
99 
100  inline bool isLocked() const { return needunlock_; }
101  //<! only useful if DontWaitForLock
102  void unlockNow();
103  //<! to explicitly release earlier than the
105  void reLock(WaitType wt=WaitIfLocked);
106  bool convertToWriteLock();
107  //<! only interesting for MultiRead
108 
109 protected:
110 
113  bool isread_;
114 
115 private:
116 
117  Locker(const Locker&);
119  Locker& operator =(const Locker&);
121 
122 };
123 
124 
132 mGlobal(Basic) bool lockSimpleSpinWaitLock(volatile int& lock);
133 mGlobal(Basic) bool lockSimpleSpinLock(volatile int& lock,Locker::WaitType);
134 mGlobal(Basic) void unlockSimpleSpinLock(volatile int& lock);
135 
136 
137 } // namespace Threads
#define mExpClass(module)
Definition: commondefs.h:157
Mutex * mutex_
Definition: threadlock.h:74
Locks the lock, shutting out access from other threads if needed.
Definition: threadlock.h:83
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:87
Mutex & mutex()
Definition: threadlock.h:68
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class...
Definition: threadlock.h:51
Lock & lock_
Definition: threadlock.h:111
bool needunlock_
Definition: threadlock.h:112
ReadWriteLock * rwlock_
Definition: threadlock.h:76
#define mGlobal(module)
Definition: commondefs.h:160
bool lockSimpleSpinLock(volatile int &lock, Locker::WaitType)
bool isread_
Definition: threadlock.h:113
SpinLock * splock_
Definition: threadlock.h:75
RWType
Definition: threadlock.h:89
bool isLocked() const
Definition: threadlock.h:100
interface to threads that should be portable.
Definition: atomic.h:24
Is an alternative to Mutex. It is a lock which causes a thread trying to acquire it to simply wait in...
Definition: atomic.h:130
SpinLock & spinLock()
Definition: threadlock.h:69
Type
Definition: threadlock.h:55
bool isMutex() const
Definition: threadlock.h:65
Is a lock that allows a thread to have exlusive rights to something.
Definition: thread.h:43
bool isSpinLock() const
Definition: threadlock.h:66
bool isRWLock() const
Definition: threadlock.h:67
virtual ~Locker()
Definition: threadlock.h:98
void unlockSimpleSpinLock(volatile int &lock)
bool lockSimpleSpinWaitLock(volatile int &lock)
ReadWriteLock & readWriteLock()
Definition: threadlock.h:70

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