xref: /haiku/src/kits/support/Locker.cpp (revision 90c3b9bf9fe633e4c6a59d42c11c3f523183c553)
1 /*
2  * Copyright 2001-2009 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Erik Jaesler, erik@cgsoftware.com
7  */
8 
9 
10 /*!	Semaphore-type class for thread safety */
11 
12 
13 #include <OS.h>
14 #include <Locker.h>
15 #include <SupportDefs.h>
16 
17 #include "support_kit_config.h"
18 
19 
20 // Data Member Documentation:
21 //
22 // The "fBenaphoreCount" member is set to 1 if the BLocker style is
23 // semaphore.  If the style is benaphore, it is initialized to 0 and
24 // is incremented atomically when it is acquired, decremented when it
25 // is released.  By setting the benaphore count to 1 when the style is
26 // semaphore, the benaphore effectively becomes a semaphore.  I was able
27 // to determine this is what Be's implementation does by testing the
28 // result of the CountLockRequests() member.
29 //
30 // The "fSemaphoreID" member holds the sem_id returned from create_sem()
31 // when the BLocker is constructed.  It is used to acquire and release
32 // the lock regardless of the lock style (semaphore or benaphore).
33 //
34 // The "fLockOwner" member holds the thread_id of the thread which
35 // currently holds the lock.  If no thread holds the lock, it is set to
36 // B_ERROR.
37 //
38 // The "fRecursiveCount" member holds a count of the number of times the
39 // thread holding the lock has acquired the lock without a matching unlock.
40 // It is basically the number of times the thread must call Unlock() before
41 // the lock can be acquired by a different thread.
42 //
43 
44 
45 BLocker::BLocker()
46 {
47 	InitLocker(NULL, true);
48 }
49 
50 
51 BLocker::BLocker(const char *name)
52 {
53 	InitLocker(name, true);
54 }
55 
56 
57 BLocker::BLocker(bool benaphoreStyle)
58 {
59 	InitLocker(NULL, benaphoreStyle);
60 }
61 
62 
63 BLocker::BLocker(const char *name, bool benaphoreStyle)
64 {
65 	InitLocker(name, benaphoreStyle);
66 }
67 
68 
69 /*!	This constructor is not documented.  The final argument is ignored for
70 	now.  In Be's headers, its called "for_IPC".  DO NOT USE THIS
71 	CONSTRUCTOR!
72 */
73 BLocker::BLocker(const char *name, bool benaphoreStyle,
74 	bool)
75 {
76 	InitLocker(name, benaphoreStyle);
77 }
78 
79 
80 BLocker::~BLocker()
81 {
82 	delete_sem(fSemaphoreID);
83 }
84 
85 
86 status_t
87 BLocker::InitCheck() const
88 {
89 	return fSemaphoreID >= 0 ? B_OK : fSemaphoreID;
90 }
91 
92 
93 bool
94 BLocker::Lock()
95 {
96 	status_t result;
97 	return AcquireLock(B_INFINITE_TIMEOUT, &result);
98 }
99 
100 
101 status_t
102 BLocker::LockWithTimeout(bigtime_t timeout)
103 {
104 	status_t result;
105 
106 	AcquireLock(timeout, &result);
107 	return result;
108 }
109 
110 
111 void
112 BLocker::Unlock()
113 {
114 	// The Be Book explicitly allows any thread, not just the lock owner, to
115 	// unlock. This is bad practice and Haiku should not allow it.
116 	if (!IsLocked())
117 		debugger("Trying to unlock from the wrong thread (#6400)");
118 
119 	// Decrement the number of outstanding locks this thread holds
120 	// on this BLocker.
121 	fRecursiveCount--;
122 
123 	// If the recursive count is now at 0, that means the BLocker has
124 	// been released by the thread.
125 	if (fRecursiveCount == 0) {
126 		// The BLocker is no longer owned by any thread.
127 		fLockOwner = B_ERROR;
128 
129 		// Decrement the benaphore count and store the undecremented
130 		// value in oldBenaphoreCount.
131 		int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, -1);
132 
133 		// If the oldBenaphoreCount is greater than 1, then there is
134 		// at lease one thread waiting for the lock in the case of a
135 		// benaphore.
136 		if (oldBenaphoreCount > 1) {
137 			// Since there are threads waiting for the lock, it must
138 			// be released.  Note, the old benaphore count will always be
139 			// greater than 1 for a semaphore so the release is always done.
140 			release_sem(fSemaphoreID);
141 		}
142 	}
143 }
144 
145 
146 thread_id
147 BLocker::LockingThread() const
148 {
149 	return fLockOwner;
150 }
151 
152 
153 bool
154 BLocker::IsLocked() const
155 {
156 	// This member returns true if the calling thread holds the lock.
157 	// The easiest way to determine this is to compare the result of
158 	// find_thread() to the fLockOwner.
159 	return find_thread(NULL) == fLockOwner;
160 }
161 
162 
163 int32
164 BLocker::CountLocks() const
165 {
166 	return fRecursiveCount;
167 }
168 
169 
170 int32
171 BLocker::CountLockRequests() const
172 {
173 	return fBenaphoreCount;
174 }
175 
176 
177 sem_id
178 BLocker::Sem() const
179 {
180 	return fSemaphoreID;
181 }
182 
183 
184 void
185 BLocker::InitLocker(const char *name, bool benaphore)
186 {
187 	if (name == NULL)
188 		name = "some BLocker";
189 
190 	if (benaphore && !BLOCKER_ALWAYS_SEMAPHORE_STYLE) {
191 		// Because this is a benaphore, initialize the benaphore count and
192 		// create the semaphore.  Because this is a benaphore, the semaphore
193 		// count starts at 0 (ie acquired).
194 		fBenaphoreCount = 0;
195 		fSemaphoreID = create_sem(0, name);
196 	} else {
197 		// Because this is a semaphore, initialize the benaphore count to -1
198 		// and create the semaphore.  Because this is semaphore style, the
199 		// semaphore count starts at 1 so that one thread can acquire it and
200 		// the next thread to acquire it will block.
201 		fBenaphoreCount = 1;
202 		fSemaphoreID = create_sem(1, name);
203 	}
204 
205 	// The lock is currently not acquired so there is no owner.
206 	fLockOwner = B_ERROR;
207 
208 	// The lock is currently not acquired so the recursive count is zero.
209 	fRecursiveCount = 0;
210 }
211 
212 
213 bool
214 BLocker::AcquireLock(bigtime_t timeout, status_t *error)
215 {
216 	// By default, return no error.
217 	status_t status = B_OK;
218 
219 	// Only try to acquire the lock if the thread doesn't already own it.
220 	if (!IsLocked()) {
221 		// Increment the benaphore count and test to see if it was already
222 		// greater than 0. If it is greater than 0, then some thread already has
223 		// the benaphore or the style is a semaphore. Either way, we need to
224 		// acquire the semaphore in this case.
225 		int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, 1);
226 		if (oldBenaphoreCount > 0) {
227 			do {
228 				status = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_TIMEOUT,
229 					timeout);
230 			} while (status == B_INTERRUPTED);
231 
232 			// Note, if the lock here does time out, the benaphore count
233 			// is not decremented.  By doing this, the benaphore count will
234 			// never go back to zero.  This means that the locking essentially
235 			// changes to semaphore style if this was a benaphore.
236 			//
237 			// Doing the decrement of the benaphore count when the acquisition
238 			// fails is a risky thing to do.  If you decrement the counter at
239 			// the same time the thread which holds the benaphore does an
240 			// Unlock(), there is serious risk of a race condition.
241 			//
242 			// If the Unlock() sees a positive count and releases the semaphore
243 			// and then the timed out thread decrements the count to 0, there
244 			// is no one to take the semaphore.  The next two threads will be
245 			// able to acquire the benaphore at the same time!  The first will
246 			// increment the counter and acquire the lock.  The second will
247 			// acquire the semaphore and therefore the lock.  Not good.
248 			//
249 			// This has been discussed on the becodetalk mailing list and
250 			// Trey from Be had this to say:
251 			//
252 			// I looked at the LockWithTimeout() code, and it does not have
253 			// _this_ (ie the race condition) problem.  It circumvents it by
254 			// NOT doing the atomic_add(&count, -1) if the semaphore
255 			// acquisition fails.  This means that if a
256 			// BLocker::LockWithTimeout() times out, all other Lock*() attempts
257 			// turn into guaranteed semaphore grabs, _with_ the overhead of a
258 			// (now) useless atomic_add().
259 			//
260 			// Given Trey's comments, it looks like Be took the same approach
261 			// I did.  The output of CountLockRequests() of Be's implementation
262 			// confirms Trey's comments also.
263 			//
264 			// Finally some thoughts for the future with this code:
265 			//   - If 2^31 timeouts occur on a 32-bit machine (ie today),
266 			//     the benaphore count will wrap to a negative number.  This
267 			//     would have unknown consequences on the ability of the BLocker
268 			//     to continue to function.
269 			//
270 		}
271 	}
272 
273 	// If the lock has successfully been acquired.
274 	if (status == B_OK) {
275 		// Set the lock owner to this thread and increment the recursive count
276 		// by one.  The recursive count is incremented because one more Unlock()
277 		// is now required to release the lock (ie, 0 => 1, 1 => 2 etc).
278 		fLockOwner = find_thread(NULL);
279 		fRecursiveCount++;
280 	}
281 
282 	if (error != NULL)
283 		*error = status;
284 
285 	// Return true if the lock has been acquired.
286 	return (status == B_OK);
287 }
288