blob: 7007b5dd2c02f9a689dde6191ff8541abd5b2381 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
// Event.cpp
// Implements the cEvent object representing an OS-specific synchronization primitive that can be waited-for
// Implemented as an Event on Win and as a 1-semaphore on *nix
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Event.h"
#include "Errors.h"
cEvent::cEvent(void) :
m_ShouldWait(true)
{
}
void cEvent::Wait(void)
{
std::unique_lock<std::mutex> Lock(m_Mutex);
while (m_ShouldWait)
{
m_CondVar.wait(Lock);
}
m_ShouldWait = true;
}
bool cEvent::Wait(int a_TimeoutMSec)
{
std::chrono::system_clock::time_point dst = std::chrono::system_clock::now() + std::chrono::milliseconds(a_TimeoutMSec);
std::unique_lock<std::mutex> Lock(m_Mutex); // We assume that this lock is acquired without much delay - we are the only user of the mutex
while (m_ShouldWait && (std::chrono::system_clock::now() < dst))
{
switch (m_CondVar.wait_until(Lock, dst))
{
case std::cv_status::no_timeout:
{
// The wait was successful, check for spurious wakeup:
if (!m_ShouldWait)
{
m_ShouldWait = true;
return true;
}
// This was a spurious wakeup, wait again:
continue;
}
case std::cv_status::timeout:
{
// The wait timed out, return failure:
return false;
}
} // switch (wait_until())
} // while (m_ShouldWait && not timeout)
// The wait timed out in the while() condition:
return false;
}
void cEvent::Set(void)
{
{
std::unique_lock<std::mutex> Lock(m_Mutex);
m_ShouldWait = false;
}
m_CondVar.notify_one();
}
|