parent
154c329a25
commit
82472d09ac
@ -12,68 +12,9 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
cEvent::cEvent(void)
|
cEvent::cEvent(void) :
|
||||||
|
m_ShouldWait(true)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
|
||||||
m_Event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
|
||||||
if (m_Event == nullptr)
|
|
||||||
{
|
|
||||||
LOGERROR("cEvent: cannot create event, GLE = %u. Aborting server.", (unsigned)GetLastError());
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
#else // *nix
|
|
||||||
m_bIsNamed = false;
|
|
||||||
m_Event = new sem_t;
|
|
||||||
if (sem_init(m_Event, 0, 0))
|
|
||||||
{
|
|
||||||
// This path is used by MacOS, because it doesn't support unnamed semaphores.
|
|
||||||
delete m_Event;
|
|
||||||
m_bIsNamed = true;
|
|
||||||
|
|
||||||
AString EventName;
|
|
||||||
Printf(EventName, "cEvent%p", this);
|
|
||||||
m_Event = sem_open(EventName.c_str(), O_CREAT, 777, 0);
|
|
||||||
if (m_Event == SEM_FAILED)
|
|
||||||
{
|
|
||||||
AString error = GetOSErrorString(errno);
|
|
||||||
LOGERROR("cEvent: Cannot create event, err = %s. Aborting server.", error.c_str());
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
// Unlink the semaphore immediately - it will continue to function but will not pollute the namespace
|
|
||||||
// We don't store the name, so can't call this in the destructor
|
|
||||||
if (sem_unlink(EventName.c_str()) != 0)
|
|
||||||
{
|
|
||||||
AString error = GetOSErrorString(errno);
|
|
||||||
LOGWARN("ERROR: Could not unlink cEvent. (%s)", error.c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // *nix
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cEvent::~cEvent()
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
CloseHandle(m_Event);
|
|
||||||
#else
|
|
||||||
if (m_bIsNamed)
|
|
||||||
{
|
|
||||||
if (sem_close(m_Event) != 0)
|
|
||||||
{
|
|
||||||
AString error = GetOSErrorString(errno);
|
|
||||||
LOGERROR("ERROR: Could not close cEvent. (%s)", error.c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sem_destroy(m_Event);
|
|
||||||
delete m_Event;
|
|
||||||
m_Event = nullptr;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -82,20 +23,12 @@ cEvent::~cEvent()
|
|||||||
|
|
||||||
void cEvent::Wait(void)
|
void cEvent::Wait(void)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
std::unique_lock<std::mutex> Lock(m_Mutex);
|
||||||
DWORD res = WaitForSingleObject(m_Event, INFINITE);
|
while (m_ShouldWait)
|
||||||
if (res != WAIT_OBJECT_0)
|
{
|
||||||
{
|
m_CondVar.wait(Lock);
|
||||||
LOGWARN("cEvent: waiting for the event failed: %u, GLE = %u. Continuing, but server may be unstable.", (unsigned)res, (unsigned)GetLastError());
|
}
|
||||||
}
|
m_ShouldWait = true;
|
||||||
#else
|
|
||||||
int res = sem_wait(m_Event);
|
|
||||||
if (res != 0)
|
|
||||||
{
|
|
||||||
AString error = GetOSErrorString(errno);
|
|
||||||
LOGWARN("cEvent: waiting for the event failed: %i, err = %s. Continuing, but server may be unstable.", res, error.c_str());
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -104,45 +37,34 @@ void cEvent::Wait(void)
|
|||||||
|
|
||||||
bool cEvent::Wait(int a_TimeoutMSec)
|
bool cEvent::Wait(int a_TimeoutMSec)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
std::chrono::steady_clock::time_point dst = std::chrono::steady_clock::now() + std::chrono::milliseconds(a_TimeoutMSec);
|
||||||
DWORD res = WaitForSingleObject(m_Event, (DWORD)a_TimeoutMSec);
|
std::unique_lock<std::mutex> Lock(m_Mutex); // We assume that this lock is acquired without much delay - we are the only user of the mutex
|
||||||
switch (res)
|
while (m_ShouldWait && (std::chrono::steady_clock::now() < dst))
|
||||||
|
{
|
||||||
|
switch (m_CondVar.wait_until(Lock, dst))
|
||||||
{
|
{
|
||||||
case WAIT_OBJECT_0: return true; // Regular event signalled
|
case std::cv_status::no_timeout:
|
||||||
case WAIT_TIMEOUT: return false; // Regular event timeout
|
|
||||||
default:
|
|
||||||
{
|
{
|
||||||
LOGWARN("cEvent: waiting for the event failed: %u, GLE = %u. Continuing, but server may be unstable.", (unsigned)res, (unsigned)GetLastError());
|
// The wait was successful, check for spurious wakeup:
|
||||||
|
if (!m_ShouldWait)
|
||||||
|
{
|
||||||
|
m_ShouldWait = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// This was a spurious wakeup, wait again:
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
case std::cv_status::timeout:
|
||||||
|
{
|
||||||
|
// The wait timed out, return failure:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
} // switch (wait_until())
|
||||||
#else
|
} // while (m_ShouldWait && not timeout)
|
||||||
// Get the current time:
|
|
||||||
timespec timeout;
|
|
||||||
if (clock_gettime(CLOCK_REALTIME, &timeout) == -1)
|
|
||||||
{
|
|
||||||
LOGWARN("cEvent: Getting current time failed: %i, err = %s. Continuing, but the server may be unstable.", errno, GetOSErrorString(errno).c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the specified timeout:
|
// The wait timed out in the while() condition:
|
||||||
timeout.tv_sec += a_TimeoutMSec / 1000;
|
return false;
|
||||||
timeout.tv_nsec += (a_TimeoutMSec % 1000) * 1000000; // 1 msec = 1000000 usec
|
|
||||||
|
|
||||||
// Wait with timeout:
|
|
||||||
int res = sem_timedwait(m_Event, &timeout);
|
|
||||||
switch (res)
|
|
||||||
{
|
|
||||||
case 0: return true; // Regular event signalled
|
|
||||||
case ETIMEDOUT: return false; // Regular even timeout
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
AString error = GetOSErrorString(errno);
|
|
||||||
LOGWARN("cEvent: waiting for the event failed: %i, err = %s. Continuing, but server may be unstable.", res, error.c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -151,19 +73,11 @@ bool cEvent::Wait(int a_TimeoutMSec)
|
|||||||
|
|
||||||
void cEvent::Set(void)
|
void cEvent::Set(void)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
{
|
||||||
if (!SetEvent(m_Event))
|
std::unique_lock<std::mutex> Lock(m_Mutex);
|
||||||
{
|
m_ShouldWait = false;
|
||||||
LOGWARN("cEvent: Could not set cEvent: GLE = %u", (unsigned)GetLastError());
|
}
|
||||||
}
|
m_CondVar.notify_one();
|
||||||
#else
|
|
||||||
int res = sem_post(m_Event);
|
|
||||||
if (res != 0)
|
|
||||||
{
|
|
||||||
AString error = GetOSErrorString(errno);
|
|
||||||
LOGWARN("cEvent: Could not set cEvent: %i, err = %s", res, error.c_str());
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
|
|
||||||
// Event.h
|
// Event.h
|
||||||
|
|
||||||
// Interfaces to the cEvent object representing an OS-specific synchronization primitive that can be waited-for
|
// Interfaces to the cEvent object representing a synchronization primitive that can be waited-for
|
||||||
// Implemented as an Event on Win and as a 1-semaphore on *nix
|
// Implemented using C++11 condition variable and mutex
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#ifndef CEVENT_H_INCLUDED
|
|
||||||
#define CEVENT_H_INCLUDED
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -20,9 +21,13 @@ class cEvent
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
cEvent(void);
|
cEvent(void);
|
||||||
~cEvent();
|
|
||||||
|
|
||||||
|
/** Waits until the event has been set.
|
||||||
|
If the event has been set before it has been waited for, Wait() returns immediately. */
|
||||||
void Wait(void);
|
void Wait(void);
|
||||||
|
|
||||||
|
/** Sets the event - releases one thread that has been waiting in Wait().
|
||||||
|
If there was no thread waiting, the next call to Wait() will not block. */
|
||||||
void Set (void);
|
void Set (void);
|
||||||
|
|
||||||
/** Waits for the event until either it is signalled, or the (relative) timeout is passed.
|
/** Waits for the event until either it is signalled, or the (relative) timeout is passed.
|
||||||
@ -31,21 +36,17 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
#ifdef _WIN32
|
/** Used for checking for spurious wakeups. */
|
||||||
HANDLE m_Event;
|
bool m_ShouldWait;
|
||||||
#else
|
|
||||||
sem_t * m_Event;
|
/** Mutex protecting m_ShouldWait from multithreaded access. */
|
||||||
bool m_bIsNamed;
|
std::mutex m_Mutex;
|
||||||
#endif
|
|
||||||
|
/** The condition variable used as the Event. */
|
||||||
|
std::condition_variable m_CondVar;
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // CEVENT_H_INCLUDED
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user