bool hsGlobalSemaphore::Wait(hsMilliseconds timeToWait) { #ifdef USE_SEMA // SHOULDN'T THIS USE timeToWait??!?!? -rje // shouldn't this use sem_timedwait? -dpogue (2012-03-04) hsAssert( timeToWait==kPosInfinity32, "sem_t does not support wait with timeout. #undef USE_SEMA and recompile." ); int status = sem_wait(fPSema); hsThrowIfOSErr(status); return true; #else bool retVal = true; int status = ::pthread_mutex_lock(&fPMutex); hsThrowIfOSErr(status); if (timeToWait == kPosInfinity32) { while (fCounter == 0) { status = ::pthread_cond_wait(&fPCond, &fPMutex); hsThrowIfOSErr(status); } } else { timespec spec; int result; result = ::clock_gettime(CLOCK_REALTIME, &spec); hsThrowIfFalse(result == 0); spec.tv_sec += timeToWait / 1000; spec.tv_nsec += (timeToWait % 1000) * 1000 * 1000; while (spec.tv_nsec >= 1000 * 1000 * 1000) { spec.tv_sec += 1; spec.tv_nsec -= 1000 * 1000 * 1000; } while (fCounter == 0) { status = ::pthread_cond_timedwait(&fPCond, &fPMutex, &spec); if (status == ETIMEDOUT) { retVal = false; goto EXIT; } hsThrowIfOSErr(status); } } hsAssert(fCounter > 0, "oops"); fCounter -= 1; EXIT: status = ::pthread_mutex_unlock(&fPMutex); hsThrowIfOSErr(status); return retVal; #endif }
bool hsGlobalSemaphore::Wait(hsMilliseconds timeToWait) { if (timeToWait == kPosInfinity32) timeToWait = INFINITE; DWORD result =::WaitForSingleObject(fSemaH, timeToWait); if (result == WAIT_OBJECT_0) return true; else { hsThrowIfFalse(result == WAIT_TIMEOUT); return false; } }
bool hsEvent::Wait(hsMilliseconds timeToWait) { if (timeToWait == kPosInfinity32) timeToWait = INFINITE; DWORD result =::WaitForSingleObject(fEvent, timeToWait); if (result == WAIT_OBJECT_0) { ::ResetEvent(fEvent); return true; } else { hsThrowIfFalse(result == WAIT_TIMEOUT); return false; } }