From a90da050ebd925776f0f689abc22f67df0e2ffae Mon Sep 17 00:00:00 2001 From: Vlad Khorsun Date: Tue, 14 Apr 2026 15:43:23 +0300 Subject: [PATCH] Use Windows native implementation of condition variables. --- src/common/classes/condition.h | 66 +++++----------------------------- src/common/classes/locks.h | 2 ++ 2 files changed, 10 insertions(+), 58 deletions(-) diff --git a/src/common/classes/condition.h b/src/common/classes/condition.h index bd943dedfa5..d1c989733ad 100644 --- a/src/common/classes/condition.h +++ b/src/common/classes/condition.h @@ -43,50 +43,19 @@ class MemoryPool; class Condition { private: - AtomicCounter waiters; - - enum - { - SIGNAL = 0, - BROADCAST, - MAX_EVENTS - }; - - HANDLE events[MAX_EVENTS]; + CONDITION_VARIABLE m_condvar; void init() { - events[SIGNAL] = CreateEvent(NULL, // no security - FALSE, // auto-reset event - FALSE, // non-signaled initially - NULL); // unnamed - - if (!events[SIGNAL]) - system_call_failed::raise("CreateEvent(SIGNAL)"); - - // Create a manual-reset event. - events[BROADCAST] = CreateEvent(NULL, // no security - TRUE, // manual-reset - FALSE, // non-signaled initially - NULL); // unnamed - - if (!events[BROADCAST]) - { - CloseHandle(events[SIGNAL]); - system_call_failed::raise("CreateEvent(BROADCAST)"); - } + InitializeConditionVariable(&m_condvar); } public: - Condition() { init(); } + Condition() { init(); } explicit Condition(MemoryPool&) { init(); } ~Condition() { - if (events[SIGNAL] && !CloseHandle(events[SIGNAL])) - system_call_failed::raise("CloseHandle(SIGNAL)"); - if (events[BROADCAST] && !CloseHandle(events[BROADCAST])) - system_call_failed::raise("CloseHandle(BROADCAST)"); } // Forbid copying @@ -95,41 +64,22 @@ class Condition void wait(Mutex& m) { - ++waiters; - - m.leave(); - - if (WaitForMultipleObjects((DWORD) MAX_EVENTS, events, FALSE, INFINITE) == WAIT_FAILED) - system_call_failed::raise("WaitForMultipleObjects"); - - if (--waiters == 0) - { - if (!ResetEvent(events[BROADCAST])) - system_call_failed::raise("ResetEvent(BROADCAST)"); - } - - m.enter("Condition::wait"); + if (!SleepConditionVariableCS(&m_condvar, &m.spinlock, INFINITE)) + system_call_failed::raise("SleepConditionVariableCS"); } void notifyOne() { - if (waiters.value() > 0) - { - if (!SetEvent(events[SIGNAL])) - system_call_failed::raise("SetEvent(SIGNAL)"); - } + WakeConditionVariable(&m_condvar); } void notifyAll() { - if (waiters.value() > 0) - { - if (!SetEvent(events[BROADCAST])) - system_call_failed::raise("SetEvent"); - } + WakeAllConditionVariable(&m_condvar); } }; + } // namespace Firebird #else // WIN_NT diff --git a/src/common/classes/locks.h b/src/common/classes/locks.h index 2c44ca01df3..f1f56e8c82e 100644 --- a/src/common/classes/locks.h +++ b/src/common/classes/locks.h @@ -56,6 +56,8 @@ class Exception; // Needed for catch class Mutex : public Reasons { +friend class Condition; + protected: CRITICAL_SECTION spinlock; #ifdef DEV_BUILD