@@ -7,20 +7,75 @@ Event::Event() noexcept { handle = (void*)CreateEvent(NULL, FALSE, FALSE, NULL);
77Event::~Event () noexcept { CloseHandle (handle); }
88void Event::Reset () noexcept { ResetEvent (handle); }
99void 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
0 commit comments