| 55 | } |
| 56 | |
| 57 | int CSceSemaphore::Wait(int count, uint32_t* pTimeOut) |
| 58 | { |
| 59 | int err = SCE_KERNEL_ERROR_UNKNOWN; |
| 60 | std::unique_lock<std::mutex> lock(m_mutex); |
| 61 | do |
| 62 | { |
| 63 | if (count < 1 || count > m_maxCount) |
| 64 | { |
| 65 | err = SCE_KERNEL_ERROR_EINVAL; |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | auto pred = [this, count] { return m_count >= count; }; |
| 70 | if (!pTimeOut) // infinite |
| 71 | { |
| 72 | m_cond.wait(lock, pred); |
| 73 | m_count -= count; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | auto timeMs = std::chrono::microseconds(*pTimeOut); |
| 78 | auto start = std::chrono::high_resolution_clock::now(); |
| 79 | |
| 80 | bool notExpired = m_cond.wait_for(lock, timeMs, pred); |
| 81 | |
| 82 | auto end = std::chrono::high_resolution_clock::now(); |
| 83 | if (notExpired) |
| 84 | { |
| 85 | auto dura = std::chrono::duration_cast<std::chrono::microseconds>(end - start); |
| 86 | auto timeLeft = timeMs - dura; |
| 87 | *pTimeOut = timeLeft.count(); |
| 88 | m_count -= count; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | *pTimeOut = 0; |
| 93 | err = SCE_KERNEL_ERROR_ETIMEDOUT; |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | err = SCE_OK; |
| 100 | } while (false); |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | // TODO: |
| 105 | // implement Cancel |
no test coverage detected