| 104 | } |
| 105 | |
| 106 | bool Semaphore::try_wait() |
| 107 | { |
| 108 | #if CROWN_PLATFORM_WINDOWS |
| 109 | DWORD err = WaitForSingleObject(_priv->handle, 0u); |
| 110 | if (err == WAIT_OBJECT_0) |
| 111 | return true; |
| 112 | else if (err == WAIT_TIMEOUT) |
| 113 | return false; |
| 114 | CE_FATAL("WaitForSingleObject: GetLastError = %d", GetLastError()); |
| 115 | return false; |
| 116 | #else |
| 117 | pthread_mutex_lock(&_priv->mutex); |
| 118 | if (_priv->count == 0) { |
| 119 | pthread_mutex_unlock(&_priv->mutex); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | while (_priv->count <= 0) { |
| 124 | int err = pthread_cond_wait(&_priv->cond, &_priv->mutex); |
| 125 | CE_ASSERT(err == 0, "pthread_cond_wait: errno = %d", err); |
| 126 | CE_UNUSED(err); |
| 127 | } |
| 128 | _priv->count--; |
| 129 | pthread_mutex_unlock(&_priv->mutex); |
| 130 | return true; |
| 131 | #endif // if CROWN_PLATFORM_WINDOWS |
| 132 | } |
| 133 | |
| 134 | } // namespace crown |
no test coverage detected