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