| 57 | } |
| 58 | |
| 59 | WaitResult ConditionVariable::wait(Mutex &mutex, u32 ms) |
| 60 | { |
| 61 | WaitResult wr; |
| 62 | #if CROWN_PLATFORM_WINDOWS |
| 63 | if (SleepConditionVariableCS(&_priv->cv |
| 64 | , (CRITICAL_SECTION *)mutex.native_handle() |
| 65 | , ms == 0 ? INFINITE : ms |
| 66 | )) |
| 67 | wr.error = WaitResult::SUCCESS; |
| 68 | else if (GetLastError() == ERROR_TIMEOUT) |
| 69 | wr.error = WaitResult::TIMEOUT; |
| 70 | else |
| 71 | wr.error = WaitResult::UNKNOWN; |
| 72 | #else |
| 73 | int err; |
| 74 | |
| 75 | if (ms == 0) { |
| 76 | err = pthread_cond_wait(&_priv->cond, (pthread_mutex_t *)mutex.native_handle()); |
| 77 | } else { |
| 78 | timespec ts; |
| 79 | clock_gettime(CLOCK_REALTIME, &ts); |
| 80 | |
| 81 | const u64 ns = ts.tv_sec * UINT64_C(1000000000) + ts.tv_nsec + u64(ms) * UINT64_C(1000000); |
| 82 | ts.tv_sec = ns / UINT64_C(1000000000); |
| 83 | ts.tv_nsec = ns % UINT64_C(1000000000); |
| 84 | |
| 85 | err = pthread_cond_timedwait(&_priv->cond, (pthread_mutex_t *)mutex.native_handle(), &ts); |
| 86 | } |
| 87 | |
| 88 | if (err == 0) |
| 89 | wr.error = WaitResult::SUCCESS; |
| 90 | else if (err == ETIMEDOUT) |
| 91 | wr.error = WaitResult::TIMEOUT; |
| 92 | else |
| 93 | wr.error = WaitResult::UNKNOWN; |
| 94 | #endif // if CROWN_PLATFORM_WINDOWS |
| 95 | return wr; |
| 96 | } |
| 97 | |
| 98 | void ConditionVariable::signal() |
| 99 | { |
nothing calls this directly
no test coverage detected