Skip to content

Commit 7eafbf4

Browse files
committed
xrCore: now Event and Lock common for all platform
1 parent 0469849 commit 7eafbf4

File tree

3 files changed

+27
-65
lines changed

3 files changed

+27
-65
lines changed

src/xrCore/Threading/Event.cpp

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,61 @@
11
#include "stdafx.h"
22
#include "Event.hpp"
3-
#if defined(WINDOWS)
43

5-
Event::Event() noexcept { handle = (void*)CreateEvent(NULL, FALSE, FALSE, NULL); }
6-
Event::~Event() noexcept { CloseHandle(handle); }
7-
void Event::Reset() noexcept { ResetEvent(handle); }
8-
void Event::Set() noexcept { SetEvent(handle); }
9-
void Event::Wait() noexcept { WaitForSingleObject(handle, INFINITE); }
10-
bool Event::Wait(u32 millisecondsTimeout) noexcept
11-
{
12-
return WaitForSingleObject(handle, millisecondsTimeout) != WAIT_TIMEOUT;
13-
}
14-
#elif defined(LINUX)
15-
#include <pthread.h>
164
Event::Event() noexcept
175
{
18-
m_id.signaled = false;
19-
pthread_mutex_init(&m_id.mutex, nullptr);
20-
pthread_cond_init(&m_id.cond, nullptr);
6+
signaled = false;
7+
mutex = SDL_CreateMutex();
8+
cond = SDL_CreateCond();
219
}
2210
Event::~Event() noexcept
2311
{
24-
pthread_mutex_destroy(&m_id.mutex);
25-
pthread_cond_destroy(&m_id.cond);
12+
SDL_DestroyMutex(mutex);
13+
SDL_DestroyCond(cond);
2614
}
2715
void Event::Reset() noexcept
2816
{
29-
pthread_mutex_lock(&m_id.mutex);
30-
pthread_cond_signal(&m_id.cond);
31-
m_id.signaled = false;
32-
pthread_mutex_unlock(&m_id.mutex);
17+
SDL_LockMutex(mutex);
18+
SDL_CondSignal(cond);
19+
signaled = false;
20+
SDL_UnlockMutex(mutex);
3321
}
3422
void Event::Set() noexcept
3523
{
36-
pthread_mutex_lock(&m_id.mutex);
37-
pthread_cond_signal(&m_id.cond);
38-
m_id.signaled = true;
39-
pthread_mutex_unlock(&m_id.mutex);
24+
SDL_LockMutex(mutex);
25+
SDL_CondSignal(cond);
26+
signaled = true;
27+
SDL_UnlockMutex(mutex);
4028
}
4129
void Event::Wait() noexcept
4230
{
43-
pthread_mutex_lock(&m_id.mutex);
31+
SDL_LockMutex(mutex);
4432

45-
while (!m_id.signaled)
33+
while (!signaled)
4634
{
47-
pthread_cond_wait(&m_id.cond, &m_id.mutex);
35+
SDL_CondWait(cond, mutex);
4836
}
49-
m_id.signaled = false; // due in WaitForSingleObject() "Before returning, a wait function modifies the state of some types of synchronization"
37+
signaled = false; // due in WaitForSingleObject() "Before returning, a wait function modifies the state of some types of synchronization"
5038

51-
pthread_mutex_unlock(&m_id.mutex);
39+
SDL_UnlockMutex(mutex);
5240
}
5341
bool Event::Wait(u32 millisecondsTimeout) noexcept
5442
{
5543
bool result = false;
56-
pthread_mutex_lock(&m_id.mutex);
44+
SDL_LockMutex(mutex);
5745

58-
timespec ts;
59-
clock_gettime(CLOCK_REALTIME, &ts);
60-
ts.tv_nsec += (long) millisecondsTimeout * 1000 * 1000;
61-
if(ts.tv_nsec > 1000000000)
46+
while(!signaled)
6247
{
63-
ts.tv_nsec -= 1000000000;
64-
ts.tv_sec += 1;
65-
}
48+
int res = SDL_CondWaitTimeout(cond, mutex, millisecondsTimeout);
6649

67-
while(!m_id.signaled)
68-
{
69-
int res = pthread_cond_timedwait(&m_id.cond, &m_id.mutex, &ts);
70-
if(res == ETIMEDOUT)
50+
if (res == SDL_MUTEX_TIMEDOUT)
7151
{
7252
result = true;
7353
break;
7454
}
7555
}
76-
m_id.signaled = false;
56+
signaled = false;
7757

78-
pthread_mutex_unlock(&m_id.mutex);
58+
SDL_UnlockMutex(mutex);
7959

8060
return result;
8161
}
82-
#endif

src/xrCore/Threading/Event.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44
class XRCORE_API Event
55
{
66
void* handle;
7-
#if defined(LINUX)
8-
struct EventHandle
9-
{
10-
pthread_mutex_t mutex;
11-
pthread_cond_t cond;
12-
bool signaled;
13-
};
147

158
private:
16-
EventHandle m_id;
17-
#endif
9+
SDL_mutex* mutex;
10+
SDL_cond* cond;
11+
bool signaled;
1812

1913
public:
2014
Event() noexcept;

src/xrCore/Threading/Lock.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,11 @@
44

55
struct LockImpl
66
{
7-
#ifdef WINDOWS
8-
CRITICAL_SECTION cs;
9-
10-
LockImpl() { InitializeCriticalSection(&cs); }
11-
~LockImpl() { DeleteCriticalSection(&cs); }
12-
13-
ICF void Lock() { EnterCriticalSection(&cs); }
14-
ICF void Unlock() { LeaveCriticalSection(&cs); }
15-
ICF bool TryLock() { return !!TryEnterCriticalSection(&cs); }
16-
#else
177
std::recursive_mutex mutex;
188

199
ICF void Lock() { mutex.lock(); }
2010
ICF void Unlock() { mutex.unlock(); }
2111
ICF bool TryLock() { return mutex.try_lock(); }
22-
#endif
2312
};
2413

2514
#ifdef CONFIG_PROFILE_LOCKS

0 commit comments

Comments
 (0)