Skip to content

Commit db2894d

Browse files
committed
xrCore: fixed linux Event implementation according Windows
1 parent a66e449 commit db2894d

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

src/xrCore/Threading/Event.cpp

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,66 +15,68 @@ bool Event::Wait(u32 millisecondsTimeout) noexcept
1515
#include <pthread.h>
1616
Event::Event() noexcept
1717
{
18-
m_id.signaled = false;
19-
pthread_mutex_init(&m_id.mutex, nullptr);
20-
pthread_cond_init(&m_id.cond, nullptr);
18+
m_id.signaled = false;
19+
pthread_mutex_init(&m_id.mutex, nullptr);
20+
pthread_cond_init(&m_id.cond, nullptr);
2121
}
2222
Event::~Event() noexcept
2323
{
24-
pthread_mutex_destroy(&m_id.mutex);
25-
pthread_cond_destroy(&m_id.cond);
24+
pthread_mutex_destroy(&m_id.mutex);
25+
pthread_cond_destroy(&m_id.cond);
2626
}
2727
void Event::Reset() noexcept
2828
{
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);
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);
3333
}
3434
void Event::Set() noexcept
3535
{
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);
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);
4040
}
4141
void Event::Wait() noexcept
4242
{
43-
pthread_mutex_lock(&m_id.mutex);
43+
pthread_mutex_lock(&m_id.mutex);
4444

45-
while (!m_id.signaled)
46-
{
47-
pthread_cond_wait(&m_id.cond, &m_id.mutex);
48-
}
45+
while (!m_id.signaled)
46+
{
47+
pthread_cond_wait(&m_id.cond, &m_id.mutex);
48+
}
49+
m_id.signaled = false; // due in WaitForSingleObject() "Before returning, a wait function modifies the state of some types of synchronization"
4950

50-
pthread_mutex_unlock(&m_id.mutex);
51+
pthread_mutex_unlock(&m_id.mutex);
5152
}
5253
bool Event::Wait(u32 millisecondsTimeout) noexcept
5354
{
54-
bool result = false;
55-
pthread_mutex_lock(&m_id.mutex);
55+
bool result = false;
56+
pthread_mutex_lock(&m_id.mutex);
5657

57-
timespec ts;
58-
clock_gettime(CLOCK_REALTIME, &ts);
59-
ts.tv_nsec += (long) millisecondsTimeout * 1000 * 1000;
60-
if(ts.tv_nsec > 1000000000)
61-
{
62-
ts.tv_nsec -= 1000000000;
63-
ts.tv_sec += 1;
64-
}
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+
}
6566

66-
while(!m_id.signaled)
67-
{
68-
int res = pthread_cond_timedwait(&m_id.cond, &m_id.mutex, &ts);
69-
if(res == ETIMEDOUT)
70-
{
71-
result = true;
72-
break;
73-
}
74-
}
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+
m_id.signaled = false;
7577

76-
pthread_mutex_unlock(&m_id.mutex);
78+
pthread_mutex_unlock(&m_id.mutex);
7779

78-
return result;
80+
return result;
7981
}
8082
#endif

0 commit comments

Comments
 (0)