Make UEFI boot-platform build again
[haiku.git] / headers / private / fs_shell / fssh_auto_locker.h
blob889e0bb3ba6e0c05d3a3d7ead48901d2f176f86f
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 _FSSH_AUTO_LOCKER_H
6 #define _FSSH_AUTO_LOCKER_H
9 #include <fssh_types.h>
12 namespace FSShell {
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()
67 : fLockable(NULL),
68 fLocked(false)
72 inline AutoLocker(Lockable *lockable, bool alreadyLocked = false,
73 bool lockIfNotLocked = true)
74 : fLockable(lockable),
75 fLocked(fLockable && alreadyLocked)
77 if (!alreadyLocked && lockIfNotLocked)
78 Lock();
81 inline AutoLocker(Lockable &lockable, bool alreadyLocked = false,
82 bool lockIfNotLocked = true)
83 : fLockable(&lockable),
84 fLocked(fLockable && alreadyLocked)
86 if (!alreadyLocked && lockIfNotLocked)
87 Lock();
90 inline ~AutoLocker()
92 Unlock();
95 inline void SetTo(Lockable *lockable, bool alreadyLocked,
96 bool lockIfNotLocked = true)
98 Unlock();
99 fLockable = lockable;
100 fLocked = alreadyLocked;
101 if (!alreadyLocked && lockIfNotLocked)
102 Lock();
105 inline void SetTo(Lockable &lockable, bool alreadyLocked,
106 bool lockIfNotLocked = true)
108 SetTo(&lockable, alreadyLocked, lockIfNotLocked);
111 inline void Unset()
113 Unlock();
114 Detach();
117 inline bool Lock()
119 if (fLockable && !fLocked)
120 fLocked = fLocking.Lock(fLockable);
121 return fLocked;
124 inline void Unlock()
126 if (fLockable && fLocked) {
127 fLocking.Unlock(fLockable);
128 fLocked = false;
132 inline void Detach()
134 fLockable = NULL;
135 fLocked = false;
138 inline AutoLocker<Lockable, Locking> &operator=(Lockable *lockable)
140 SetTo(lockable);
141 return *this;
144 inline AutoLocker<Lockable, Locking> &operator=(Lockable &lockable)
146 SetTo(&lockable);
147 return *this;
150 inline bool IsLocked() const { return fLocked; }
152 inline operator bool() const { return fLocked; }
154 private:
155 Lockable *fLockable;
156 bool fLocked;
157 Locking fLocking;
161 } // namespace FSShell
163 using FSShell::AutoLocker;
165 #endif // _FSSH_AUTO_LOCKER_H