// 其他非静态成员函数。 bool Event::Wait(std::uint64_t u64MilliSeconds) const noexcept { auto u64Now = GetFastMonoClock(); const auto u64Until = u64Now + u64MilliSeconds; Mutex::UniqueLock vLock(x_mtxGuard); if(!x_bSet){ for(;;){ if(u64Until <= u64Now){ return false; } if(!x_cvWaiter.Wait(vLock, u64Until - u64Now)){ return false; } if(x_bSet){ break; } u64Now = GetFastMonoClock(); } } return true; }
std::size_t Semaphore::BatchWait(unsigned long long ullMilliSeconds, std::size_t uWaitCount) noexcept { const auto ullUntil = GetFastMonoClock() + ullMilliSeconds; std::size_t uSucceeded = 0; auto ullTimeRemaining = ullMilliSeconds; while(uSucceeded < uWaitCount){ const auto dwResult = ::WaitForSingleObject(x_hSemaphore.Get(), Min(ullTimeRemaining, ULONG_MAX >> 1)); if(dwResult == WAIT_FAILED){ ASSERT_MSG(false, L"WaitForSingleObject() 失败。"); } if(dwResult != WAIT_TIMEOUT){ ++uSucceeded; continue; } const auto ullNow = GetFastMonoClock(); if(ullUntil <= ullNow){ break; } ullTimeRemaining = ullUntil - ullNow; } return uSucceeded; }
// 其他非静态成员函数。 bool Semaphore::Wait(std::uint64_t u64MilliSeconds) noexcept { auto u64Now = GetFastMonoClock(); const auto u64Until = u64Now + u64MilliSeconds; Mutex::UniqueLock vLock(x_mtxGuard); if(x_uCount == 0){ for(;;){ if(u64Until <= u64Now){ return false; } if(!x_cvWaiter.Wait(vLock, u64Until - u64Now)){ return false; } if(x_uCount != 0){ break; } u64Now = GetFastMonoClock(); } } --x_uCount; return true; }