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