Skip to content

Commit 7163d73

Browse files
authored
Merge pull request #8989 from FirebirdSQL/work/WinNativeCV
Use Windows native implementation of condition variables.
2 parents 9c08e53 + a90da05 commit 7163d73

File tree

2 files changed

+10
-58
lines changed

2 files changed

+10
-58
lines changed

src/common/classes/condition.h

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -43,50 +43,19 @@ class MemoryPool;
4343
class Condition
4444
{
4545
private:
46-
AtomicCounter waiters;
47-
48-
enum
49-
{
50-
SIGNAL = 0,
51-
BROADCAST,
52-
MAX_EVENTS
53-
};
54-
55-
HANDLE events[MAX_EVENTS];
46+
CONDITION_VARIABLE m_condvar;
5647

5748
void init()
5849
{
59-
events[SIGNAL] = CreateEvent(NULL, // no security
60-
FALSE, // auto-reset event
61-
FALSE, // non-signaled initially
62-
NULL); // unnamed
63-
64-
if (!events[SIGNAL])
65-
system_call_failed::raise("CreateEvent(SIGNAL)");
66-
67-
// Create a manual-reset event.
68-
events[BROADCAST] = CreateEvent(NULL, // no security
69-
TRUE, // manual-reset
70-
FALSE, // non-signaled initially
71-
NULL); // unnamed
72-
73-
if (!events[BROADCAST])
74-
{
75-
CloseHandle(events[SIGNAL]);
76-
system_call_failed::raise("CreateEvent(BROADCAST)");
77-
}
50+
InitializeConditionVariable(&m_condvar);
7851
}
7952

8053
public:
81-
Condition() { init(); }
54+
Condition() { init(); }
8255
explicit Condition(MemoryPool&) { init(); }
8356

8457
~Condition()
8558
{
86-
if (events[SIGNAL] && !CloseHandle(events[SIGNAL]))
87-
system_call_failed::raise("CloseHandle(SIGNAL)");
88-
if (events[BROADCAST] && !CloseHandle(events[BROADCAST]))
89-
system_call_failed::raise("CloseHandle(BROADCAST)");
9059
}
9160

9261
// Forbid copying
@@ -95,41 +64,22 @@ class Condition
9564

9665
void wait(Mutex& m)
9766
{
98-
++waiters;
99-
100-
m.leave();
101-
102-
if (WaitForMultipleObjects((DWORD) MAX_EVENTS, events, FALSE, INFINITE) == WAIT_FAILED)
103-
system_call_failed::raise("WaitForMultipleObjects");
104-
105-
if (--waiters == 0)
106-
{
107-
if (!ResetEvent(events[BROADCAST]))
108-
system_call_failed::raise("ResetEvent(BROADCAST)");
109-
}
110-
111-
m.enter("Condition::wait");
67+
if (!SleepConditionVariableCS(&m_condvar, &m.spinlock, INFINITE))
68+
system_call_failed::raise("SleepConditionVariableCS");
11269
}
11370

11471
void notifyOne()
11572
{
116-
if (waiters.value() > 0)
117-
{
118-
if (!SetEvent(events[SIGNAL]))
119-
system_call_failed::raise("SetEvent(SIGNAL)");
120-
}
73+
WakeConditionVariable(&m_condvar);
12174
}
12275

12376
void notifyAll()
12477
{
125-
if (waiters.value() > 0)
126-
{
127-
if (!SetEvent(events[BROADCAST]))
128-
system_call_failed::raise("SetEvent");
129-
}
78+
WakeAllConditionVariable(&m_condvar);
13079
}
13180
};
13281

82+
13383
} // namespace Firebird
13484

13585
#else // WIN_NT

src/common/classes/locks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class Exception; // Needed for catch
5656

5757
class Mutex : public Reasons
5858
{
59+
friend class Condition;
60+
5961
protected:
6062
CRITICAL_SECTION spinlock;
6163
#ifdef DEV_BUILD

0 commit comments

Comments
 (0)