|
1 | 1 | #include "stdafx.h" |
2 | 2 | #include "Event.hpp" |
| 3 | +#if defined(WINDOWS) |
3 | 4 |
|
| 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> |
4 | 16 | Event::Event() noexcept |
5 | 17 | { |
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); |
9 | 21 | } |
10 | 22 | Event::~Event() noexcept |
11 | 23 | { |
12 | | - SDL_DestroyMutex(mutex); |
13 | | - SDL_DestroyCond(cond); |
| 24 | + pthread_mutex_destroy(&m_id.mutex); |
| 25 | + pthread_cond_destroy(&m_id.cond); |
14 | 26 | } |
15 | 27 | void Event::Reset() noexcept |
16 | 28 | { |
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); |
21 | 33 | } |
22 | 34 | void Event::Set() noexcept |
23 | 35 | { |
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); |
28 | 40 | } |
29 | 41 | void Event::Wait() noexcept |
30 | 42 | { |
31 | | - SDL_LockMutex(mutex); |
| 43 | + pthread_mutex_lock(&m_id.mutex); |
32 | 44 |
|
33 | | - while (!signaled) |
| 45 | + while (!m_id.signaled) |
34 | 46 | { |
35 | | - SDL_CondWait(cond, mutex); |
| 47 | + pthread_cond_wait(&m_id.cond, &m_id.mutex); |
36 | 48 | } |
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" |
38 | 50 |
|
39 | | - SDL_UnlockMutex(mutex); |
| 51 | + pthread_mutex_unlock(&m_id.mutex); |
40 | 52 | } |
41 | 53 | bool Event::Wait(u32 millisecondsTimeout) noexcept |
42 | 54 | { |
43 | 55 | bool result = false; |
44 | | - SDL_LockMutex(mutex); |
| 56 | + pthread_mutex_lock(&m_id.mutex); |
45 | 57 |
|
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) |
47 | 62 | { |
48 | | - int res = SDL_CondWaitTimeout(cond, mutex, millisecondsTimeout); |
| 63 | + ts.tv_nsec -= 1000000000; |
| 64 | + ts.tv_sec += 1; |
| 65 | + } |
49 | 66 |
|
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) |
51 | 71 | { |
52 | 72 | result = true; |
53 | 73 | break; |
54 | 74 | } |
55 | 75 | } |
56 | | - signaled = false; |
| 76 | + m_id.signaled = false; |
57 | 77 |
|
58 | | - SDL_UnlockMutex(mutex); |
| 78 | + pthread_mutex_unlock(&m_id.mutex); |
59 | 79 |
|
60 | 80 | return result; |
61 | 81 | } |
| 82 | +#endif |
0 commit comments