mirror of
https://github.com/EQEmu/Server.git
synced 2025-12-14 03:11:28 +00:00
weird #ifdef pattern, reorganized and cleaned it up. easier to understand now.
This commit is contained in:
parent
a7084b4d6c
commit
cdc7b2a000
@ -20,17 +20,9 @@
|
|||||||
#include "Condition.h"
|
#include "Condition.h"
|
||||||
|
|
||||||
#ifdef _WINDOWS
|
#ifdef _WINDOWS
|
||||||
#else
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _WINDOWS
|
Condition::Condition()
|
||||||
|
{
|
||||||
|
|
||||||
Condition::Condition()
|
|
||||||
{
|
|
||||||
m_events[SignalEvent] = CreateEvent (nullptr, // security
|
m_events[SignalEvent] = CreateEvent (nullptr, // security
|
||||||
FALSE, // is auto-reset event?
|
FALSE, // is auto-reset event?
|
||||||
FALSE, // is signaled initially?
|
FALSE, // is signaled initially?
|
||||||
@ -41,33 +33,33 @@ Condition::Condition()
|
|||||||
nullptr); // name
|
nullptr); // name
|
||||||
m_waiters = 0;
|
m_waiters = 0;
|
||||||
InitializeCriticalSection(&CSMutex);
|
InitializeCriticalSection(&CSMutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
Condition::~Condition()
|
Condition::~Condition()
|
||||||
{
|
{
|
||||||
DeleteCriticalSection(&CSMutex);
|
DeleteCriticalSection(&CSMutex);
|
||||||
CloseHandle(m_events[SignalEvent]);
|
CloseHandle(m_events[SignalEvent]);
|
||||||
CloseHandle(m_events[BroadcastEvent]);
|
CloseHandle(m_events[BroadcastEvent]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Condition::Signal()
|
void Condition::Signal()
|
||||||
{
|
{
|
||||||
EnterCriticalSection(&CSMutex);
|
EnterCriticalSection(&CSMutex);
|
||||||
if(m_waiters > 0)
|
if(m_waiters > 0)
|
||||||
SetEvent(m_events[SignalEvent]);
|
SetEvent(m_events[SignalEvent]);
|
||||||
LeaveCriticalSection(&CSMutex);
|
LeaveCriticalSection(&CSMutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Condition::SignalAll()
|
void Condition::SignalAll()
|
||||||
{
|
{
|
||||||
EnterCriticalSection(&CSMutex);
|
EnterCriticalSection(&CSMutex);
|
||||||
if(m_waiters > 0)
|
if(m_waiters > 0)
|
||||||
SetEvent(m_events[BroadcastEvent]);
|
SetEvent(m_events[BroadcastEvent]);
|
||||||
LeaveCriticalSection(&CSMutex);
|
LeaveCriticalSection(&CSMutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Condition::Wait()
|
void Condition::Wait()
|
||||||
{
|
{
|
||||||
EnterCriticalSection(&CSMutex);
|
EnterCriticalSection(&CSMutex);
|
||||||
|
|
||||||
m_waiters++;
|
m_waiters++;
|
||||||
@ -85,49 +77,51 @@ void Condition::Wait()
|
|||||||
ResetEvent(m_events[BroadcastEvent]);
|
ResetEvent(m_events[BroadcastEvent]);
|
||||||
|
|
||||||
LeaveCriticalSection(&CSMutex);
|
LeaveCriticalSection(&CSMutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#else //!WIN32
|
Condition::Condition()
|
||||||
|
{
|
||||||
Condition::Condition()
|
|
||||||
{
|
|
||||||
pthread_cond_init(&cond,nullptr);
|
pthread_cond_init(&cond,nullptr);
|
||||||
pthread_mutex_init(&mutex,nullptr);
|
pthread_mutex_init(&mutex,nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Condition::Signal()
|
void Condition::Signal()
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
pthread_cond_signal(&cond);
|
pthread_cond_signal(&cond);
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Condition::SignalAll()
|
void Condition::SignalAll()
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
pthread_cond_broadcast(&cond);
|
pthread_cond_broadcast(&cond);
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Condition::Wait()
|
void Condition::Wait()
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
pthread_cond_wait(&cond,&mutex);
|
pthread_cond_wait(&cond,&mutex);
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
I commented this specifically because I think it might be very
|
I commented this specifically because I think it might be very
|
||||||
difficult to write a windows counterpart to it, so I would like
|
difficult to write a windows counterpart to it, so I would like
|
||||||
to discourage its use until we can confirm that it can be reasonably
|
to discourage its use until we can confirm that it can be reasonably
|
||||||
implemented on windows.
|
implemented on windows.
|
||||||
|
|
||||||
bool Condition::TimedWait(unsigned long usec)
|
bool Condition::TimedWait(unsigned long usec)
|
||||||
{
|
{
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
struct timespec timeout;
|
struct timespec timeout;
|
||||||
int retcode=0;
|
int retcode=0;
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
gettimeofday(&now,nullptr);
|
gettimeofday(&now,nullptr);
|
||||||
now.tv_usec+=usec;
|
now.tv_usec+=usec;
|
||||||
@ -139,15 +133,16 @@ int retcode=0;
|
|||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
return retcode!=ETIMEDOUT;
|
return retcode!=ETIMEDOUT;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Condition::~Condition()
|
Condition::~Condition()
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
pthread_cond_destroy(&cond);
|
pthread_cond_destroy(&cond);
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
pthread_mutex_destroy(&mutex);
|
pthread_mutex_destroy(&mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user