project64/Source/Common/SyncEvent.cpp

75 lines
1.6 KiB
C++
Raw Permalink Normal View History

2021-04-12 09:41:28 +00:00
#include "SyncEvent.h"
#ifdef _WIN32
#include <Windows.h>
#else
#include <pthread.h>
#endif
2015-11-06 11:42:27 +00:00
SyncEvent::SyncEvent(bool bManualReset)
{
#ifdef _WIN32
2021-04-12 11:35:39 +00:00
m_Event = CreateEvent(nullptr, bManualReset, FALSE, nullptr);
#else
m_signalled = false;
m_Event = new pthread_mutex_t;
m_cond = new pthread_cond_t;
2022-10-03 08:04:42 +00:00
pthread_mutex_init((pthread_mutex_t *)m_Event, nullptr);
pthread_cond_init((pthread_cond_t *)m_cond, nullptr);
#endif
2015-11-06 11:42:27 +00:00
}
SyncEvent::~SyncEvent()
{
#ifdef _WIN32
2015-11-06 11:42:27 +00:00
CloseHandle(m_Event);
#else
2022-10-03 08:04:42 +00:00
pthread_mutex_destroy((pthread_mutex_t *)m_Event);
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()
{
#ifdef _WIN32
SetEvent(m_Event);
#else
2022-10-03 08:04:42 +00:00
pthread_mutex_lock((pthread_mutex_t *)m_Event);
m_signalled = true;
2022-10-03 08:04:42 +00:00
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) const
2015-11-06 11:42:27 +00:00
{
#ifdef _WIN32
return (WAIT_OBJECT_0 == WaitForSingleObject(m_Event, iWaitTime));
#else
2022-10-03 08:04:42 +00:00
pthread_mutex_lock((pthread_mutex_t *)m_Event);
while (!m_signalled)
{
2022-10-03 08:04:42 +00:00
pthread_cond_wait((pthread_cond_t *)m_cond, (pthread_mutex_t *)m_Event);
}
2022-10-03 08:04:42 +00:00
pthread_mutex_unlock((pthread_mutex_t *)m_Event);
2016-11-27 20:23:12 +00:00
return true;
#endif
2015-11-06 11:42:27 +00:00
}
void SyncEvent::Reset()
{
#ifdef _WIN32
ResetEvent(m_Event);
#else
2022-10-03 08:04:42 +00:00
pthread_mutex_lock((pthread_mutex_t *)m_Event);
m_signalled = false;
2022-10-03 08:04:42 +00:00
pthread_mutex_unlock((pthread_mutex_t *)m_Event);
#endif
2015-11-06 11:42:27 +00:00
}
void * SyncEvent::GetHandle()
{
return m_Event;
2021-03-16 23:00:53 +00:00
}