diff --git a/Sources/Plasma/CoreLib/hsThread.h b/Sources/Plasma/CoreLib/hsThread.h index dc8418b7..e36d131b 100644 --- a/Sources/Plasma/CoreLib/hsThread.h +++ b/Sources/Plasma/CoreLib/hsThread.h @@ -157,6 +157,7 @@ public: hsSemaphore(int initialValue=0, const char* name=nil); ~hsSemaphore(); + hsBool TryWait(); hsBool Wait(hsMilliseconds timeToWait = kPosInfinity32); void Signal(); }; diff --git a/Sources/Plasma/CoreLib/hsThread_Unix.cpp b/Sources/Plasma/CoreLib/hsThread_Unix.cpp index 109492a3..f4952482 100644 --- a/Sources/Plasma/CoreLib/hsThread_Unix.cpp +++ b/Sources/Plasma/CoreLib/hsThread_Unix.cpp @@ -316,6 +316,18 @@ hsSemaphore::~hsSemaphore() #endif } +hsBool hsSemaphore::TryWait() +{ +#ifdef USE_SEMA + int status = ::sem_trywait(fPSema); + return status != E_AGAIN; +#else + int status = ::pthread_mutex_trylock(&fPMutex); + hsThrowIfOSErr(status); + return status==EBUSY ? false : true; +#endif +} + hsBool hsSemaphore::Wait(hsMilliseconds timeToWait) { #ifdef USE_SEMA // SHOULDN'T THIS USE timeToWait??!?!? -rje diff --git a/Sources/Plasma/CoreLib/hsThread_Win.cpp b/Sources/Plasma/CoreLib/hsThread_Win.cpp index 2cde7e9e..7cb6b9a9 100644 --- a/Sources/Plasma/CoreLib/hsThread_Win.cpp +++ b/Sources/Plasma/CoreLib/hsThread_Win.cpp @@ -170,6 +170,13 @@ hsSemaphore::~hsSemaphore() ::CloseHandle(fSemaH); } +hsBool hsSemaphore::TryWait() +{ + DWORD result = ::WaitForSingleObject(fSemaH, 0); + hsAssert(result != WAIT_ABANDONED, "hsSemaphore -> Abandoned Semaphore"); + return result == WAIT_OBJECT_0; +} + hsBool hsSemaphore::Wait(hsMilliseconds timeToWait) { if (timeToWait == kPosInfinity32)