2015-11-06 11:42:27 +00:00
|
|
|
#include "stdafx.h"
|
2016-04-28 07:29:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
2015-11-06 11:42:27 +00:00
|
|
|
|
|
|
|
SyncEvent::SyncEvent(bool bManualReset)
|
|
|
|
{
|
2016-04-28 07:29:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
m_Event = CreateEvent(NULL, bManualReset, FALSE, NULL);
|
|
|
|
#else
|
|
|
|
m_signalled = false;
|
2017-04-29 05:42:02 +00:00
|
|
|
m_Event = new pthread_mutex_t;
|
2016-04-28 07:29:27 +00:00
|
|
|
m_cond = new pthread_cond_t;
|
2017-04-29 05:42:02 +00:00
|
|
|
pthread_mutex_init((pthread_mutex_t*)m_Event, NULL);
|
2016-04-28 07:29:27 +00:00
|
|
|
pthread_cond_init((pthread_cond_t*)m_cond, NULL);
|
|
|
|
#endif
|
2015-11-06 11:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SyncEvent::~SyncEvent()
|
|
|
|
{
|
2016-04-28 07:29:27 +00:00
|
|
|
#ifdef _WIN32
|
2015-11-06 11:42:27 +00:00
|
|
|
CloseHandle(m_Event);
|
2016-04-28 07:29:27 +00:00
|
|
|
#else
|
2017-04-29 05:42:02 +00:00
|
|
|
pthread_mutex_destroy((pthread_mutex_t*)m_Event);
|
2016-04-28 07:29:27 +00:00
|
|
|
pthread_cond_destroy((pthread_cond_t*)m_cond);
|
|
|
|
delete (pthread_mutex_t*)m_Event;
|
|
|
|
delete (pthread_cond_t*)m_cond;
|
|
|
|
#endif
|
2015-11-06 11:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SyncEvent::Trigger()
|
|
|
|
{
|
2016-04-28 07:29:27 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-29 05:42:02 +00:00
|
|
|
SetEvent(m_Event);
|
2016-04-28 07:29:27 +00:00
|
|
|
#else
|
|
|
|
pthread_mutex_lock((pthread_mutex_t*)m_Event);
|
|
|
|
m_signalled = true;
|
|
|
|
pthread_mutex_unlock((pthread_mutex_t*)m_Event);
|
|
|
|
pthread_cond_signal((pthread_cond_t*)m_cond);
|
|
|
|
#endif
|
2015-11-06 11:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SyncEvent::IsTriggered(int32_t iWaitTime)
|
|
|
|
{
|
2016-04-28 07:29:27 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-29 05:42:02 +00:00
|
|
|
return (WAIT_OBJECT_0 == WaitForSingleObject(m_Event, iWaitTime));
|
2016-04-28 07:29:27 +00:00
|
|
|
#else
|
|
|
|
pthread_mutex_lock((pthread_mutex_t*)m_Event);
|
|
|
|
while (!m_signalled)
|
|
|
|
{
|
|
|
|
pthread_cond_wait((pthread_cond_t*)m_cond, (pthread_mutex_t*)m_Event);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock((pthread_mutex_t*)m_Event);
|
2017-04-29 05:42:02 +00:00
|
|
|
Reset();
|
2016-11-27 20:23:12 +00:00
|
|
|
return true;
|
2016-04-28 07:29:27 +00:00
|
|
|
#endif
|
2015-11-06 11:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SyncEvent::Reset()
|
|
|
|
{
|
2016-04-28 07:29:27 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-29 05:42:02 +00:00
|
|
|
ResetEvent(m_Event);
|
2016-04-28 07:29:27 +00:00
|
|
|
#else
|
2017-04-29 05:42:02 +00:00
|
|
|
pthread_mutex_lock((pthread_mutex_t*)m_Event);
|
|
|
|
m_signalled = false;
|
|
|
|
pthread_mutex_unlock((pthread_mutex_t*)m_Event);
|
2016-04-28 07:29:27 +00:00
|
|
|
#endif
|
2015-11-06 11:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void * SyncEvent::GetHandle()
|
|
|
|
{
|
|
|
|
return m_Event;
|
2021-03-16 23:00:53 +00:00
|
|
|
}
|