2007-04-02 15:39:10 +04:00
|
|
|
/*
|
2014-06-25 03:25:43 +04:00
|
|
|
* Copyright 2007-2014 Haiku, Inc. All rights reserved.
|
2007-04-02 15:39:10 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
2013-02-07 06:05:00 +04:00
|
|
|
* Authors:
|
|
|
|
* Niels Sascha Reedijk, niels.reedijk@gmail.com
|
2014-06-25 03:25:43 +04:00
|
|
|
* John Scipione, jscipione@gmail.com
|
2013-02-07 06:05:00 +04:00
|
|
|
*
|
2007-04-02 15:39:10 +04:00
|
|
|
* Corresponds to:
|
2013-09-15 13:26:54 +04:00
|
|
|
* headers/os/support/Locker.h rev 36218
|
|
|
|
* src/kits/support/Locker.cpp rev 32758
|
2007-04-02 15:39:10 +04:00
|
|
|
*/
|
|
|
|
|
2014-06-25 03:25:43 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\file Locker.h
|
|
|
|
\ingroup support
|
2013-02-08 00:04:05 +04:00
|
|
|
\ingroup libbe
|
2013-02-07 06:05:00 +04:00
|
|
|
\brief Provides locking class BLocker.
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\class BLocker
|
|
|
|
\ingroup support
|
2013-02-08 00:04:05 +04:00
|
|
|
\ingroup libbe
|
2013-02-07 06:05:00 +04:00
|
|
|
\brief Semaphore-type class for thread safety.
|
|
|
|
|
|
|
|
The BLocker interface is not merely a wrapper around a semaphore, but it
|
2014-06-25 03:25:43 +04:00
|
|
|
also has two advantages. First of all, it implements a benaphore.
|
2013-02-07 06:05:00 +04:00
|
|
|
A benaphore is in some ways more speed efficient,
|
|
|
|
because before it uses the internal semaphore, it first checks against a
|
|
|
|
variable that is only operated on with atomic operations. Setting a variable
|
|
|
|
is a lot more efficient than acquiring a semaphore, thus this type of locking
|
2013-02-07 23:19:09 +04:00
|
|
|
is much preferred.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
It basically works as follows. Whenever you newly created BLocker object
|
2013-02-07 23:19:09 +04:00
|
|
|
receives a locking request, it atomically sets the benaphore variable to
|
2013-02-07 06:05:00 +04:00
|
|
|
\c 1. Then only additional calls from different threads will utilize the
|
|
|
|
semaphore. You can imagine that in many cases where you protect
|
|
|
|
of data that \em might be accessed by two or more concurrent threads, but
|
|
|
|
the chances of it happening being very small, the benaphore benefits the
|
2014-06-25 03:25:43 +04:00
|
|
|
most from it's speed.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
The other feature of BLocker that improves basic semaphore handling is that
|
|
|
|
it allows for recursive locks. The following piece of code works with a
|
|
|
|
BLocker, but block inevitably with a semaphore. Let's pretend I call
|
2013-02-07 23:19:09 +04:00
|
|
|
\c Water():
|
2007-04-02 15:39:10 +04:00
|
|
|
|
2013-02-07 06:05:00 +04:00
|
|
|
\code
|
|
|
|
status_t
|
|
|
|
Flower::Grow(int length)
|
|
|
|
{
|
2007-04-02 15:39:10 +04:00
|
|
|
if (fLock->Lock()) {
|
2013-02-07 06:05:00 +04:00
|
|
|
fLength += length;
|
|
|
|
fLock->Unlock();
|
|
|
|
return B_OK;
|
2014-06-25 03:25:43 +04:00
|
|
|
} else
|
2013-02-07 06:05:00 +04:00
|
|
|
return B_ERROR;
|
|
|
|
}
|
2007-04-02 15:39:10 +04:00
|
|
|
|
2013-02-07 06:05:00 +04:00
|
|
|
status_t
|
|
|
|
Flower::Water(int amount)
|
|
|
|
{
|
2007-04-02 15:39:10 +04:00
|
|
|
if (fLock->Lock()) {
|
2013-02-07 06:05:00 +04:00
|
|
|
status_t status = Grow(amount * 2);
|
|
|
|
fLock->Unlock();
|
|
|
|
return status;
|
2014-06-25 03:25:43 +04:00
|
|
|
} else
|
2013-02-07 06:05:00 +04:00
|
|
|
return B_ERROR;
|
|
|
|
}
|
|
|
|
\endcode
|
2007-03-30 17:50:00 +04:00
|
|
|
|
2013-02-07 06:05:00 +04:00
|
|
|
This code would work because BLocker keeps track of the amount of lock
|
|
|
|
requests from the same thread. A normal semaphore would block in \c Grow()
|
|
|
|
because the semaphore would be acquired already. Please do make sure you
|
|
|
|
pair every Lock() with an Unlock() though, or you'll create a deadlock.
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\sa BAutolock
|
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn BLocker::BLocker()
|
2014-06-25 03:25:43 +04:00
|
|
|
\brief Create a new BLocker with the default name "some BLocker" and
|
|
|
|
benaphore-style locking.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
2014-06-25 03:25:43 +04:00
|
|
|
This BLocker will use the benaphore-style locking.
|
2007-04-02 15:39:10 +04:00
|
|
|
|
2013-02-07 06:05:00 +04:00
|
|
|
\note For debugging purposes, it's extremely convenient to actually give a
|
2014-06-25 03:25:43 +04:00
|
|
|
name to the object. In case of a deadlock, it's easier to track down
|
|
|
|
which BLocker object might have caused the problems.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
2014-06-25 03:25:43 +04:00
|
|
|
\see BLocker(const char*, bool) for all the options.
|
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn BLocker::BLocker(const char* name)
|
2014-06-25 03:25:43 +04:00
|
|
|
\brief Creates a new BLocker with the given \a name and benaphore-style
|
|
|
|
locking.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
\param name A NULL-terminated string that contains the name of the
|
2014-06-25 03:25:43 +04:00
|
|
|
semaphore. Note that the length of the names are limited to
|
|
|
|
\c B_OS_NAME_LENGTH constant, which includes the \c \\0
|
|
|
|
character.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
\see BLocker(const char* name, bool benaphoreStyle) for all the options.
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn BLocker::BLocker(bool benaphoreStyle)
|
2014-06-25 03:25:43 +04:00
|
|
|
\brief Creates a BLocker with the default name "some BLocker" and the given
|
|
|
|
locking style.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
\note For debugging purposes, it's extremely convenient to actually give a
|
2014-06-25 03:25:43 +04:00
|
|
|
name to the object. In case of a deadlock, it's easier to track down
|
|
|
|
which BLocker object might have caused the problems.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
\param benaphoreStyle If you pass \c true, the locker will be in benaphore
|
2014-06-25 03:25:43 +04:00
|
|
|
style (which is the default option for other constructors). If you
|
|
|
|
pass \c false, the object will completely rely on semaphores for
|
|
|
|
it's functioning.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
\see BLocker(const char* name, bool benaphoreStyle) if you also want
|
2014-06-25 03:25:43 +04:00
|
|
|
to set a name.
|
|
|
|
|
|
|
|
\since BeOS R4
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn BLocker::BLocker(const char* name, bool benaphoreStyle)
|
2014-06-25 03:25:43 +04:00
|
|
|
\brief Creates a new BLocker with the given \a name and locking style.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
\param name A NULL-terminated string that contains the name of the
|
2014-06-25 03:25:43 +04:00
|
|
|
semaphore. Note that the length of the names are limited to
|
|
|
|
\c B_OS_NAME_LENGTH constant, which includes the \c \\0
|
|
|
|
character.
|
2013-02-07 06:05:00 +04:00
|
|
|
\param benaphoreStyle If you pass \c true, the locker will be in benaphore
|
2014-06-25 03:25:43 +04:00
|
|
|
style (which is the default option for other constructors). If
|
|
|
|
you pass \c false, the object will completely rely on semaphores
|
|
|
|
for its functioning.
|
|
|
|
|
|
|
|
\since BeOS R4
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn virtual BLocker::~BLocker()
|
|
|
|
\brief Destructor.
|
|
|
|
|
|
|
|
Release the internal semaphore. Because of this, any pending Lock() calls
|
|
|
|
from other threads be cancelled. The return code will be \c false for
|
2013-02-07 23:19:09 +04:00
|
|
|
those calls.
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2013-09-15 13:26:54 +04:00
|
|
|
/*!
|
2013-10-04 18:52:49 +04:00
|
|
|
\fn status_t BLocker::InitCheck() const
|
2013-09-15 13:26:54 +04:00
|
|
|
\brief Check whether the locker has properly initialized
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\return A status code, \c B_OK if the semaphore has been properly
|
|
|
|
initialized or any other error (negative) value related
|
|
|
|
to semaphore initialization.
|
|
|
|
|
|
|
|
\since Haiku R1
|
2013-09-15 13:26:54 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn bool BLocker::Lock()
|
|
|
|
\brief Add a lock request and block on it until we get it.
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
\retval true Lock acquired successfully.
|
2013-02-07 06:05:00 +04:00
|
|
|
\retval false Failed to acquire the lock. Most probable cause is that the
|
2014-06-25 03:25:43 +04:00
|
|
|
object is deleted. This frees the semaphore and releases the
|
|
|
|
pending Lock() requests.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
\see LockWithTimeout(bigtime_t timeout), Unlock()
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn status_t BLocker::LockWithTimeout(bigtime_t timeout)
|
|
|
|
\brief Add a lock request and block until we get it or until it times out.
|
|
|
|
|
|
|
|
\param timeout This is a timeout in microseconds (one millionth of a
|
2014-06-25 03:25:43 +04:00
|
|
|
second) relative to now.
|
2013-02-07 06:05:00 +04:00
|
|
|
|
|
|
|
\see Lock(), Unlock()
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn void BLocker::Unlock(void)
|
|
|
|
\brief Release the lock that's currently held.
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn thread_id BLocker::LockingThread(void) const
|
|
|
|
\brief Return the \c thread_id of the thread that's currently holding the
|
2014-06-25 03:25:43 +04:00
|
|
|
lock.
|
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn bool BLocker::IsLocked(void) const
|
|
|
|
\brief Check if the calling thread is actually holding the lock.
|
|
|
|
|
2014-06-25 03:25:43 +04:00
|
|
|
\return Whether or not the calling thread is holding the lock.
|
2013-02-07 06:05:00 +04:00
|
|
|
\retval true The thread from which this method is called from is currently
|
2014-06-25 03:25:43 +04:00
|
|
|
holding the lock.
|
2013-02-07 06:05:00 +04:00
|
|
|
\retval false The object is unlocked or the lock is held by another thread.
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn int32 BLocker::CountLocks(void) const
|
|
|
|
\brief Return the number of recursive locks that are currently held.
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\return the number of currently held recursive locks as an int32.
|
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2014-06-25 03:25:43 +04:00
|
|
|
\fn int32 BLocker::CountLockRequests(void) const
|
2013-02-07 06:05:00 +04:00
|
|
|
\brief Return the number of threads with a pending lock request.
|
2014-06-25 03:25:43 +04:00
|
|
|
|
|
|
|
\return The number of threads with a pending lock request as an int32.
|
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-07 23:19:09 +04:00
|
|
|
|
2007-03-30 17:50:00 +04:00
|
|
|
/*!
|
2013-02-07 06:05:00 +04:00
|
|
|
\fn sem_id BLocker::Sem(void) const
|
|
|
|
\brief Return the sem_id of the semaphore this object holds.
|
|
|
|
|
|
|
|
\warning Like any other internal objects that the Haiku API might expose,
|
2014-06-25 03:25:43 +04:00
|
|
|
this semaphore id should in general be left alone. You should not
|
|
|
|
use any of the public low-level semaphore functions on this
|
|
|
|
semaphore, because it will harm the internal consistency of the
|
|
|
|
object.
|
|
|
|
|
|
|
|
\return The sem_id of the semaphore this object holds.
|
|
|
|
|
|
|
|
\since BeOS R3
|
2007-03-30 17:50:00 +04:00
|
|
|
*/
|