xref: /haiku/headers/private/kernel/util/AutoLock.h (revision d9cebac2b77547b7064f22497514eecd2d047160)
1 /*
2  * Copyright 2005-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef KERNEL_UTIL_AUTO_LOCKER_H
6 #define KERNEL_UTIL_AUTO_LOCKER_H
7 
8 
9 #include <KernelExport.h>
10 
11 #include <shared/AutoLocker.h>
12 
13 #include <int.h>
14 #include <lock.h>
15 
16 
17 namespace BPrivate {
18 
19 // MutexLocking
20 class MutexLocking {
21 public:
22 	inline bool Lock(mutex *lockable)
23 	{
24 		return mutex_lock(lockable) == B_OK;
25 	}
26 
27 	inline void Unlock(mutex *lockable)
28 	{
29 		mutex_unlock(lockable);
30 	}
31 };
32 
33 // MutexLocker
34 typedef AutoLocker<mutex, MutexLocking> MutexLocker;
35 
36 // RecursiveLockLocking
37 class RecursiveLockLocking {
38 public:
39 	inline bool Lock(recursive_lock *lockable)
40 	{
41 		return recursive_lock_lock(lockable) == B_OK;
42 	}
43 
44 	inline void Unlock(recursive_lock *lockable)
45 	{
46 		recursive_lock_unlock(lockable);
47 	}
48 };
49 
50 // RecursiveLocker
51 typedef AutoLocker<recursive_lock, RecursiveLockLocking> RecursiveLocker;
52 
53 // BenaphoreLocking
54 class BenaphoreLocking {
55 public:
56 	inline bool Lock(benaphore *lockable)
57 	{
58 		return benaphore_lock(lockable) == B_OK;
59 	}
60 
61 	inline void Unlock(benaphore *lockable)
62 	{
63 		benaphore_unlock(lockable);
64 	}
65 };
66 
67 // BenaphoreLocker
68 typedef AutoLocker<benaphore, BenaphoreLocking> BenaphoreLocker;
69 
70 // InterruptsLocking
71 class InterruptsLocking {
72 public:
73 	inline bool Lock(int* lockable)
74 	{
75 		*lockable = disable_interrupts();
76 		return true;
77 	}
78 
79 	inline void Unlock(int* lockable)
80 	{
81 		restore_interrupts(*lockable);
82 	}
83 };
84 
85 // InterruptsLocker
86 class InterruptsLocker : public AutoLocker<int, InterruptsLocking> {
87 public:
88 	inline InterruptsLocker(bool alreadyLocked = false,
89 		bool lockIfNotLocked = true)
90 		: AutoLocker<int, InterruptsLocking>(&fState, alreadyLocked,
91 			lockIfNotLocked)
92 	{
93 	}
94 
95 private:
96 	int	fState;
97 };
98 
99 // SpinLocking
100 class SpinLocking {
101 public:
102 	inline bool Lock(spinlock* lockable)
103 	{
104 		acquire_spinlock(lockable);
105 		return true;
106 	}
107 
108 	inline void Unlock(spinlock* lockable)
109 	{
110 		release_spinlock(lockable);
111 	}
112 };
113 
114 // SpinLocker
115 typedef AutoLocker<spinlock, SpinLocking> SpinLocker;
116 
117 // InterruptsSpinLocking
118 class InterruptsSpinLocking {
119 public:
120 	struct State {
121 		State(spinlock* lock)
122 			: lock(lock)
123 		{
124 		}
125 
126 		int			state;
127 		spinlock*	lock;
128 	};
129 
130 	inline bool Lock(spinlock* lockable)
131 	{
132 		fState = disable_interrupts();
133 		acquire_spinlock(lockable);
134 		return true;
135 	}
136 
137 	inline void Unlock(spinlock* lockable)
138 	{
139 		release_spinlock(lockable);
140 		restore_interrupts(fState);
141 	}
142 
143 private:
144 	int	fState;
145 };
146 
147 // InterruptsSpinLocker
148 typedef AutoLocker<spinlock, InterruptsSpinLocking> InterruptsSpinLocker;
149 
150 }	// namespace BPrivate
151 
152 using BPrivate::AutoLocker;
153 using BPrivate::MutexLocker;
154 using BPrivate::RecursiveLocker;
155 using BPrivate::BenaphoreLocker;
156 using BPrivate::InterruptsLocker;
157 using BPrivate::SpinLocker;
158 using BPrivate::InterruptsSpinLocker;
159 
160 #endif	// KERNEL_UTIL_AUTO_LOCKER_H
161