Skip to content

Commit 024fb27

Browse files
committed
Revert "xrCore: now Event and Lock common for all platform" due possible performance issues
This reverts commit 7eafbf4.
1 parent b1799b8 commit 024fb27

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

src/xrCore/Threading/Event.cpp

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

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>
416
Event::Event() noexcept
517
{
6-
signaled = false;
7-
mutex = SDL_CreateMutex();
8-
cond = SDL_CreateCond();
18+
m_id.signaled = false;
19+
pthread_mutex_init(&m_id.mutex, nullptr);
20+
pthread_cond_init(&m_id.cond, nullptr);
921
}
1022
Event::~Event() noexcept
1123
{
12-
SDL_DestroyMutex(mutex);
13-
SDL_DestroyCond(cond);
24+
pthread_mutex_destroy(&m_id.mutex);
25+
pthread_cond_destroy(&m_id.cond);
1426
}
1527
void Event::Reset() noexcept
1628
{
17-
SDL_LockMutex(mutex);
18-
SDL_CondSignal(cond);
19-
signaled = false;
20-
SDL_UnlockMutex(mutex);
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);
2133
}
2234
void Event::Set() noexcept
2335
{
24-
SDL_LockMutex(mutex);
25-
SDL_CondSignal(cond);
26-
signaled = true;
27-
SDL_UnlockMutex(mutex);
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);
2840
}
2941
void Event::Wait() noexcept
3042
{
31-
SDL_LockMutex(mutex);
43+
pthread_mutex_lock(&m_id.mutex);
3244

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

39-
SDL_UnlockMutex(mutex);
51+
pthread_mutex_unlock(&m_id.mutex);
4052
}
4153
bool Event::Wait(u32 millisecondsTimeout) noexcept
4254
{
4355
bool result = false;
44-
SDL_LockMutex(mutex);
56+
pthread_mutex_lock(&m_id.mutex);
4557

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

50-
if (res == SDL_MUTEX_TIMEDOUT)
67+
while(!m_id.signaled)
68+
{
69+
int res = pthread_cond_timedwait(&m_id.cond, &m_id.mutex, &ts);
70+
if(res == ETIMEDOUT)
5171
{
5272
result = true;
5373
break;
5474
}
5575
}
56-
signaled = false;
76+
m_id.signaled = false;
5777

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

6080
return result;
6181
}
82+
#endif

src/xrCore/Threading/Event.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
class XRCORE_API Event
77
{
88
void* handle;
9+
#if defined(LINUX)
10+
struct EventHandle
11+
{
12+
pthread_mutex_t mutex;
13+
pthread_cond_t cond;
14+
bool signaled;
15+
};
916

1017
private:
11-
SDL_mutex* mutex;
12-
SDL_cond* cond;
13-
bool signaled;
18+
EventHandle m_id;
19+
#endif
1420

1521
public:
1622
Event() noexcept;

src/xrCore/Threading/Lock.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
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
717
std::recursive_mutex mutex;
818

919
ICF void Lock() { mutex.lock(); }
1020
ICF void Unlock() { mutex.unlock(); }
1121
ICF bool TryLock() { return mutex.try_lock(); }
22+
#endif
1223
};
1324

1425
#ifdef CONFIG_PROFILE_LOCKS

0 commit comments

Comments
 (0)