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

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