Make UEFI boot-platform build again
[haiku.git] / headers / private / shared / AutoLocker.h
blobfdcc49944bc596e5717fc2e8db88748cea3186c9
1 /*
2 * Copyright 2005-2007, Ingo Weinhold, bonefish@users.sf.net.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5 #ifndef _AUTO_LOCKER_H
6 #define _AUTO_LOCKER_H
9 #include <stddef.h>
12 namespace BPrivate {
14 // AutoLockerStandardLocking
15 template<typename Lockable>
16 class AutoLockerStandardLocking {
17 public:
18 inline bool Lock(Lockable* lockable)
20 return lockable->Lock();
23 inline void Unlock(Lockable* lockable)
25 lockable->Unlock();
29 // AutoLockerReadLocking
30 template<typename Lockable>
31 class AutoLockerReadLocking {
32 public:
33 inline bool Lock(Lockable* lockable)
35 return lockable->ReadLock();
38 inline void Unlock(Lockable* lockable)
40 lockable->ReadUnlock();
44 // AutoLockerWriteLocking
45 template<typename Lockable>
46 class AutoLockerWriteLocking {
47 public:
48 inline bool Lock(Lockable* lockable)
50 return lockable->WriteLock();
53 inline void Unlock(Lockable* lockable)
55 lockable->WriteUnlock();
59 // AutoLocker
60 template<typename Lockable,
61 typename Locking = AutoLockerStandardLocking<Lockable> >
62 class AutoLocker {
63 private:
64 typedef AutoLocker<Lockable, Locking> ThisClass;
65 public:
66 inline AutoLocker()
68 fLockable(NULL),
69 fLocked(false)
73 inline AutoLocker(const Locking& locking)
75 fLockable(NULL),
76 fLocking(locking),
77 fLocked(false)
81 inline AutoLocker(Lockable* lockable, bool alreadyLocked = false,
82 bool lockIfNotLocked = true)
84 fLockable(lockable),
85 fLocked(fLockable && alreadyLocked)
87 if (!alreadyLocked && lockIfNotLocked)
88 Lock();
91 inline AutoLocker(Lockable& lockable, bool alreadyLocked = false,
92 bool lockIfNotLocked = true)
94 fLockable(&lockable),
95 fLocked(fLockable && alreadyLocked)
97 if (!alreadyLocked && lockIfNotLocked)
98 Lock();
101 inline ~AutoLocker()
103 Unlock();
106 inline void SetTo(Lockable* lockable, bool alreadyLocked,
107 bool lockIfNotLocked = true)
109 Unlock();
110 fLockable = lockable;
111 fLocked = (lockable && alreadyLocked);
112 if (!alreadyLocked && lockIfNotLocked)
113 Lock();
116 inline void SetTo(Lockable& lockable, bool alreadyLocked,
117 bool lockIfNotLocked = true)
119 SetTo(&lockable, alreadyLocked, lockIfNotLocked);
122 inline void Unset()
124 Unlock();
125 Detach();
128 inline bool Lock()
130 if (fLockable && !fLocked)
131 fLocked = fLocking.Lock(fLockable);
132 return fLocked;
135 inline void Unlock()
137 if (fLockable && fLocked) {
138 fLocking.Unlock(fLockable);
139 fLocked = false;
143 inline void Detach()
145 fLockable = NULL;
146 fLocked = false;
149 inline AutoLocker<Lockable, Locking>& operator=(Lockable* lockable)
151 SetTo(lockable);
152 return *this;
155 inline AutoLocker<Lockable, Locking>& operator=(Lockable& lockable)
157 SetTo(&lockable);
158 return *this;
161 inline bool IsLocked() const { return fLocked; }
163 inline operator bool() const { return fLocked; }
165 protected:
166 Lockable* fLockable;
167 Locking fLocking;
168 bool fLocked;
172 } // namespace BPrivate
174 using ::BPrivate::AutoLocker;
175 using ::BPrivate::AutoLockerReadLocking;
176 using ::BPrivate::AutoLockerWriteLocking;
178 #endif // _AUTO_LOCKER_H