Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at devShellTools-shell 186 lines 4.5 kB view raw
1diff --git a/src/util/thread.h b/src/util/thread.h 2index 28aeca8a..db5c9913 100644 3--- a/src/util/thread.h 4+++ b/src/util/thread.h 5@@ -149,178 +149,8 @@ namespace dxvk { 6 } 7 } 8 9- 10- /** 11- * \brief SRW-based mutex implementation 12- * 13- * Drop-in replacement for \c std::mutex that uses Win32 14- * SRW locks, which are implemented with \c futex in wine. 15- */ 16- class mutex { 17- 18- public: 19- 20- using native_handle_type = PSRWLOCK; 21- 22- mutex() { } 23- 24- mutex(const mutex&) = delete; 25- mutex& operator = (const mutex&) = delete; 26- 27- void lock() { 28- AcquireSRWLockExclusive(&m_lock); 29- } 30- 31- void unlock() { 32- ReleaseSRWLockExclusive(&m_lock); 33- } 34- 35- bool try_lock() { 36- return TryAcquireSRWLockExclusive(&m_lock); 37- } 38- 39- native_handle_type native_handle() { 40- return &m_lock; 41- } 42- 43- private: 44- 45- SRWLOCK m_lock = SRWLOCK_INIT; 46- 47- }; 48- 49- 50- /** 51- * \brief Recursive mutex implementation 52- * 53- * Drop-in replacement for \c std::recursive_mutex that 54- * uses Win32 critical sections. 55- */ 56- class recursive_mutex { 57- 58- public: 59- 60- using native_handle_type = PCRITICAL_SECTION; 61- 62- recursive_mutex() { 63- InitializeCriticalSection(&m_lock); 64- } 65- 66- ~recursive_mutex() { 67- DeleteCriticalSection(&m_lock); 68- } 69- 70- recursive_mutex(const recursive_mutex&) = delete; 71- recursive_mutex& operator = (const recursive_mutex&) = delete; 72- 73- void lock() { 74- EnterCriticalSection(&m_lock); 75- } 76- 77- void unlock() { 78- LeaveCriticalSection(&m_lock); 79- } 80- 81- bool try_lock() { 82- return TryEnterCriticalSection(&m_lock); 83- } 84- 85- native_handle_type native_handle() { 86- return &m_lock; 87- } 88- 89- private: 90- 91- CRITICAL_SECTION m_lock; 92- 93- }; 94- 95- 96- /** 97- * \brief SRW-based condition variable implementation 98- * 99- * Drop-in replacement for \c std::condition_variable that 100- * uses Win32 condition variables on SRW locks. 101- */ 102- class condition_variable { 103- 104- public: 105- 106- using native_handle_type = PCONDITION_VARIABLE; 107- 108- condition_variable() { 109- InitializeConditionVariable(&m_cond); 110- } 111- 112- condition_variable(condition_variable&) = delete; 113- 114- condition_variable& operator = (condition_variable&) = delete; 115- 116- void notify_one() { 117- WakeConditionVariable(&m_cond); 118- } 119- 120- void notify_all() { 121- WakeAllConditionVariable(&m_cond); 122- } 123- 124- void wait(std::unique_lock<dxvk::mutex>& lock) { 125- auto srw = lock.mutex()->native_handle(); 126- SleepConditionVariableSRW(&m_cond, srw, INFINITE, 0); 127- } 128- 129- template<typename Predicate> 130- void wait(std::unique_lock<dxvk::mutex>& lock, Predicate pred) { 131- while (!pred()) 132- wait(lock); 133- } 134- 135- template<typename Clock, typename Duration> 136- std::cv_status wait_until(std::unique_lock<dxvk::mutex>& lock, const std::chrono::time_point<Clock, Duration>& time) { 137- auto now = Clock::now(); 138- 139- return (now < time) 140- ? wait_for(lock, now - time) 141- : std::cv_status::timeout; 142- } 143- 144- template<typename Clock, typename Duration, typename Predicate> 145- bool wait_until(std::unique_lock<dxvk::mutex>& lock, const std::chrono::time_point<Clock, Duration>& time, Predicate pred) { 146- if (pred()) 147- return true; 148- 149- auto now = Clock::now(); 150- return now < time && wait_for(lock, now - time, pred); 151- } 152- 153- template<typename Rep, typename Period> 154- std::cv_status wait_for(std::unique_lock<dxvk::mutex>& lock, const std::chrono::duration<Rep, Period>& timeout) { 155- auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout); 156- auto srw = lock.mutex()->native_handle(); 157- 158- return SleepConditionVariableSRW(&m_cond, srw, ms.count(), 0) 159- ? std::cv_status::no_timeout 160- : std::cv_status::timeout; 161- } 162- 163- template<typename Rep, typename Period, typename Predicate> 164- bool wait_for(std::unique_lock<dxvk::mutex>& lock, const std::chrono::duration<Rep, Period>& timeout, Predicate pred) { 165- bool result = pred(); 166- 167- if (!result && wait_for(lock, timeout) == std::cv_status::no_timeout) 168- result = pred(); 169- 170- return result; 171- } 172- 173- native_handle_type native_handle() { 174- return &m_cond; 175- } 176- 177- private: 178- 179- CONDITION_VARIABLE m_cond; 180- 181- }; 182+ using mutex = std::mutex; 183+ using recursive_mutex = std::recursive_mutex; 184+ using condition_variable = std::condition_variable; 185 186 }