2005-07-26 03:29:44 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001-2005, Haiku, Inc.
|
|
|
|
* Distributed under the terms of the MIT license.
|
|
|
|
*
|
|
|
|
* Author: Erik Jaesler <erik@cgsoftware.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Semaphore-type class for thread safety */
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#include <OS.h>
|
2004-06-17 12:10:36 +04:00
|
|
|
#include <Locker.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
2008-04-22 19:04:01 +04:00
|
|
|
#include "support_kit_config.h"
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Data Member Documentation:
|
|
|
|
//
|
|
|
|
// The "fBenaphoreCount" member is set to 1 if the BLocker style is
|
|
|
|
// semaphore. If the style is benaphore, it is initialized to 0 and
|
|
|
|
// is incremented atomically when it is acquired, decremented when it
|
|
|
|
// is released. By setting the benaphore count to 1 when the style is
|
|
|
|
// semaphore, the benaphore effectively becomes a semaphore. I was able
|
|
|
|
// to determine this is what Be's implementation does by testing the
|
|
|
|
// result of the CountLockRequests() member.
|
|
|
|
//
|
|
|
|
// The "fSemaphoreID" member holds the sem_id returned from create_sem()
|
|
|
|
// when the BLocker is constructed. It is used to acquire and release
|
|
|
|
// the lock regardless of the lock style (semaphore or benaphore).
|
|
|
|
//
|
|
|
|
// The "fLockOwner" member holds the thread_id of the thread which
|
|
|
|
// currently holds the lock. If no thread holds the lock, it is set to
|
|
|
|
// B_ERROR.
|
|
|
|
//
|
|
|
|
// The "fRecursiveCount" member holds a count of the number of times the
|
|
|
|
// thread holding the lock has acquired the lock without a matching unlock.
|
|
|
|
// It is basically the number of times the thread must call Unlock() before
|
|
|
|
// the lock can be acquired by a different thread.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Constructors:
|
|
|
|
//
|
|
|
|
// All constructors just pass their arguments to InitLocker(). Note that
|
|
|
|
// the default for "name" is "some BLocker" and "benaphore_style" is true.
|
|
|
|
//
|
|
|
|
|
|
|
|
BLocker::BLocker()
|
|
|
|
{
|
2005-07-26 03:29:44 +04:00
|
|
|
InitLocker(NULL, true);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BLocker::BLocker(const char *name)
|
|
|
|
{
|
|
|
|
InitLocker(name, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-26 03:29:44 +04:00
|
|
|
BLocker::BLocker(bool benaphoreStyle)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-07-26 03:29:44 +04:00
|
|
|
InitLocker(NULL, benaphoreStyle);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-26 03:29:44 +04:00
|
|
|
BLocker::BLocker(const char *name, bool benaphoreStyle)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-07-26 03:29:44 +04:00
|
|
|
InitLocker(name, benaphoreStyle);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// This constructor is not documented. The final argument is ignored for
|
|
|
|
// now. In Be's headers, its called "for_IPC". DO NOT USE THIS
|
|
|
|
// CONSTRUCTOR!
|
|
|
|
//
|
2005-07-26 03:29:44 +04:00
|
|
|
BLocker::BLocker(const char *name, bool benaphoreStyle,
|
|
|
|
bool)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-07-26 03:29:44 +04:00
|
|
|
InitLocker(name, benaphoreStyle);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// The destructor just deletes the semaphore. By deleting the semaphore,
|
|
|
|
// any threads waiting to acquire the BLocker will be unblocked.
|
|
|
|
//
|
|
|
|
BLocker::~BLocker()
|
|
|
|
{
|
|
|
|
delete_sem(fSemaphoreID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
BLocker::Lock(void)
|
|
|
|
{
|
|
|
|
status_t result;
|
2005-07-26 03:29:44 +04:00
|
|
|
return AcquireLock(B_INFINITE_TIMEOUT, &result);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
BLocker::LockWithTimeout(bigtime_t timeout)
|
|
|
|
{
|
|
|
|
status_t result;
|
|
|
|
|
|
|
|
AcquireLock(timeout, &result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BLocker::Unlock(void)
|
|
|
|
{
|
|
|
|
// If the thread currently holds the lockdecrement
|
|
|
|
if (IsLocked()) {
|
|
|
|
// Decrement the number of outstanding locks this thread holds
|
|
|
|
// on this BLocker.
|
|
|
|
fRecursiveCount--;
|
2005-07-26 03:29:44 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// If the recursive count is now at 0, that means the BLocker has
|
|
|
|
// been released by the thread.
|
|
|
|
if (fRecursiveCount == 0) {
|
|
|
|
// The BLocker is no longer owned by any thread.
|
|
|
|
fLockOwner = B_ERROR;
|
2005-07-26 03:29:44 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// Decrement the benaphore count and store the undecremented
|
|
|
|
// value in oldBenaphoreCount.
|
|
|
|
int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, -1);
|
2005-07-26 03:29:44 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// If the oldBenaphoreCount is greater than 1, then there is
|
|
|
|
// at lease one thread waiting for the lock in the case of a
|
|
|
|
// benaphore.
|
|
|
|
if (oldBenaphoreCount > 1) {
|
|
|
|
// Since there are threads waiting for the lock, it must
|
|
|
|
// be released. Note, the old benaphore count will always be
|
|
|
|
// greater than 1 for a semaphore so the release is always done.
|
|
|
|
release_sem(fSemaphoreID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
thread_id
|
|
|
|
BLocker::LockingThread(void) const
|
|
|
|
{
|
|
|
|
return fLockOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
BLocker::IsLocked(void) const
|
|
|
|
{
|
|
|
|
// This member returns true if the calling thread holds the lock.
|
|
|
|
// The easiest way to determine this is to compare the result of
|
|
|
|
// find_thread() to the fLockOwner.
|
2005-07-26 03:29:44 +04:00
|
|
|
return find_thread(NULL) == fLockOwner;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32
|
|
|
|
BLocker::CountLocks(void) const
|
|
|
|
{
|
|
|
|
return fRecursiveCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32
|
|
|
|
BLocker::CountLockRequests(void) const
|
|
|
|
{
|
|
|
|
return fBenaphoreCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sem_id
|
|
|
|
BLocker::Sem(void) const
|
|
|
|
{
|
|
|
|
return fSemaphoreID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-07-26 03:29:44 +04:00
|
|
|
BLocker::InitLocker(const char *name, bool benaphore)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-07-26 03:29:44 +04:00
|
|
|
if (name == NULL)
|
|
|
|
name = "some BLocker";
|
|
|
|
|
2008-04-22 19:04:01 +04:00
|
|
|
if (benaphore && !BLOCKER_ALWAYS_SEMAPHORE_STYLE) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// Because this is a benaphore, initialize the benaphore count and
|
|
|
|
// create the semaphore. Because this is a benaphore, the semaphore
|
|
|
|
// count starts at 0 (ie acquired).
|
|
|
|
fBenaphoreCount = 0;
|
|
|
|
fSemaphoreID = create_sem(0, name);
|
|
|
|
} else {
|
|
|
|
// Because this is a semaphore, initialize the benaphore count to -1
|
|
|
|
// and create the semaphore. Because this is semaphore style, the
|
|
|
|
// semaphore count starts at 1 so that one thread can acquire it and
|
|
|
|
// the next thread to acquire it will block.
|
|
|
|
fBenaphoreCount = 1;
|
|
|
|
fSemaphoreID = create_sem(1, name);
|
|
|
|
}
|
2005-07-26 03:29:44 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// The lock is currently not acquired so there is no owner.
|
|
|
|
fLockOwner = B_ERROR;
|
|
|
|
|
|
|
|
// The lock is currently not acquired so the recursive count is zero.
|
|
|
|
fRecursiveCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2004-06-17 12:10:36 +04:00
|
|
|
BLocker::AcquireLock(bigtime_t timeout, status_t *error)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
// By default, return no error.
|
2004-06-17 12:10:36 +04:00
|
|
|
status_t status = B_OK;
|
2005-07-26 03:29:44 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// Only try to acquire the lock if the thread doesn't already own it.
|
2005-07-26 03:29:44 +04:00
|
|
|
if (!IsLocked()) {
|
|
|
|
// Increment the benaphore count and test to see if it was already greater
|
|
|
|
// than 0. If it is greater than 0, then some thread already has the
|
|
|
|
// benaphore or the style is a semaphore. Either way, we need to acquire
|
|
|
|
// the semaphore in this case.
|
|
|
|
int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, 1);
|
|
|
|
if (oldBenaphoreCount > 0) {
|
2004-06-17 12:10:36 +04:00
|
|
|
do {
|
2005-07-26 03:29:44 +04:00
|
|
|
status = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_TIMEOUT,
|
|
|
|
timeout);
|
2004-06-17 12:10:36 +04:00
|
|
|
} while (status == B_INTERRUPTED);
|
2005-07-26 03:29:44 +04:00
|
|
|
|
2004-06-17 12:10:36 +04:00
|
|
|
// Note, if the lock here does time out, the benaphore count
|
2005-07-26 03:29:44 +04:00
|
|
|
// is not decremented. By doing this, the benaphore count will
|
|
|
|
// never go back to zero. This means that the locking essentially
|
|
|
|
// changes to semaphore style if this was a benaphore.
|
|
|
|
//
|
|
|
|
// Doing the decrement of the benaphore count when the acquisition
|
|
|
|
// fails is a risky thing to do. If you decrement the counter at
|
|
|
|
// the same time the thread which holds the benaphore does an
|
|
|
|
// Unlock(), there is serious risk of a race condition.
|
|
|
|
//
|
|
|
|
// If the Unlock() sees a positive count and releases the semaphore
|
|
|
|
// and then the timed out thread decrements the count to 0, there
|
|
|
|
// is no one to take the semaphore. The next two threads will be
|
|
|
|
// able to acquire the benaphore at the same time! The first will
|
|
|
|
// increment the counter and acquire the lock. The second will
|
|
|
|
// acquire the semaphore and therefore the lock. Not good.
|
|
|
|
//
|
|
|
|
// This has been discussed on the becodetalk mailing list and
|
|
|
|
// Trey from Be had this to say:
|
|
|
|
//
|
|
|
|
// I looked at the LockWithTimeout() code, and it does not have
|
|
|
|
// _this_ (ie the race condition) problem. It circumvents it by
|
|
|
|
// NOT doing the atomic_add(&count, -1) if the semaphore
|
|
|
|
// acquisition fails. This means that if a
|
|
|
|
// BLocker::LockWithTimeout() times out, all other Lock*() attempts
|
|
|
|
// turn into guaranteed semaphore grabs, _with_ the overhead of a
|
|
|
|
// (now) useless atomic_add().
|
|
|
|
//
|
|
|
|
// Given Trey's comments, it looks like Be took the same approach
|
|
|
|
// I did. The output of CountLockRequests() of Be's implementation
|
|
|
|
// confirms Trey's comments also.
|
|
|
|
//
|
|
|
|
// Finally some thoughts for the future with this code:
|
|
|
|
// - If 2^31 timeouts occur on a 32-bit machine (ie today),
|
|
|
|
// the benaphore count will wrap to a negative number. This
|
|
|
|
// would have unknown consequences on the ability of the BLocker
|
|
|
|
// to continue to function.
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the lock has successfully been acquired.
|
|
|
|
if (status == B_OK) {
|
|
|
|
// Set the lock owner to this thread and increment the recursive count
|
|
|
|
// by one. The recursive count is incremented because one more Unlock()
|
|
|
|
// is now required to release the lock (ie, 0 => 1, 1 => 2 etc).
|
|
|
|
fLockOwner = find_thread(NULL);
|
|
|
|
fRecursiveCount++;
|
|
|
|
}
|
|
|
|
|
2004-06-17 12:10:36 +04:00
|
|
|
if (error != NULL)
|
|
|
|
*error = status;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-07-26 03:29:44 +04:00
|
|
|
// Return true if the lock has been acquired.
|
|
|
|
return (status == B_OK);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|