summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/Event.cpp
blob: 4c2adea3cf4db1aadc448a289265ee478d439e70 (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

// Event.cpp

// Interfaces to the cEvent object representing a synchronization primitive that can be waited-for
// Implemented using C++11 condition variable and mutex

#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_ShouldContinue(false)
{
}





void cEvent::Wait(void)
{
	{
		std::unique_lock<std::mutex> Lock(m_Mutex);
		m_CondVar.wait(Lock, [this](){ return m_ShouldContinue.load(); });
	}
	m_ShouldContinue = false;
}





bool cEvent::Wait(unsigned a_TimeoutMSec)
{
	auto dst = std::chrono::system_clock::now() + std::chrono::milliseconds(a_TimeoutMSec);
	bool Result;
	{
		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
		Result = m_CondVar.wait_until(Lock, dst, [this](){ return m_ShouldContinue.load(); });
	}
	m_ShouldContinue = false;
	return Result;
}





void cEvent::Set(void)
{
	m_ShouldContinue = true;
	m_CondVar.notify_one();
}




void cEvent::SetAll(void)
{
	m_ShouldContinue = true;
	m_CondVar.notify_all();
}