2 * Copyright 2005-2007, Ingo Weinhold, bonefish@users.sf.net.
3 * All rights reserved. Distributed under the terms of the MIT License.
14 // AutoLockerStandardLocking
15 template<typename Lockable
>
16 class AutoLockerStandardLocking
{
18 inline bool Lock(Lockable
* lockable
)
20 return lockable
->Lock();
23 inline void Unlock(Lockable
* lockable
)
29 // AutoLockerReadLocking
30 template<typename Lockable
>
31 class AutoLockerReadLocking
{
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
{
48 inline bool Lock(Lockable
* lockable
)
50 return lockable
->WriteLock();
53 inline void Unlock(Lockable
* lockable
)
55 lockable
->WriteUnlock();
60 template<typename Lockable
,
61 typename Locking
= AutoLockerStandardLocking
<Lockable
> >
64 typedef AutoLocker
<Lockable
, Locking
> ThisClass
;
73 inline AutoLocker(const Locking
& locking
)
81 inline AutoLocker(Lockable
* lockable
, bool alreadyLocked
= false,
82 bool lockIfNotLocked
= true)
85 fLocked(fLockable
&& alreadyLocked
)
87 if (!alreadyLocked
&& lockIfNotLocked
)
91 inline AutoLocker(Lockable
& lockable
, bool alreadyLocked
= false,
92 bool lockIfNotLocked
= true)
95 fLocked(fLockable
&& alreadyLocked
)
97 if (!alreadyLocked
&& lockIfNotLocked
)
106 inline void SetTo(Lockable
* lockable
, bool alreadyLocked
,
107 bool lockIfNotLocked
= true)
110 fLockable
= lockable
;
111 fLocked
= (lockable
&& alreadyLocked
);
112 if (!alreadyLocked
&& lockIfNotLocked
)
116 inline void SetTo(Lockable
& lockable
, bool alreadyLocked
,
117 bool lockIfNotLocked
= true)
119 SetTo(&lockable
, alreadyLocked
, lockIfNotLocked
);
130 if (fLockable
&& !fLocked
)
131 fLocked
= fLocking
.Lock(fLockable
);
137 if (fLockable
&& fLocked
) {
138 fLocking
.Unlock(fLockable
);
149 inline AutoLocker
<Lockable
, Locking
>& operator=(Lockable
* lockable
)
155 inline AutoLocker
<Lockable
, Locking
>& operator=(Lockable
& lockable
)
161 inline bool IsLocked() const { return fLocked
; }
163 inline operator bool() const { return fLocked
; }
172 } // namespace BPrivate
174 using ::BPrivate::AutoLocker
;
175 using ::BPrivate::AutoLockerReadLocking
;
176 using ::BPrivate::AutoLockerWriteLocking
;
178 #endif // _AUTO_LOCKER_H