forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauto_reset_event.cpp
More file actions
97 lines (84 loc) · 1.83 KB
/
Copy pathauto_reset_event.cpp
File metadata and controls
97 lines (84 loc) · 1.83 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include "auto_reset_event.hpp"
#if CPPCORO_OS_WINNT
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <system_error>
#endif
namespace cppcoro
{
#if CPPCORO_OS_WINNT
auto_reset_event::auto_reset_event(bool initiallySet)
: m_event(::CreateEventW(NULL, FALSE, initiallySet ? TRUE : FALSE, NULL))
{
if (m_event.handle() == NULL)
{
DWORD errorCode = ::GetLastError();
throw std::system_error
{
static_cast<int>(errorCode),
std::system_category(),
"auto_reset_event: CreateEvent failed"
};
}
}
auto_reset_event::~auto_reset_event()
{
}
void auto_reset_event::set()
{
BOOL ok =::SetEvent(m_event.handle());
if (!ok)
{
DWORD errorCode = ::GetLastError();
throw std::system_error
{
static_cast<int>(errorCode),
std::system_category(),
"auto_reset_event: SetEvent failed"
};
}
}
void auto_reset_event::wait()
{
DWORD result = ::WaitForSingleObjectEx(m_event.handle(), INFINITE, FALSE);
if (result != WAIT_OBJECT_0)
{
DWORD errorCode = ::GetLastError();
throw std::system_error
{
static_cast<int>(errorCode),
std::system_category(),
"auto_reset_event: WaitForSingleObjectEx failed"
};
}
}
#else
auto_reset_event::auto_reset_event(bool initiallySet)
: m_isSet(initiallySet)
{}
auto_reset_event::~auto_reset_event()
{}
void auto_reset_event::set()
{
std::unique_lock lock{ m_mutex };
if (!m_isSet)
{
m_isSet = true;
m_cv.notify_one();
}
}
void auto_reset_event::wait()
{
std::unique_lock lock{ m_mutex };
while (!m_isSet)
{
m_cv.wait(lock);
}
m_isSet = false;
}
#endif
}