18e326c9cSAxel Dörfler /*
229e8fa59SJohn Scipione * Copyright 2001-2009 Haiku, Inc. All rights reserved.
38e326c9cSAxel Dörfler * Distributed under the terms of the MIT license.
48e326c9cSAxel Dörfler *
56f6dceacSAxel Dörfler * Authors:
629e8fa59SJohn Scipione * Erik Jaesler, erik@cgsoftware.com
78e326c9cSAxel Dörfler */
88e326c9cSAxel Dörfler
96f6dceacSAxel Dörfler
106f6dceacSAxel Dörfler /*! Semaphore-type class for thread safety */
118e326c9cSAxel Dörfler
1252a38012Sejakowatz
1352a38012Sejakowatz #include <OS.h>
1420b628d3SStefano Ceccherini #include <Locker.h>
1552a38012Sejakowatz #include <SupportDefs.h>
1652a38012Sejakowatz
17*7c52cca9SAdrien Destugues #include <stdio.h>
18*7c52cca9SAdrien Destugues
190970b97bSIngo Weinhold #include "support_kit_config.h"
2052a38012Sejakowatz
216f6dceacSAxel Dörfler
2252a38012Sejakowatz // Data Member Documentation:
2352a38012Sejakowatz //
2452a38012Sejakowatz // The "fBenaphoreCount" member is set to 1 if the BLocker style is
2552a38012Sejakowatz // semaphore. If the style is benaphore, it is initialized to 0 and
2652a38012Sejakowatz // is incremented atomically when it is acquired, decremented when it
2752a38012Sejakowatz // is released. By setting the benaphore count to 1 when the style is
2852a38012Sejakowatz // semaphore, the benaphore effectively becomes a semaphore. I was able
2952a38012Sejakowatz // to determine this is what Be's implementation does by testing the
3052a38012Sejakowatz // result of the CountLockRequests() member.
3152a38012Sejakowatz //
3252a38012Sejakowatz // The "fSemaphoreID" member holds the sem_id returned from create_sem()
3352a38012Sejakowatz // when the BLocker is constructed. It is used to acquire and release
3452a38012Sejakowatz // the lock regardless of the lock style (semaphore or benaphore).
3552a38012Sejakowatz //
3652a38012Sejakowatz // The "fLockOwner" member holds the thread_id of the thread which
3752a38012Sejakowatz // currently holds the lock. If no thread holds the lock, it is set to
3852a38012Sejakowatz // B_ERROR.
3952a38012Sejakowatz //
4052a38012Sejakowatz // The "fRecursiveCount" member holds a count of the number of times the
4152a38012Sejakowatz // thread holding the lock has acquired the lock without a matching unlock.
4252a38012Sejakowatz // It is basically the number of times the thread must call Unlock() before
4352a38012Sejakowatz // the lock can be acquired by a different thread.
4452a38012Sejakowatz //
4552a38012Sejakowatz
4652a38012Sejakowatz
BLocker()4752a38012Sejakowatz BLocker::BLocker()
4852a38012Sejakowatz {
498e326c9cSAxel Dörfler InitLocker(NULL, true);
5052a38012Sejakowatz }
5152a38012Sejakowatz
5252a38012Sejakowatz
BLocker(const char * name)5352a38012Sejakowatz BLocker::BLocker(const char *name)
5452a38012Sejakowatz {
5552a38012Sejakowatz InitLocker(name, true);
5652a38012Sejakowatz }
5752a38012Sejakowatz
5852a38012Sejakowatz
BLocker(bool benaphoreStyle)598e326c9cSAxel Dörfler BLocker::BLocker(bool benaphoreStyle)
6052a38012Sejakowatz {
618e326c9cSAxel Dörfler InitLocker(NULL, benaphoreStyle);
6252a38012Sejakowatz }
6352a38012Sejakowatz
6452a38012Sejakowatz
BLocker(const char * name,bool benaphoreStyle)658e326c9cSAxel Dörfler BLocker::BLocker(const char *name, bool benaphoreStyle)
6652a38012Sejakowatz {
678e326c9cSAxel Dörfler InitLocker(name, benaphoreStyle);
6852a38012Sejakowatz }
6952a38012Sejakowatz
7052a38012Sejakowatz
716f6dceacSAxel Dörfler /*! This constructor is not documented. The final argument is ignored for
726f6dceacSAxel Dörfler now. In Be's headers, its called "for_IPC". DO NOT USE THIS
736f6dceacSAxel Dörfler CONSTRUCTOR!
746f6dceacSAxel Dörfler */
BLocker(const char * name,bool benaphoreStyle,bool)758e326c9cSAxel Dörfler BLocker::BLocker(const char *name, bool benaphoreStyle,
7652a38012Sejakowatz bool)
7752a38012Sejakowatz {
788e326c9cSAxel Dörfler InitLocker(name, benaphoreStyle);
7952a38012Sejakowatz }
8052a38012Sejakowatz
8152a38012Sejakowatz
~BLocker()8252a38012Sejakowatz BLocker::~BLocker()
8352a38012Sejakowatz {
8452a38012Sejakowatz delete_sem(fSemaphoreID);
8552a38012Sejakowatz }
8652a38012Sejakowatz
8752a38012Sejakowatz
88f451e14bSIngo Weinhold status_t
InitCheck() const89f451e14bSIngo Weinhold BLocker::InitCheck() const
90f451e14bSIngo Weinhold {
91f451e14bSIngo Weinhold return fSemaphoreID >= 0 ? B_OK : fSemaphoreID;
92f451e14bSIngo Weinhold }
93f451e14bSIngo Weinhold
94f451e14bSIngo Weinhold
9552a38012Sejakowatz bool
Lock()966f6dceacSAxel Dörfler BLocker::Lock()
9752a38012Sejakowatz {
9852a38012Sejakowatz status_t result;
998e326c9cSAxel Dörfler return AcquireLock(B_INFINITE_TIMEOUT, &result);
10052a38012Sejakowatz }
10152a38012Sejakowatz
10252a38012Sejakowatz
10352a38012Sejakowatz status_t
LockWithTimeout(bigtime_t timeout)10452a38012Sejakowatz BLocker::LockWithTimeout(bigtime_t timeout)
10552a38012Sejakowatz {
10652a38012Sejakowatz status_t result;
10752a38012Sejakowatz
10852a38012Sejakowatz AcquireLock(timeout, &result);
10952a38012Sejakowatz return result;
11052a38012Sejakowatz }
11152a38012Sejakowatz
11252a38012Sejakowatz
11352a38012Sejakowatz void
Unlock()1146f6dceacSAxel Dörfler BLocker::Unlock()
11552a38012Sejakowatz {
11642274958SAdrien Destugues // The Be Book explicitly allows any thread, not just the lock owner, to
1175de22b9bSAdrien Destugues // unlock. This is bad practice, but we must allow it for compatibility
1185de22b9bSAdrien Destugues // reasons. We can at least warn the developer that something is probably
1195de22b9bSAdrien Destugues // wrong.
12044f58305SMichael Lotz if (!IsLocked()) {
12144f58305SMichael Lotz fprintf(stderr, "Unlocking BLocker with sem %" B_PRId32
12244f58305SMichael Lotz " from wrong thread %" B_PRId32 ", current holder %" B_PRId32
12344f58305SMichael Lotz " (see issue #6400).\n", fSemaphoreID, find_thread(NULL),
12444f58305SMichael Lotz fLockOwner);
12544f58305SMichael Lotz }
12642274958SAdrien Destugues
12752a38012Sejakowatz // Decrement the number of outstanding locks this thread holds
12852a38012Sejakowatz // on this BLocker.
12952a38012Sejakowatz fRecursiveCount--;
13052a38012Sejakowatz
13152a38012Sejakowatz // If the recursive count is now at 0, that means the BLocker has
13252a38012Sejakowatz // been released by the thread.
13352a38012Sejakowatz if (fRecursiveCount == 0) {
13452a38012Sejakowatz // The BLocker is no longer owned by any thread.
13552a38012Sejakowatz fLockOwner = B_ERROR;
13652a38012Sejakowatz
13752a38012Sejakowatz // Decrement the benaphore count and store the undecremented
13852a38012Sejakowatz // value in oldBenaphoreCount.
13952a38012Sejakowatz int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, -1);
14052a38012Sejakowatz
14152a38012Sejakowatz // If the oldBenaphoreCount is greater than 1, then there is
14244f58305SMichael Lotz // at least one thread waiting for the lock in the case of a
14352a38012Sejakowatz // benaphore.
14452a38012Sejakowatz if (oldBenaphoreCount > 1) {
14552a38012Sejakowatz // Since there are threads waiting for the lock, it must
14652a38012Sejakowatz // be released. Note, the old benaphore count will always be
14752a38012Sejakowatz // greater than 1 for a semaphore so the release is always done.
14852a38012Sejakowatz release_sem(fSemaphoreID);
14952a38012Sejakowatz }
15052a38012Sejakowatz }
15152a38012Sejakowatz }
15252a38012Sejakowatz
15352a38012Sejakowatz
15452a38012Sejakowatz thread_id
LockingThread() const1556f6dceacSAxel Dörfler BLocker::LockingThread() const
15652a38012Sejakowatz {
15752a38012Sejakowatz return fLockOwner;
15852a38012Sejakowatz }
15952a38012Sejakowatz
16052a38012Sejakowatz
16152a38012Sejakowatz bool
IsLocked() const1626f6dceacSAxel Dörfler BLocker::IsLocked() const
16352a38012Sejakowatz {
16452a38012Sejakowatz // This member returns true if the calling thread holds the lock.
16552a38012Sejakowatz // The easiest way to determine this is to compare the result of
16652a38012Sejakowatz // find_thread() to the fLockOwner.
1678e326c9cSAxel Dörfler return find_thread(NULL) == fLockOwner;
16852a38012Sejakowatz }
16952a38012Sejakowatz
17052a38012Sejakowatz
17152a38012Sejakowatz int32
CountLocks() const1726f6dceacSAxel Dörfler BLocker::CountLocks() const
17352a38012Sejakowatz {
17452a38012Sejakowatz return fRecursiveCount;
17552a38012Sejakowatz }
17652a38012Sejakowatz
17752a38012Sejakowatz
17852a38012Sejakowatz int32
CountLockRequests() const1796f6dceacSAxel Dörfler BLocker::CountLockRequests() const
18052a38012Sejakowatz {
18152a38012Sejakowatz return fBenaphoreCount;
18252a38012Sejakowatz }
18352a38012Sejakowatz
18452a38012Sejakowatz
18552a38012Sejakowatz sem_id
Sem() const1866f6dceacSAxel Dörfler BLocker::Sem() const
18752a38012Sejakowatz {
18852a38012Sejakowatz return fSemaphoreID;
18952a38012Sejakowatz }
19052a38012Sejakowatz
19152a38012Sejakowatz
19252a38012Sejakowatz void
InitLocker(const char * name,bool benaphore)1938e326c9cSAxel Dörfler BLocker::InitLocker(const char *name, bool benaphore)
19452a38012Sejakowatz {
1958e326c9cSAxel Dörfler if (name == NULL)
1968e326c9cSAxel Dörfler name = "some BLocker";
1978e326c9cSAxel Dörfler
1980970b97bSIngo Weinhold if (benaphore && !BLOCKER_ALWAYS_SEMAPHORE_STYLE) {
19952a38012Sejakowatz // Because this is a benaphore, initialize the benaphore count and
20052a38012Sejakowatz // create the semaphore. Because this is a benaphore, the semaphore
20152a38012Sejakowatz // count starts at 0 (ie acquired).
20252a38012Sejakowatz fBenaphoreCount = 0;
20352a38012Sejakowatz fSemaphoreID = create_sem(0, name);
20452a38012Sejakowatz } else {
20552a38012Sejakowatz // Because this is a semaphore, initialize the benaphore count to -1
20652a38012Sejakowatz // and create the semaphore. Because this is semaphore style, the
20752a38012Sejakowatz // semaphore count starts at 1 so that one thread can acquire it and
20852a38012Sejakowatz // the next thread to acquire it will block.
20952a38012Sejakowatz fBenaphoreCount = 1;
21052a38012Sejakowatz fSemaphoreID = create_sem(1, name);
21152a38012Sejakowatz }
21252a38012Sejakowatz
21352a38012Sejakowatz // The lock is currently not acquired so there is no owner.
21452a38012Sejakowatz fLockOwner = B_ERROR;
21552a38012Sejakowatz
21652a38012Sejakowatz // The lock is currently not acquired so the recursive count is zero.
21752a38012Sejakowatz fRecursiveCount = 0;
21852a38012Sejakowatz }
21952a38012Sejakowatz
22052a38012Sejakowatz
22152a38012Sejakowatz bool
AcquireLock(bigtime_t timeout,status_t * error)22220b628d3SStefano Ceccherini BLocker::AcquireLock(bigtime_t timeout, status_t *error)
22352a38012Sejakowatz {
22452a38012Sejakowatz // By default, return no error.
22520b628d3SStefano Ceccherini status_t status = B_OK;
22652a38012Sejakowatz
22752a38012Sejakowatz // Only try to acquire the lock if the thread doesn't already own it.
22852a38012Sejakowatz if (!IsLocked()) {
22942274958SAdrien Destugues // Increment the benaphore count and test to see if it was already
23042274958SAdrien Destugues // greater than 0. If it is greater than 0, then some thread already has
23142274958SAdrien Destugues // the benaphore or the style is a semaphore. Either way, we need to
23242274958SAdrien Destugues // acquire the semaphore in this case.
23352a38012Sejakowatz int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, 1);
23452a38012Sejakowatz if (oldBenaphoreCount > 0) {
23520b628d3SStefano Ceccherini do {
23620b628d3SStefano Ceccherini status = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_TIMEOUT,
23752a38012Sejakowatz timeout);
23820b628d3SStefano Ceccherini } while (status == B_INTERRUPTED);
2398e326c9cSAxel Dörfler
24052a38012Sejakowatz // Note, if the lock here does time out, the benaphore count
24152a38012Sejakowatz // is not decremented. By doing this, the benaphore count will
24252a38012Sejakowatz // never go back to zero. This means that the locking essentially
24352a38012Sejakowatz // changes to semaphore style if this was a benaphore.
24452a38012Sejakowatz //
24552a38012Sejakowatz // Doing the decrement of the benaphore count when the acquisition
24652a38012Sejakowatz // fails is a risky thing to do. If you decrement the counter at
24752a38012Sejakowatz // the same time the thread which holds the benaphore does an
24852a38012Sejakowatz // Unlock(), there is serious risk of a race condition.
24952a38012Sejakowatz //
25052a38012Sejakowatz // If the Unlock() sees a positive count and releases the semaphore
25152a38012Sejakowatz // and then the timed out thread decrements the count to 0, there
25252a38012Sejakowatz // is no one to take the semaphore. The next two threads will be
25352a38012Sejakowatz // able to acquire the benaphore at the same time! The first will
25452a38012Sejakowatz // increment the counter and acquire the lock. The second will
25552a38012Sejakowatz // acquire the semaphore and therefore the lock. Not good.
25652a38012Sejakowatz //
25752a38012Sejakowatz // This has been discussed on the becodetalk mailing list and
25852a38012Sejakowatz // Trey from Be had this to say:
25952a38012Sejakowatz //
26052a38012Sejakowatz // I looked at the LockWithTimeout() code, and it does not have
26152a38012Sejakowatz // _this_ (ie the race condition) problem. It circumvents it by
26252a38012Sejakowatz // NOT doing the atomic_add(&count, -1) if the semaphore
26352a38012Sejakowatz // acquisition fails. This means that if a
26452a38012Sejakowatz // BLocker::LockWithTimeout() times out, all other Lock*() attempts
26552a38012Sejakowatz // turn into guaranteed semaphore grabs, _with_ the overhead of a
26652a38012Sejakowatz // (now) useless atomic_add().
26752a38012Sejakowatz //
26852a38012Sejakowatz // Given Trey's comments, it looks like Be took the same approach
26952a38012Sejakowatz // I did. The output of CountLockRequests() of Be's implementation
27052a38012Sejakowatz // confirms Trey's comments also.
27152a38012Sejakowatz //
27252a38012Sejakowatz // Finally some thoughts for the future with this code:
27352a38012Sejakowatz // - If 2^31 timeouts occur on a 32-bit machine (ie today),
27452a38012Sejakowatz // the benaphore count will wrap to a negative number. This
27552a38012Sejakowatz // would have unknown consequences on the ability of the BLocker
27652a38012Sejakowatz // to continue to function.
27752a38012Sejakowatz //
27852a38012Sejakowatz }
27952a38012Sejakowatz }
28052a38012Sejakowatz
28152a38012Sejakowatz // If the lock has successfully been acquired.
28220b628d3SStefano Ceccherini if (status == B_OK) {
28352a38012Sejakowatz // Set the lock owner to this thread and increment the recursive count
28452a38012Sejakowatz // by one. The recursive count is incremented because one more Unlock()
28552a38012Sejakowatz // is now required to release the lock (ie, 0 => 1, 1 => 2 etc).
2861cffe0dcSMichael Lotz if (fLockOwner < 0) {
28752a38012Sejakowatz fLockOwner = find_thread(NULL);
2881cffe0dcSMichael Lotz fRecursiveCount = 1;
2891cffe0dcSMichael Lotz } else
29052a38012Sejakowatz fRecursiveCount++;
29152a38012Sejakowatz }
29252a38012Sejakowatz
29320b628d3SStefano Ceccherini if (error != NULL)
29420b628d3SStefano Ceccherini *error = status;
29520b628d3SStefano Ceccherini
29652a38012Sejakowatz // Return true if the lock has been acquired.
29720b628d3SStefano Ceccherini return (status == B_OK);
29852a38012Sejakowatz }
299