void Conditional::Wait(Mutex *mutex, Int microseconds) { //printf("Wait(%s) start\n", mName.GetCString()); #if defined(FastOSWindows) if (0 > microseconds) SleepConditionVariableCS(&mConditionVariable->cv, &mutex->GetCriticalSection()->cs, INFINITE); else SleepConditionVariableCS(&mConditionVariable->cv, &mutex->GetCriticalSection()->cs, microseconds / 1000); #elif defined(FastOSUnixLike) if (0 > microseconds) pthread_cond_wait(&mConditionVariable->cv, &mutex->GetCriticalSection()->cs); else { struct timespec timeToWait; struct timeval now; gettimeofday(&now, NULL); timeToWait.tv_sec = now.tv_sec + (microseconds / 1000000); timeToWait.tv_nsec = 1000 * (now.tv_usec + (microseconds - ((microseconds / 1000000) * 1000000))); pthread_cond_timedwait(&mConditionVariable->cv, &mutex->GetCriticalSection()->cs, &timeToWait); } #endif //printf("Wait(%s) end\n", mName.GetCString()); }
int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline) { int timeout = 0; DWORD timeout_max_ms; mu->locked = 0; if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) == 0) { SleepConditionVariableCS(cv, &mu->cs, INFINITE); } else { gpr_timespec now = gpr_now(abs_deadline.clock_type); int64_t now_ms = (int64_t)now.tv_sec * 1000 + now.tv_nsec / 1000000; int64_t deadline_ms = (int64_t)abs_deadline.tv_sec * 1000 + abs_deadline.tv_nsec / 1000000; if (now_ms >= deadline_ms) { timeout = 1; } else { if ((deadline_ms - now_ms) >= INFINITE) { timeout_max_ms = INFINITE - 1; } else { timeout_max_ms = (DWORD)(deadline_ms - now_ms); } timeout = (SleepConditionVariableCS(cv, &mu->cs, timeout_max_ms) == 0 && GetLastError() == ERROR_TIMEOUT); } } mu->locked = 1; return timeout; }
EXEC_RETURN condition_Wait(CONDITION* condition,CS_LOCK* cs,unsigned long msec){ #if defined(WIN32) || defined(_WIN64) return SleepConditionVariableCS(condition,cs,msec) ? EXEC_SUCCESS:EXEC_ERROR; #else int res; if(msec == ~0){ res = pthread_cond_wait(condition,cs); if(res) goto err; }else{ struct timeval utc = {0}; struct timespec time_val = {0}; if(!gettimeofday(&utc,NULL)){ time_val.tv_sec = utc.tv_sec + msec / 1000; msec %= 1000; time_val.tv_nsec = utc.tv_usec * 1000 + msec * 1000000; res = pthread_cond_timedwait(condition,cs,&time_val); if(res) goto err; }else return EXEC_ERROR; } return EXEC_SUCCESS; err: errno = res; return EXEC_ERROR; #endif }
void YabThreadRemoteSleep(unsigned int id) { if (!thread_handle[id].thd) return; // Thread wasn't running in the first place SleepConditionVariableCS(&thread_handle[id].cond, &thread_handle[id].mutex, INFINITE); }
//============================================================================== void ConditionVariable::wait(Mutex& amtx) { CONDITION_VARIABLE* cond = reinterpret_cast<CONDITION_VARIABLE*>(m_impl); CRITICAL_SECTION* mtx = reinterpret_cast<CRITICAL_SECTION*>(amtx.m_impl); SleepConditionVariableCS(cond, mtx, INFINITE); }
void SkCondVar::wait() { #ifdef SK_USE_POSIX_THREADS pthread_cond_wait(&fCond, &fMutex); #elif defined(SK_BUILD_FOR_WIN32) SleepConditionVariableCS(&fCondition, &fCriticalSection, INFINITE); #endif }
int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, struct timespec* abstime) { DWORD timeout = get_milliseconds(abstime); if (!SleepConditionVariableCS(cond, mutex, timeout)) return ETIMEDOUT; return 0; }
DWORD WINAPI ProducerThreadProc (PVOID p) { ULONG ProducerId = (ULONG)(ULONG_PTR)p; while (true) { // Produce a new item. Sleep(rand() % PRODUCER_SLEEP_TIME_MS); ULONG Item = InterlockedIncrement(&LastItemProduced); EnterCriticalSection(&BufferLock); while (QueueSize == BUFFER_SIZE && StopRequested == FALSE) { // Buffer is full - sleep so consumers can get items. SleepConditionVariableCS(&BufferNotFull, &BufferLock, INFINITE); } if (StopRequested == TRUE) { LeaveCriticalSection(&BufferLock); break; } // Insert the item at the end of the queue and increment size. Buffer[(QueueStartOffset + QueueSize) % BUFFER_SIZE] = Item; ++QueueSize; ++TotalItemsProduced; printf("Producer %u: item %2d, queue size %2u\r\n", ProducerId, Item, QueueSize); LeaveCriticalSection(&BufferLock); // If a consumer is waiting, wake it. WakeConditionVariable(&BufferNotEmpty); } printf("Producer %u exiting\r\n", ProducerId); return 0; }
DWORD WINAPI ConsumerThreadProc (PVOID p) { ULONG ConsumerId = (ULONG)(ULONG_PTR)p; while (true) { EnterCriticalSection(&BufferLock); while (QueueSize == 0 && StopRequested == FALSE) { // Buffer is empty - sleep so producers can create items. SleepConditionVariableCS(&BufferNotEmpty, &BufferLock, INFINITE); } if (StopRequested == TRUE && QueueSize == 0) { LeaveCriticalSection(&BufferLock); break; } // Consume the first available item. LONG Item = Buffer[QueueStartOffset]; --QueueSize; ++QueueStartOffset; ++TotalItemsConsumed; if (QueueStartOffset == BUFFER_SIZE) { QueueStartOffset = 0; } printf("Consumer %u: item %2d, queue size %2u\r\n", ConsumerId, Item, QueueSize); LeaveCriticalSection(&BufferLock); // If a producer is waiting, wake it. WakeConditionVariable(&BufferNotFull); // Simulate processing of the item. Sleep(rand() % CONSUMER_SLEEP_TIME_MS); } printf("Consumer %u exiting\r\n", ConsumerId); return 0; }
bool ConditionVariableImpl::Wait(MutexImpl* mutex, UInt32 timeout) { #if NAZARA_CORE_WINDOWS_VISTA return SleepConditionVariableCS(&m_cv, &mutex->m_criticalSection, timeout); #else m_count++; // It's ok to release the mutex here since Win32 // manual-reset events maintain state when used with SetEvent. // This avoids the "lost wakeup" bug... LeaveCriticalSection(&mutex->m_criticalSection); // Wait for either event to become signaled due to Signal being called or SignalAll being called. int result = WaitForMultipleObjects(2, m_events, false, timeout); // Some thread called SignalAll if (--m_count == 0 && result == WAIT_OBJECT_0 + BROADCAST) // We're the last waiter to be notified or to stop waiting, so reset the manual event. ResetEvent(m_events[BROADCAST]); // Reacquire the mutex. EnterCriticalSection(&mutex->m_criticalSection); return result != WAIT_TIMEOUT; #endif }
int nn_condvar_wait (nn_condvar_t *cond, nn_mutex_t *lock, int timeout) { BOOL brc; DWORD expire; /* Likely this is redundant, but for API correctness be explicit. */ expire = (timeout < 0) ? INFINITE : (DWORD) timeout; /* We must own the lock if we are going to call this. */ nn_assert (lock->owner == GetCurrentThreadId()); /* Clear ownership as SleepConditionVariableCS will drop it. */ lock->owner = 0; brc = SleepConditionVariableCS (&cond->cv, &lock->cs, expire); /* We have reacquired the lock, so nobody should own it right now. */ nn_assert (lock->owner == 0); /* Note we own it now. */ lock->owner = GetCurrentThreadId(); if (!brc && GetLastError () == ERROR_TIMEOUT) { return (-ETIMEDOUT); } return (0); }
rboolean r_cond_wait_timed (RCond * cond, RMutex * mutex, rulong microsec) { rboolean ret; #if defined (R_OS_WIN32) if (!(ret = SleepConditionVariableCS ((PCONDITION_VARIABLE)*cond, (LPCRITICAL_SECTION)*mutex, microsec / 1000))) { /* GetLastError () should be ERROR_TIMEOUT */ } #elif defined (HAVE_PTHREAD_H) struct timespec expire; int waitret; clock_gettime (CLOCK_MONOTONIC, &expire) expire.tv_sec = microsec / R_USEC_PER_SEC; expire.tv_nsec = 1000 * (microsec % R_USEC_PER_SEC); waitret = pthread_cond_timedwait ((pthread_cond_t *)*cond, (pthread_mutex_t *)*mutex, &expire); if (!(ret = (waitret == 0))) { /* waitret should be ETIMEDOUT */ } #else (void) cond; ret = FALSE; #endif return ret; }
//============================================================================== Bool Barrier::wait() { ANKI_ASSERT(m_impl); BarrierImpl& barrier = *reinterpret_cast<BarrierImpl*>(m_impl); EnterCriticalSection(&barrier.m_mtx); U32 gen = barrier.m_generation; if(--barrier.m_count == 0) { ++barrier.m_generation; barrier.m_count = barrier.m_threshold; WakeAllConditionVariable(&barrier.m_cvar); LeaveCriticalSection(&barrier.m_mtx); return true; } while(gen == barrier.m_generation) { SleepConditionVariableCS(&barrier.m_cvar, &barrier.m_mtx, INFINITE); } LeaveCriticalSection(&barrier.m_mtx); return false; }
int embb_condition_wait_until(embb_condition_t* condition_var, embb_mutex_t* mutex, const embb_time_t* time) { assert(condition_var != NULL); assert(mutex != NULL); assert(time != NULL); /* The Windows API needs a time duration, so we need to convert the given time by using the time now. */ embb_time_t now; embb_time_now(&now); /* Check if absolute timepoint (in milliseconds) still is in the future */ if ((time->seconds * 1000 + time->nanoseconds / 1000000) > (now.seconds * 1000 + now.nanoseconds / 1000000)) { /* Convert to (unsigned type) milliseconds and round up */ DWORD time_diff = (DWORD) ( time->seconds * 1000 + time->nanoseconds / 1000000 - now.seconds * 1000 - now.nanoseconds / 1000000); if (SleepConditionVariableCS(condition_var, mutex, time_diff) == 0) { if (GetLastError() == ERROR_TIMEOUT) { return EMBB_TIMEDOUT; } else { return EMBB_ERROR; } } } else { return EMBB_TIMEDOUT; } return EMBB_SUCCESS; }
/** @Status Caveat @Notes Error return codes ignored. Only ETIMEDOUT is supported, otherwise windows errors are used. */ extern "C" int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* ts) { if (*((uint32_t*)cond) == PTHREAD_COND_INITIALIZER) { pthread_cond_init(cond, 0); } if (*mutex == PTHREAD_MUTEX_INITIALIZER) { pthread_mutex_init(mutex, 0); } EbrTimeval tv; EbrGetTimeOfDay(&tv); uint64_t curNanoseconds = ((uint64_t)tv.tv_sec) * 1000 * 1000 * 1000 + ((uint64_t)tv.tv_usec) * 1000; uint64_t timeoutNS = ((uint64_t)ts->tv_sec) * 1000 * 1000 * 1000 + (uint64_t)ts->tv_nsec; int64_t waitNS = timeoutNS - curNanoseconds; if (waitNS < 0) waitNS = 0; int retVal = 0; DWORD ms = waitNS / 1000000; DWORD ret = SleepConditionVariableCS((CONDITION_VARIABLE*)*cond, (CRITICAL_SECTION*)*mutex, ms); if (ret) { retVal = 0; } else { retVal = ETIMEDOUT; } return retVal; }
static void oskar_condition_wait(oskar_ConditionVar* var) { #if defined(OSKAR_OS_WIN) SleepConditionVariableCS(&var->var, &(var->lock.lock), INFINITE); #else pthread_cond_wait(&var->var, &(var->lock.lock)); #endif }
void CondVar::SleepOn() { #ifdef WIN32 SleepConditionVariableCS(&mCondVar, &mMutex->csec, INFINITE); #else #endif mMutex->UnLock(); }
int embb_condition_wait(embb_condition_t* condition_var, embb_mutex_t* mutex) { assert(condition_var != NULL); assert(mutex != NULL); if (SleepConditionVariableCS(condition_var, mutex, INFINITE)) { return EMBB_SUCCESS; } return EMBB_ERROR; }
// -------------------------------------------------------------------------------------- FUNCTION // -------------------------------------------------------------------------------------- FUNCTION void Monitor::Wait(XR_IN xr::Core::RecursiveMutex & mutex) const { if (SleepConditionVariableCS((CONDITION_VARIABLE*)&mCondition, mutex.UnderlyingSystemObject(), INFINITE) == FALSE) { WakeAllConditionVariable ((CONDITION_VARIABLE*)&mCondition); uint32_t err = (uint32_t)GetLastError(); XR_ASSERT_ALWAYS_EQ_FM(err, ERROR_TIMEOUT, "SleepConditionVariableCS Error 0x%8.8" XR_UINT32_PRINTX "!", err); } }
/** @Status Caveat @Notes Ignores Error codes, ignores error cases */ extern "C" int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) { if (*((uint32_t*)cond) == (uint32_t)PTHREAD_COND_INITIALIZER) { pthread_cond_init(cond, 0); } if (*mutex == PTHREAD_MUTEX_INITIALIZER) { pthread_mutex_init(mutex, 0); } SleepConditionVariableCS((CONDITION_VARIABLE*)*cond, (CRITICAL_SECTION*)*mutex, INFINITE); return 0; }
// 7.25.3.6 int cnd_wait(cnd_t *cond, mtx_t *mtx) { if (!cond || !mtx) return thrd_error; #ifdef EMULATED_THREADS_USE_NATIVE_CV SleepConditionVariableCS(&cond->condvar, &mtx->cs, INFINITE); #else impl_cond_do_wait(cond, mtx, NULL); #endif return thrd_success; }
void vp_os_cond_wait(vp_os_cond_t *cond) { #if defined USE_WINDOWS_CONDITION_VARIABLES SleepConditionVariableCS(&cond->cond, (CRITICAL_SECTION *)cond->mutex, INFINITE); #elif defined USE_PTHREAD_FOR_WIN32 pthread_cond_wait(&cond->cond, (pthread_mutex_t *)cond->mutex); #else WaitForSingleObject(cond->LockSemaphore,INFINITE); // TODO: to test #endif }
// 7.25.3.5 int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const xtime *xt) { if (!cond || !mtx || !xt) return thrd_error; #ifdef EMULATED_THREADS_USE_NATIVE_CV if (SleepConditionVariableCS(&cond->condvar, &mtx->cs, impl_xtime2msec(xt))) return thrd_success; return (GetLastError() == ERROR_TIMEOUT) ? thrd_busy : thrd_error; #else return impl_cond_do_wait(cond, mtx, xt); #endif }
bool Win32_ConditionVariable::Wait(Mutex* mutex, unsigned int timeout) { Win32_Mutex* win32mutex = static_cast<Win32_Mutex*>(mutex); if (timeout == 0) { timeout = INFINITE; } return (SleepConditionVariableCS(&m_convar_handle, &win32mutex->m_critical_section, timeout) != 0); }
lm_error lm_wait(lm_monitor monitor, int ms) { DWORD timeout = (ms >= 0) ? ((DWORD)ms) : INFINITE; if (!monitor || !monitor->ready) return lm_pointer; if (SleepConditionVariableCS(&monitor->cv, &monitor->cs, timeout)) return lm_ok; return (GetLastError() == ERROR_TIMEOUT) ? lm_timeout : lm_pointer; }
PLATAPI void plat_cv_wait_timed(plat_thread_cv_t* cv, plat_thread_mutex_t* mutex, ts_time_t timeout) { DWORD millis; millis = (timeout == TS_TIME_MAX) ? INFINITE : timeout / T_MS; SleepConditionVariableCS(&cv->tcv_cond_var, &mutex->tm_crit_section, millis); }
teUtilsStatus eUtils_QueueQueue(tsUtilsQueue *psQueue, void *pvData) { tsQueuePrivate *psQueuePrivate = (tsQueuePrivate*)psQueue->pvPriv; DBG_vPrintf(DBG_QUEUE, "Queue %p: Queue %p\n", psQueue, pvData); #ifndef WIN32 pthread_mutex_lock (&psQueuePrivate->mMutex); #else EnterCriticalSection (&psQueuePrivate->hMutex); #endif /* WIN32 */ DBG_vPrintf(DBG_QUEUE, "Got mutex on queue %p (size = %d)\n", psQueue, psQueuePrivate->u32Size); if (psQueuePrivate->iFlags & UTILS_QUEUE_NONBLOCK_INPUT) { // Check if buffer is full and return if so. if (psQueuePrivate->u32Size == psQueuePrivate->u32Capacity) { DBG_vPrintf(DBG_QUEUE, "Queue full, could not enqueue entry\n"); #ifndef WIN32 pthread_mutex_unlock (&psQueuePrivate->mMutex); #else LeaveCriticalSection (&psQueuePrivate->hMutex); #endif /* WIN32 */ return E_UTILS_ERROR_BLOCK; } } else { // Block until space is available while (psQueuePrivate->u32Size == psQueuePrivate->u32Capacity) #ifndef WIN32 pthread_cond_wait (&psQueuePrivate->cond_space_available, &psQueuePrivate->mMutex); #else SleepConditionVariableCS(&psQueuePrivate->hSpaceAvailable, &psQueuePrivate->hMutex, INFINITE); #endif /* WIN32 */ } psQueuePrivate->apvBuffer[psQueuePrivate->u32In] = pvData; psQueuePrivate->u32Size++; psQueuePrivate->u32In = (psQueuePrivate->u32In+1) % psQueuePrivate->u32Capacity; #ifndef WIN32 pthread_mutex_unlock (&psQueuePrivate->mMutex); pthread_cond_broadcast (&psQueuePrivate->cond_data_available); #else LeaveCriticalSection (&psQueuePrivate->hMutex); WakeConditionVariable (&psQueuePrivate->hDataAvailable); #endif /* WIN32 */ DBG_vPrintf(DBG_QUEUE, "Queue %p: Queued %p\n", psQueue, pvData); return E_UTILS_OK; }
teUtilsStatus eUtils_QueueDequeue(tsUtilsQueue *psQueue, void **ppvData) { tsQueuePrivate *psQueuePrivate = (tsQueuePrivate*)psQueue->pvPriv; DBG_vPrintf(DBG_QUEUE, "Queue %p: Dequeue\n", psQueue); #ifndef WIN32 pthread_mutex_lock (&psQueuePrivate->mMutex); #else EnterCriticalSection (&psQueuePrivate->hMutex); #endif /* WIN32 */ DBG_vPrintf(DBG_QUEUE, "Got mutex on queue %p (size=%d)\n", psQueue, psQueuePrivate->u32Size); if (psQueuePrivate->iFlags & UTILS_QUEUE_NONBLOCK_OUTPUT) { // Check if buffer is empty and return if so. if (psQueuePrivate->u32Size == 0) { DBG_vPrintf(DBG_QUEUE, "Queue empty\n"); #ifndef WIN32 pthread_mutex_unlock (&psQueuePrivate->mMutex); #else LeaveCriticalSection (&psQueuePrivate->hMutex); #endif /* WIN32 */ return E_UTILS_ERROR_BLOCK; } } else { // Wait for data to become available while (psQueuePrivate->u32Size == 0) #ifndef WIN32 pthread_cond_wait (&psQueuePrivate->cond_data_available, &psQueuePrivate->mMutex); #else SleepConditionVariableCS(&psQueuePrivate->hDataAvailable, &psQueuePrivate->hMutex, INFINITE); #endif /* WIN32 */ } *ppvData = psQueuePrivate->apvBuffer[psQueuePrivate->u32Out]; psQueuePrivate->u32Size--; psQueuePrivate->u32Out = (psQueuePrivate->u32Out + 1) % psQueuePrivate->u32Capacity; #ifndef WIN32 pthread_mutex_unlock (&psQueuePrivate->mMutex); pthread_cond_broadcast (&psQueuePrivate->cond_space_available); #else LeaveCriticalSection (&psQueuePrivate->hMutex); WakeConditionVariable (&psQueuePrivate->hSpaceAvailable); #endif /* WIN32 */ DBG_vPrintf(DBG_QUEUE, "Queue %p: Dequeued %p\n", psQueue, *ppvData); return E_UTILS_OK; }
int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline) { int timeout = 0; DWORD timeout_max_ms; mu->locked = 0; if (gpr_time_cmp(abs_deadline, gpr_inf_future) == 0) { SleepConditionVariableCS(cv, &mu->cs, INFINITE); } else { gpr_timespec now = gpr_now(); gpr_int64 now_ms = now.tv_sec * 1000 + now.tv_nsec / 1000000; gpr_int64 deadline_ms = abs_deadline.tv_sec * 1000 + abs_deadline.tv_nsec / 1000000; if (now_ms >= deadline_ms) { timeout = 1; } else { timeout_max_ms = (DWORD)min(deadline_ms - now_ms, INFINITE - 1); timeout = (SleepConditionVariableCS(cv, &mu->cs, timeout_max_ms) == 0 && GetLastError() == ERROR_TIMEOUT); } } mu->locked = 1; return timeout; }
int nn_condvar_wait (nn_condvar_t *cond, nn_mutex_t *lock, int timeout) { BOOL brc; DWORD expire; /* Likely this is redundant, but for API correctness be explicit. */ expire = (timeout < 0) ? INFINITE : (DWORD) timeout; brc = SleepConditionVariableCS (&cond->cv, &lock->mutex, expire); if (!brc && GetLastError () == ERROR_TIMEOUT) { return (-ETIMEDOUT); } return (0); }