Skip to content

Commit

Permalink
xrCore: now Event and Lock common for all platform
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleivg committed Nov 6, 2018
1 parent 0469849 commit 7eafbf4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 65 deletions.
69 changes: 24 additions & 45 deletions src/xrCore/Threading/Event.cpp
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
12 changes: 3 additions & 9 deletions src/xrCore/Threading/Event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@
class XRCORE_API Event
{
void* handle;
#if defined(LINUX)
struct EventHandle
{
pthread_mutex_t mutex;
pthread_cond_t cond;
bool signaled;
};

private:
EventHandle m_id;
#endif
SDL_mutex* mutex;
SDL_cond* cond;
bool signaled;

public:
Event() noexcept;
Expand Down
11 changes: 0 additions & 11 deletions src/xrCore/Threading/Lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@

struct LockImpl
{
#ifdef WINDOWS
CRITICAL_SECTION cs;

LockImpl() { InitializeCriticalSection(&cs); }
~LockImpl() { DeleteCriticalSection(&cs); }

ICF void Lock() { EnterCriticalSection(&cs); }
ICF void Unlock() { LeaveCriticalSection(&cs); }
ICF bool TryLock() { return !!TryEnterCriticalSection(&cs); }
#else
std::recursive_mutex mutex;

ICF void Lock() { mutex.lock(); }
ICF void Unlock() { mutex.unlock(); }
ICF bool TryLock() { return mutex.try_lock(); }
#endif
};

#ifdef CONFIG_PROFILE_LOCKS
Expand Down

0 comments on commit 7eafbf4

Please sign in to comment.