-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xrCore: now Event and Lock common for all platform
- Loading branch information
Showing
3 changed files
with
27 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,61 @@ | ||
#include "stdafx.h" | ||
#include "Event.hpp" | ||
#if defined(WINDOWS) | ||
|
||
Event::Event() noexcept { handle = (void*)CreateEvent(NULL, FALSE, FALSE, NULL); } | ||
Event::~Event() noexcept { CloseHandle(handle); } | ||
void Event::Reset() noexcept { ResetEvent(handle); } | ||
void Event::Set() noexcept { SetEvent(handle); } | ||
void Event::Wait() noexcept { WaitForSingleObject(handle, INFINITE); } | ||
bool Event::Wait(u32 millisecondsTimeout) noexcept | ||
{ | ||
return WaitForSingleObject(handle, millisecondsTimeout) != WAIT_TIMEOUT; | ||
} | ||
#elif defined(LINUX) | ||
#include <pthread.h> | ||
Event::Event() noexcept | ||
{ | ||
m_id.signaled = false; | ||
pthread_mutex_init(&m_id.mutex, nullptr); | ||
pthread_cond_init(&m_id.cond, nullptr); | ||
signaled = false; | ||
mutex = SDL_CreateMutex(); | ||
cond = SDL_CreateCond(); | ||
} | ||
Event::~Event() noexcept | ||
{ | ||
pthread_mutex_destroy(&m_id.mutex); | ||
pthread_cond_destroy(&m_id.cond); | ||
SDL_DestroyMutex(mutex); | ||
SDL_DestroyCond(cond); | ||
} | ||
void Event::Reset() noexcept | ||
{ | ||
pthread_mutex_lock(&m_id.mutex); | ||
pthread_cond_signal(&m_id.cond); | ||
m_id.signaled = false; | ||
pthread_mutex_unlock(&m_id.mutex); | ||
SDL_LockMutex(mutex); | ||
SDL_CondSignal(cond); | ||
signaled = false; | ||
SDL_UnlockMutex(mutex); | ||
} | ||
void Event::Set() noexcept | ||
{ | ||
pthread_mutex_lock(&m_id.mutex); | ||
pthread_cond_signal(&m_id.cond); | ||
m_id.signaled = true; | ||
pthread_mutex_unlock(&m_id.mutex); | ||
SDL_LockMutex(mutex); | ||
SDL_CondSignal(cond); | ||
signaled = true; | ||
SDL_UnlockMutex(mutex); | ||
} | ||
void Event::Wait() noexcept | ||
{ | ||
pthread_mutex_lock(&m_id.mutex); | ||
SDL_LockMutex(mutex); | ||
|
||
while (!m_id.signaled) | ||
while (!signaled) | ||
{ | ||
pthread_cond_wait(&m_id.cond, &m_id.mutex); | ||
SDL_CondWait(cond, mutex); | ||
} | ||
m_id.signaled = false; // due in WaitForSingleObject() "Before returning, a wait function modifies the state of some types of synchronization" | ||
signaled = false; // due in WaitForSingleObject() "Before returning, a wait function modifies the state of some types of synchronization" | ||
|
||
pthread_mutex_unlock(&m_id.mutex); | ||
SDL_UnlockMutex(mutex); | ||
} | ||
bool Event::Wait(u32 millisecondsTimeout) noexcept | ||
{ | ||
bool result = false; | ||
pthread_mutex_lock(&m_id.mutex); | ||
SDL_LockMutex(mutex); | ||
|
||
timespec ts; | ||
clock_gettime(CLOCK_REALTIME, &ts); | ||
ts.tv_nsec += (long) millisecondsTimeout * 1000 * 1000; | ||
if(ts.tv_nsec > 1000000000) | ||
while(!signaled) | ||
{ | ||
ts.tv_nsec -= 1000000000; | ||
ts.tv_sec += 1; | ||
} | ||
int res = SDL_CondWaitTimeout(cond, mutex, millisecondsTimeout); | ||
|
||
while(!m_id.signaled) | ||
{ | ||
int res = pthread_cond_timedwait(&m_id.cond, &m_id.mutex, &ts); | ||
if(res == ETIMEDOUT) | ||
if (res == SDL_MUTEX_TIMEDOUT) | ||
{ | ||
result = true; | ||
break; | ||
} | ||
} | ||
m_id.signaled = false; | ||
signaled = false; | ||
|
||
pthread_mutex_unlock(&m_id.mutex); | ||
SDL_UnlockMutex(mutex); | ||
|
||
return result; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters