2021-04-12 09:41:28 +00:00
|
|
|
#include "SyncEvent.h"
|
2016-04-28 07:29:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
2021-06-28 01:57:35 +00:00
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
2016-04-28 07:29:27 +00:00
|
|
|
#endif
|
2015-11-06 11:42:27 +00:00
|
|
|
|
|
|
|
SyncEvent::SyncEvent(bool bManualReset)
|
|
|
|
{
|
2016-04-28 07:29:27 +00:00
|
|
|
#ifdef _WIN32
|
2021-04-12 11:35:39 +00:00
|
|
|
m_Event = CreateEvent(nullptr, bManualReset, FALSE, nullptr);
|
2016-04-28 07:29:27 +00:00
|
|
|
#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;
|
2021-04-12 11:35:39 +00:00
|
|
|
pthread_mutex_init((pthread_mutex_t*)m_Event, nullptr);
|
|
|
|
pthread_cond_init((pthread_cond_t*)m_cond, nullptr);
|
2016-04-28 07:29:27 +00:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2022-07-11 10:00:25 +00:00
|
|
|
bool SyncEvent::IsTriggered(int32_t iWaitTime) const
|
2015-11-06 11:42:27 +00:00
|
|
|
{
|
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);
|
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
|
|
|
}
|