xref: /haiku/src/kits/support/Locker.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2001-2009, Haiku, Inc.
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 
112 void
113 BLocker::Unlock()
114 {
115 	// If the thread currently holds the lockdecrement
116 	if (IsLocked()) {
117 		// Decrement the number of outstanding locks this thread holds
118 		// on this BLocker.
119 		fRecursiveCount--;
120 
121 		// If the recursive count is now at 0, that means the BLocker has
122 		// been released by the thread.
123 		if (fRecursiveCount == 0) {
124 			// The BLocker is no longer owned by any thread.
125 			fLockOwner = B_ERROR;
126 
127     		// Decrement the benaphore count and store the undecremented
128     		// value in oldBenaphoreCount.
129 			int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, -1);
130 
131 			// If the oldBenaphoreCount is greater than 1, then there is
132 			// at lease one thread waiting for the lock in the case of a
133 			// benaphore.
134    		    if (oldBenaphoreCount > 1) {
135  				// Since there are threads waiting for the lock, it must
136  				// be released.  Note, the old benaphore count will always be
137  				// greater than 1 for a semaphore so the release is always done.
138     			release_sem(fSemaphoreID);
139 	    	}
140 	    }
141     }
142 }
143 
144 
145 thread_id
146 BLocker::LockingThread() const
147 {
148     return fLockOwner;
149 }
150 
151 
152 bool
153 BLocker::IsLocked() const
154 {
155 	// This member returns true if the calling thread holds the lock.
156 	// The easiest way to determine this is to compare the result of
157 	// find_thread() to the fLockOwner.
158     return find_thread(NULL) == fLockOwner;
159 }
160 
161 
162 int32
163 BLocker::CountLocks() const
164 {
165     return fRecursiveCount;
166 }
167 
168 
169 int32
170 BLocker::CountLockRequests() const
171 {
172     return fBenaphoreCount;
173 }
174 
175 
176 sem_id
177 BLocker::Sem() const
178 {
179     return fSemaphoreID;
180 }
181 
182 
183 void
184 BLocker::InitLocker(const char *name, bool benaphore)
185 {
186 	if (name == NULL)
187 		name = "some BLocker";
188 
189 	if (benaphore && !BLOCKER_ALWAYS_SEMAPHORE_STYLE) {
190 		// Because this is a benaphore, initialize the benaphore count and
191 		// create the semaphore.  Because this is a benaphore, the semaphore
192 		// count starts at 0 (ie acquired).
193 		fBenaphoreCount = 0;
194 		fSemaphoreID = create_sem(0, name);
195 	} else {
196 		// Because this is a semaphore, initialize the benaphore count to -1
197 		// and create the semaphore.  Because this is semaphore style, the
198 		// semaphore count starts at 1 so that one thread can acquire it and
199 		// the next thread to acquire it will block.
200 		fBenaphoreCount = 1;
201 		fSemaphoreID = create_sem(1, name);
202 	}
203 
204 	// The lock is currently not acquired so there is no owner.
205 	fLockOwner = B_ERROR;
206 
207 	// The lock is currently not acquired so the recursive count is zero.
208 	fRecursiveCount = 0;
209 }
210 
211 
212 bool
213 BLocker::AcquireLock(bigtime_t timeout, status_t *error)
214 {
215 	// By default, return no error.
216 	status_t status = B_OK;
217 
218 	// Only try to acquire the lock if the thread doesn't already own it.
219 	if (!IsLocked()) {
220 		// Increment the benaphore count and test to see if it was already greater
221 		// than 0.  If it is greater than 0, then some thread already has the
222 		// benaphore or the style is a semaphore.  Either way, we need to acquire
223 		// the semaphore in this case.
224 		int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, 1);
225 		if (oldBenaphoreCount > 0) {
226 			do {
227 				status = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_TIMEOUT,
228 					timeout);
229 			} while (status == B_INTERRUPTED);
230 
231 			// Note, if the lock here does time out, the benaphore count
232 			// is not decremented.  By doing this, the benaphore count will
233 			// never go back to zero.  This means that the locking essentially
234 			// changes to semaphore style if this was a benaphore.
235 			//
236 			// Doing the decrement of the benaphore count when the acquisition
237 			// fails is a risky thing to do.  If you decrement the counter at
238 			// the same time the thread which holds the benaphore does an
239 			// Unlock(), there is serious risk of a race condition.
240 			//
241 			// If the Unlock() sees a positive count and releases the semaphore
242 			// and then the timed out thread decrements the count to 0, there
243 			// is no one to take the semaphore.  The next two threads will be
244 			// able to acquire the benaphore at the same time!  The first will
245 			// increment the counter and acquire the lock.  The second will
246 			// acquire the semaphore and therefore the lock.  Not good.
247 			//
248 			// This has been discussed on the becodetalk mailing list and
249 			// Trey from Be had this to say:
250 			//
251 			// I looked at the LockWithTimeout() code, and it does not have
252 			// _this_ (ie the race condition) problem.  It circumvents it by
253 			// NOT doing the atomic_add(&count, -1) if the semaphore
254 			// acquisition fails.  This means that if a
255 			// BLocker::LockWithTimeout() times out, all other Lock*() attempts
256 			// turn into guaranteed semaphore grabs, _with_ the overhead of a
257 			// (now) useless atomic_add().
258 			//
259 			// Given Trey's comments, it looks like Be took the same approach
260 			// I did.  The output of CountLockRequests() of Be's implementation
261 			// confirms Trey's comments also.
262 			//
263 			// Finally some thoughts for the future with this code:
264 			//   - If 2^31 timeouts occur on a 32-bit machine (ie today),
265 			//     the benaphore count will wrap to a negative number.  This
266 			//     would have unknown consequences on the ability of the BLocker
267 			//     to continue to function.
268 			//
269 		}
270 	}
271 
272 	// If the lock has successfully been acquired.
273 	if (status == B_OK) {
274 		// Set the lock owner to this thread and increment the recursive count
275 		// by one.  The recursive count is incremented because one more Unlock()
276 		// is now required to release the lock (ie, 0 => 1, 1 => 2 etc).
277 		fLockOwner = find_thread(NULL);
278 		fRecursiveCount++;
279 	}
280 
281 	if (error != NULL)
282 		*error = status;
283 
284 	// Return true if the lock has been acquired.
285 	return (status == B_OK);
286 }
287