Some cleanups for BStopWatch. Added the check for B_INTERRUPTED to the semaphore acquisition in BLocker

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@8019 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini 2004-06-17 08:10:36 +00:00
parent ca0e044be6
commit 20b628d310
2 changed files with 67 additions and 52 deletions

View File

@ -27,16 +27,11 @@
// //
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include "Locker.h"
#include <OS.h> #include <OS.h>
#include <Locker.h>
#include <SupportDefs.h> #include <SupportDefs.h>
#ifdef USE_OPENBEOS_NAMESPACE
namespace OpenBeOS {
#endif
// //
// Data Member Documentation: // Data Member Documentation:
// //
@ -240,12 +235,11 @@ BLocker::InitLocker(const char *name,
bool bool
BLocker::AcquireLock(bigtime_t timeout, BLocker::AcquireLock(bigtime_t timeout, status_t *error)
status_t *error)
{ {
// By default, return no error. // By default, return no error.
*error = B_NO_ERROR; status_t status = B_OK;
// 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()) {
@ -255,10 +249,11 @@ BLocker::AcquireLock(bigtime_t timeout,
// the semaphore in this case. // the semaphore in this case.
int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, 1); int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, 1);
if (oldBenaphoreCount > 0) { if (oldBenaphoreCount > 0) {
do {
*error = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_TIMEOUT, status = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_TIMEOUT,
timeout); timeout);
// Note, if the lock here does time out, the benaphore count } while (status == B_INTERRUPTED);
// 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
// changes to semaphore style if this was a benaphore. // changes to semaphore style if this was a benaphore.
@ -300,7 +295,7 @@ BLocker::AcquireLock(bigtime_t timeout,
} }
// If the lock has successfully been acquired. // If the lock has successfully been acquired.
if (*error == B_NO_ERROR) { 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()
@ -309,11 +304,9 @@ BLocker::AcquireLock(bigtime_t timeout,
fRecursiveCount++; fRecursiveCount++;
} }
if (error != NULL)
*error = status;
// Return true if the lock has been acquired. // Return true if the lock has been acquired.
return (*error == B_NO_ERROR); return (status == B_OK);
} }
#ifdef USE_OPENBEOS_NAMESPACE
}
#endif

View File

@ -1,5 +1,5 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2004, OpenBeOS
// //
// Permission is hereby granted, free of charge, to any person obtaining a // Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"), // copy of this software and associated documentation files (the "Software"),
@ -22,80 +22,102 @@
// File Name: StopWatch.cpp // File Name: StopWatch.cpp
// Author(s): unknown // Author(s): unknown
// //
// Description: Timer class // Description: Timer Class, mostly useful for debugging
//
// //
// //
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include <OS.h> // for system_time()
#include <StopWatch.h> #include <StopWatch.h>
#include <OS.h>
#include <stdio.h> #include <stdio.h>
#ifdef USE_OPENBEOS_NAMESPACE
namespace OpenBeOS {
#endif
BStopWatch::BStopWatch(const char *name, bool silent){ BStopWatch::BStopWatch(const char *name, bool silent)
fSilent = silent; :
fName = name; fName(name),
fSilent(silent)
{
Reset(); Reset();
} }
BStopWatch::~BStopWatch(){
BStopWatch::~BStopWatch()
{
if (!fSilent){ if (!fSilent){
printf("StopWatch \"%s\": %d usecs.", fName, (int)ElapsedTime() ); printf("StopWatch \"%s\": %d usecs.", fName, (int)ElapsedTime());
if (fLap){ if (fLap) {
for (int i=1; i<=fLap; i++){ for (int i = 1; i <= fLap; i++){
if (!((i-1)%4)) printf("\n "); if (!((i-1)%4))
printf("[%d: %d#%d] ", i, (int)(fLaps[i]-fStart), (int)(fLaps[i] - fLaps[i-1]) ); printf("\n ");
printf("[%d: %d#%d] ", i, (int)(fLaps[i]-fStart), (int)(fLaps[i] - fLaps[i -1 ]));
} }
printf("\n"); printf("\n");
} }
} }
} }
void BStopWatch::Suspend(){
void
BStopWatch::Suspend()
{
if (!fSuspendTime) if (!fSuspendTime)
fSuspendTime = system_time(); fSuspendTime = system_time();
} }
void BStopWatch::Resume(){
void
BStopWatch::Resume()
{
if (fSuspendTime) if (fSuspendTime)
fStart = system_time() - fSuspendTime - fStart; fStart = system_time() - fSuspendTime - fStart;
} }
bigtime_t BStopWatch::Lap(){
bigtime_t
BStopWatch::Lap()
{
if (!fSuspendTime){ if (!fSuspendTime){
if (fLap<9) fLap++; if (fLap<9)
fLap++;
fLaps[fLap] = system_time(); fLaps[fLap] = system_time();
return (system_time()-fStart);
}else return system_time() - fStart;
} else
return 0; return 0;
} }
bigtime_t BStopWatch::ElapsedTime() const{
bigtime_t
BStopWatch::ElapsedTime() const
{
if (fSuspendTime) if (fSuspendTime)
return (fSuspendTime-fStart); return fSuspendTime - fStart;
else else
return (system_time()-fStart); return system_time() - fStart;
} }
void BStopWatch::Reset(){
void
BStopWatch::Reset()
{
fStart = system_time(); // store current time fStart = system_time(); // store current time
fSuspendTime = 0; fSuspendTime = 0;
fLap = 0; // clear laps fLap = 0; // clear laps
for (int i=0; i<10; i++) for (int i = 0; i < 10; i++)
fLaps[i] = fStart; fLaps[i] = fStart;
} }
const char *BStopWatch::Name() const{
return fName; const char *
BStopWatch::Name() const
{
return fName != NULL ? fName : "";
} }
// just for future binary compatibility // just for future binary compatibility
void BStopWatch::_ReservedStopWatch1() {} void BStopWatch::_ReservedStopWatch1() {}
void BStopWatch::_ReservedStopWatch2() {} void BStopWatch::_ReservedStopWatch2() {}
#ifdef USE_OPENBEOS_NAMESPACE
} // namespace OpenBeOS
#endif