Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 8 additions & 58 deletions src/common/classes/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/common/classes/locks.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Exception; // Needed for catch

class Mutex : public Reasons
{
friend class Condition;

protected:
CRITICAL_SECTION spinlock;
#ifdef DEV_BUILD
Expand Down
Loading