Some style changes I forgot to commit.

Also, BLocker now accepts NULL as a name and won't crash anymore in that case
("some BLocker" is then chosen as name).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13826 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-07-25 23:29:44 +00:00
parent 35a71b1936
commit 8e326c9cc1
+23 -47
View File
@@ -1,31 +1,12 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright (c) 2001-2005, Haiku, Inc.
// * Distributed under the terms of the MIT license.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Author: Erik Jaesler <[email protected]>
// to deal in the Software without restriction, including without limitation */
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the /** Semaphore-type class for thread safety */
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: Locker.cpp
// Author(s): Erik Jaesler <[email protected]>
//
// Description: Semaphore-type class for thread safety
//
//
//------------------------------------------------------------------------------
#include <OS.h> #include <OS.h>
#include <Locker.h> #include <Locker.h>
@@ -67,7 +48,7 @@
BLocker::BLocker() BLocker::BLocker()
{ {
InitLocker("some BLocker", true); InitLocker(NULL, true);
} }
@@ -77,16 +58,15 @@ BLocker::BLocker(const char *name)
} }
BLocker::BLocker(bool benaphore_style) BLocker::BLocker(bool benaphoreStyle)
{ {
InitLocker("some BLocker", benaphore_style); InitLocker(NULL, benaphoreStyle);
} }
BLocker::BLocker(const char *name, BLocker::BLocker(const char *name, bool benaphoreStyle)
bool benaphore_style)
{ {
InitLocker(name, benaphore_style); InitLocker(name, benaphoreStyle);
} }
@@ -95,11 +75,10 @@ BLocker::BLocker(const char *name,
// now. In Be's headers, its called "for_IPC". DO NOT USE THIS // now. In Be's headers, its called "for_IPC". DO NOT USE THIS
// CONSTRUCTOR! // CONSTRUCTOR!
// //
BLocker::BLocker(const char *name, BLocker::BLocker(const char *name, bool benaphoreStyle,
bool benaphore_style,
bool) bool)
{ {
InitLocker(name, benaphore_style); InitLocker(name, benaphoreStyle);
} }
@@ -117,8 +96,7 @@ bool
BLocker::Lock(void) BLocker::Lock(void)
{ {
status_t result; status_t result;
return AcquireLock(B_INFINITE_TIMEOUT, &result);
return (AcquireLock(B_INFINITE_TIMEOUT, &result));
} }
@@ -138,7 +116,6 @@ BLocker::Unlock(void)
{ {
// If the thread currently holds the lockdecrement // If the thread currently holds the lockdecrement
if (IsLocked()) { if (IsLocked()) {
// Decrement the number of outstanding locks this thread holds // Decrement the number of outstanding locks this thread holds
// on this BLocker. // on this BLocker.
fRecursiveCount--; fRecursiveCount--;
@@ -146,7 +123,6 @@ BLocker::Unlock(void)
// If the recursive count is now at 0, that means the BLocker has // If the recursive count is now at 0, that means the BLocker has
// been released by the thread. // been released by the thread.
if (fRecursiveCount == 0) { if (fRecursiveCount == 0) {
// The BLocker is no longer owned by any thread. // The BLocker is no longer owned by any thread.
fLockOwner = B_ERROR; fLockOwner = B_ERROR;
@@ -158,7 +134,6 @@ BLocker::Unlock(void)
// at lease one thread waiting for the lock in the case of a // at lease one thread waiting for the lock in the case of a
// benaphore. // benaphore.
if (oldBenaphoreCount > 1) { if (oldBenaphoreCount > 1) {
// Since there are threads waiting for the lock, it must // Since there are threads waiting for the lock, it must
// be released. Note, the old benaphore count will always be // be released. Note, the old benaphore count will always be
// greater than 1 for a semaphore so the release is always done. // greater than 1 for a semaphore so the release is always done.
@@ -182,7 +157,7 @@ BLocker::IsLocked(void) const
// This member returns true if the calling thread holds the lock. // This member returns true if the calling thread holds the lock.
// The easiest way to determine this is to compare the result of // The easiest way to determine this is to compare the result of
// find_thread() to the fLockOwner. // find_thread() to the fLockOwner.
return (find_thread(NULL) == fLockOwner); return find_thread(NULL) == fLockOwner;
} }
@@ -208,9 +183,11 @@ BLocker::Sem(void) const
void void
BLocker::InitLocker(const char *name, BLocker::InitLocker(const char *name, bool benaphore)
bool benaphore)
{ {
if (name == NULL)
name = "some BLocker";
if (benaphore) { if (benaphore) {
// Because this is a benaphore, initialize the benaphore count and // Because this is a benaphore, initialize the benaphore count and
// create the semaphore. Because this is a benaphore, the semaphore // create the semaphore. Because this is a benaphore, the semaphore
@@ -242,7 +219,6 @@ BLocker::AcquireLock(bigtime_t timeout, status_t *error)
// Only try to acquire the lock if the thread doesn't already own it. // Only try to acquire the lock if the thread doesn't already own it.
if (!IsLocked()) { if (!IsLocked()) {
// Increment the benaphore count and test to see if it was already greater // 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 // 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 // benaphore or the style is a semaphore. Either way, we need to acquire
@@ -253,6 +229,7 @@ BLocker::AcquireLock(bigtime_t timeout, status_t *error)
status = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_TIMEOUT, status = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_TIMEOUT,
timeout); timeout);
} while (status == B_INTERRUPTED); } while (status == B_INTERRUPTED);
// Note, if the lock here does time out, the benaphore count // Note, if the lock here does time out, the benaphore count
// is not decremented. By doing this, the benaphore count will // is not decremented. By doing this, the benaphore count will
// never go back to zero. This means that the locking essentially // never go back to zero. This means that the locking essentially
@@ -296,7 +273,6 @@ BLocker::AcquireLock(bigtime_t timeout, status_t *error)
// If the lock has successfully been acquired. // If the lock has successfully been acquired.
if (status == B_OK) { if (status == B_OK) {
// Set the lock owner to this thread and increment the recursive count // Set the lock owner to this thread and increment the recursive count
// by one. The recursive count is incremented because one more Unlock() // by one. The recursive count is incremented because one more Unlock()
// is now required to release the lock (ie, 0 => 1, 1 => 2 etc). // is now required to release the lock (ie, 0 => 1, 1 => 2 etc).