From 20b628d310c6fc13957c59adc4fdb2ea26195fac Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Thu, 17 Jun 2004 08:10:36 +0000 Subject: [PATCH] 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 --- src/kits/support/Locker.cpp | 33 +++++-------- src/kits/support/StopWatch.cpp | 86 +++++++++++++++++++++------------- 2 files changed, 67 insertions(+), 52 deletions(-) diff --git a/src/kits/support/Locker.cpp b/src/kits/support/Locker.cpp index 2b06712ab2..d054cb6c21 100644 --- a/src/kits/support/Locker.cpp +++ b/src/kits/support/Locker.cpp @@ -27,16 +27,11 @@ // //------------------------------------------------------------------------------ -#include "Locker.h" #include +#include #include -#ifdef USE_OPENBEOS_NAMESPACE -namespace OpenBeOS { -#endif - - // // Data Member Documentation: // @@ -240,12 +235,11 @@ BLocker::InitLocker(const char *name, bool -BLocker::AcquireLock(bigtime_t timeout, - status_t *error) +BLocker::AcquireLock(bigtime_t timeout, status_t *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. if (!IsLocked()) { @@ -255,10 +249,11 @@ BLocker::AcquireLock(bigtime_t timeout, // the semaphore in this case. int32 oldBenaphoreCount = atomic_add(&fBenaphoreCount, 1); if (oldBenaphoreCount > 0) { - - *error = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_TIMEOUT, + do { + status = acquire_sem_etc(fSemaphoreID, 1, B_RELATIVE_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 // never go back to zero. This means that the locking essentially // 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 (*error == B_NO_ERROR) { + 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() @@ -309,11 +304,9 @@ BLocker::AcquireLock(bigtime_t timeout, fRecursiveCount++; } + if (error != NULL) + *error = status; + // Return true if the lock has been acquired. - return (*error == B_NO_ERROR); + return (status == B_OK); } - - -#ifdef USE_OPENBEOS_NAMESPACE -} -#endif diff --git a/src/kits/support/StopWatch.cpp b/src/kits/support/StopWatch.cpp index e53e3f80c1..c97239c0f4 100644 --- a/src/kits/support/StopWatch.cpp +++ b/src/kits/support/StopWatch.cpp @@ -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 // copy of this software and associated documentation files (the "Software"), @@ -22,80 +22,102 @@ // File Name: StopWatch.cpp // Author(s): unknown // -// Description: Timer class +// Description: Timer Class, mostly useful for debugging +// // // //------------------------------------------------------------------------------ + +#include // for system_time() #include -#include + #include -#ifdef USE_OPENBEOS_NAMESPACE -namespace OpenBeOS { -#endif -BStopWatch::BStopWatch(const char *name, bool silent){ - fSilent = silent; - fName = name; +BStopWatch::BStopWatch(const char *name, bool silent) + : + fName(name), + fSilent(silent) +{ Reset(); } -BStopWatch::~BStopWatch(){ + +BStopWatch::~BStopWatch() +{ if (!fSilent){ - printf("StopWatch \"%s\": %d usecs.", fName, (int)ElapsedTime() ); + printf("StopWatch \"%s\": %d usecs.", fName, (int)ElapsedTime()); - if (fLap){ - for (int i=1; i<=fLap; i++){ - if (!((i-1)%4)) printf("\n "); - printf("[%d: %d#%d] ", i, (int)(fLaps[i]-fStart), (int)(fLaps[i] - fLaps[i-1]) ); + if (fLap) { + for (int i = 1; i <= fLap; i++){ + if (!((i-1)%4)) + printf("\n "); + printf("[%d: %d#%d] ", i, (int)(fLaps[i]-fStart), (int)(fLaps[i] - fLaps[i -1 ])); } printf("\n"); } } } -void BStopWatch::Suspend(){ + +void +BStopWatch::Suspend() +{ if (!fSuspendTime) fSuspendTime = system_time(); } -void BStopWatch::Resume(){ + +void +BStopWatch::Resume() +{ if (fSuspendTime) fStart = system_time() - fSuspendTime - fStart; } -bigtime_t BStopWatch::Lap(){ + +bigtime_t +BStopWatch::Lap() +{ if (!fSuspendTime){ - if (fLap<9) fLap++; + if (fLap<9) + fLap++; fLaps[fLap] = system_time(); - return (system_time()-fStart); - }else + + return system_time() - fStart; + } else return 0; } -bigtime_t BStopWatch::ElapsedTime() const{ + +bigtime_t +BStopWatch::ElapsedTime() const +{ if (fSuspendTime) - return (fSuspendTime-fStart); + return fSuspendTime - fStart; else - return (system_time()-fStart); + return system_time() - fStart; } -void BStopWatch::Reset(){ + +void +BStopWatch::Reset() +{ fStart = system_time(); // store current time fSuspendTime = 0; fLap = 0; // clear laps - for (int i=0; i<10; i++) + for (int i = 0; i < 10; i++) fLaps[i] = fStart; } -const char *BStopWatch::Name() const{ - return fName; + +const char * +BStopWatch::Name() const +{ + return fName != NULL ? fName : ""; } + // just for future binary compatibility void BStopWatch::_ReservedStopWatch1() {} void BStopWatch::_ReservedStopWatch2() {} - -#ifdef USE_OPENBEOS_NAMESPACE -} // namespace OpenBeOS -#endif