[Common] Make SyncEvent.cpp more android friendly

This commit is contained in:
zilmar 2016-04-28 17:29:27 +10:00
parent b2545c108b
commit 1594c2bed9
2 changed files with 49 additions and 5 deletions

View File

@ -1,29 +1,69 @@
#include "stdafx.h" #include "stdafx.h"
#include <windows.h> #ifdef _WIN32
#include <Windows.h>
#endif
SyncEvent::SyncEvent(bool bManualReset) SyncEvent::SyncEvent(bool bManualReset)
{ {
m_Event = CreateEvent(NULL, bManualReset, false, NULL); #ifdef _WIN32
m_Event = CreateEvent(NULL, bManualReset, FALSE, NULL);
#else
m_signalled = false;
m_Event = new pthread_mutex_t;
m_cond = new pthread_cond_t;
pthread_mutex_init((pthread_mutex_t*)m_Event,NULL);
pthread_cond_init((pthread_cond_t*)m_cond, NULL);
#endif
} }
SyncEvent::~SyncEvent() SyncEvent::~SyncEvent()
{ {
#ifdef _WIN32
CloseHandle(m_Event); CloseHandle(m_Event);
#else
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
} }
void SyncEvent::Trigger() void SyncEvent::Trigger()
{ {
SetEvent(m_Event); #ifdef _WIN32
SetEvent(m_Event);
#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
} }
bool SyncEvent::IsTriggered(int32_t iWaitTime) bool SyncEvent::IsTriggered(int32_t iWaitTime)
{ {
return (WAIT_OBJECT_0 == WaitForSingleObject(m_Event,iWaitTime)); #ifdef _WIN32
return (WAIT_OBJECT_0 == WaitForSingleObject(m_Event,iWaitTime));
#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);
}
m_signalled = false;
pthread_mutex_unlock((pthread_mutex_t*)m_Event);
#endif
} }
void SyncEvent::Reset() void SyncEvent::Reset()
{ {
ResetEvent(m_Event); #ifdef _WIN32
ResetEvent(m_Event);
#else
#ifdef tofix
pthread_mutex_unlock(&evt.m_mutex);
#endif
#endif
} }
void * SyncEvent::GetHandle() void * SyncEvent::GetHandle()

View File

@ -18,4 +18,8 @@ protected:
SyncEvent& operator=(const SyncEvent&); // Disable assignment SyncEvent& operator=(const SyncEvent&); // Disable assignment
void * m_Event; void * m_Event;
#ifndef _WIN32
void * m_cond;
bool m_signalled;
#endif
}; };