Skip to content

Commit 243b268

Browse files
committed
xrCore: implemented Event for Linux (from LumixEngine under MIT License)
1 parent bc58018 commit 243b268

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

src/xrCore/Threading/Event.cpp

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,75 @@ Event::Event() noexcept { handle = (void*)CreateEvent(NULL, FALSE, FALSE, NULL);
77
Event::~Event() noexcept { CloseHandle(handle); }
88
void Event::Reset() noexcept { ResetEvent(handle); }
99
void Event::Set() noexcept { SetEvent(handle); }
10-
void Event::Wait() const noexcept { WaitForSingleObject(handle, INFINITE); }
11-
bool Event::Wait(u32 millisecondsTimeout) const noexcept
10+
void Event::Wait() noexcept { WaitForSingleObject(handle, INFINITE); }
11+
bool Event::Wait(u32 millisecondsTimeout) noexcept
1212
{
1313
return WaitForSingleObject(handle, millisecondsTimeout) != WAIT_TIMEOUT;
1414
}
1515
#elif defined(LINUX)
16-
Event::Event() noexcept { handle = (void*)malloc(1); }
17-
Event::~Event() noexcept { free(handle); }
18-
void Event::Reset() noexcept { memset(handle, 1, 1); }
19-
void Event::Set() noexcept { memset(handle, 0, 1); }
20-
void Event::Wait() const noexcept { Sleep(0); }
21-
bool Event::Wait(u32 millisecondsTimeout) const noexcept
16+
#include <pthread.h>
17+
Event::Event() noexcept
2218
{
23-
Sleep(millisecondsTimeout);
24-
return true;
19+
m_id.signaled = false;
20+
pthread_mutex_init(&m_id.mutex, nullptr);
21+
pthread_cond_init(&m_id.cond, nullptr);
22+
}
23+
Event::~Event() noexcept
24+
{
25+
pthread_mutex_destroy(&m_id.mutex);
26+
pthread_cond_destroy(&m_id.cond);
27+
}
28+
void Event::Reset() noexcept
29+
{
30+
pthread_mutex_lock(&m_id.mutex);
31+
pthread_cond_signal(&m_id.cond);
32+
m_id.signaled = false;
33+
pthread_mutex_unlock(&m_id.mutex);
34+
}
35+
void Event::Set() noexcept
36+
{
37+
pthread_mutex_lock(&m_id.mutex);
38+
pthread_cond_signal(&m_id.cond);
39+
m_id.signaled = true;
40+
pthread_mutex_unlock(&m_id.mutex);
41+
}
42+
void Event::Wait() noexcept
43+
{
44+
pthread_mutex_lock(&m_id.mutex);
45+
46+
while (!m_id.signaled)
47+
{
48+
pthread_cond_wait(&m_id.cond, &m_id.mutex);
49+
}
50+
51+
pthread_mutex_unlock(&m_id.mutex);
52+
}
53+
bool Event::Wait(u32 millisecondsTimeout) noexcept
54+
{
55+
bool result = false;
56+
pthread_mutex_lock(&m_id.mutex);
57+
58+
timespec ts;
59+
clock_gettime(CLOCK_REALTIME, &ts);
60+
ts.tv_nsec += (long) millisecondsTimeout * 1000 * 1000;
61+
if(ts.tv_nsec > 1000000000)
62+
{
63+
ts.tv_nsec -= 1000000000;
64+
ts.tv_sec += 1;
65+
}
66+
67+
while(!m_id.signaled)
68+
{
69+
int res = pthread_cond_timedwait(&m_id.cond, &m_id.mutex, &ts);
70+
if(res == ETIMEDOUT)
71+
{
72+
result = true;
73+
break;
74+
}
75+
}
76+
77+
pthread_mutex_unlock(&m_id.mutex);
78+
79+
return result;
2580
}
2681
#endif

src/xrCore/Threading/Event.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@
33

44
class XRCORE_API Event
55
{
6+
#if defined(WINDOWS)
67
void* handle;
8+
#elif defined(LINUX)
9+
struct EventHandle
10+
{
11+
pthread_mutex_t mutex;
12+
pthread_cond_t cond;
13+
bool signaled;
14+
};
15+
pthread_mutex_t handle;
16+
17+
private:
18+
EventHandle m_id;
19+
#endif
720

821
public:
922
Event() noexcept;

0 commit comments

Comments
 (0)