forked from luncliff/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows_event_wait_multiple.cpp
66 lines (57 loc) · 1.81 KB
/
windows_event_wait_multiple.cpp
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
/**
* @author github.com/luncliff (luncliff@gmail.com)
*/
#undef NDEBUG
#include <atomic>
#include <cassert>
#include <iostream>
#include <gsl/gsl>
#include <coroutine/return.h>
#include <coroutine/windows.h>
using namespace std;
using namespace coro;
auto wait_an_event(set_or_cancel& token, atomic_flag& flag) -> frame_t;
auto set_after_sleep(HANDLE ev, uint32_t ms) -> frame_t;
int main(int, char*[]) {
array<HANDLE, 10> events{};
for (auto& e : events) {
e = CreateEventEx(nullptr, nullptr, //
CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
assert(e != NULL);
ResetEvent(e);
}
auto on_return = gsl::finally([&events]() {
for (auto e : events)
CloseHandle(e);
});
for (auto e : events) {
auto ms = rand() & 0b1111; // at most 16 ms
set_after_sleep(e, ms);
}
SleepEx(3, true);
// issue:
// CI environment runs slowly, so too short timeout might fail ...
// wait for 300 ms
constexpr bool wait_all = true;
auto ec = WaitForMultipleObjectsEx((DWORD)events.max_size(), events.data(),
wait_all, 300, true);
assert(ec == WAIT_OBJECT_0);
return EXIT_SUCCESS;
}
auto wait_an_event(set_or_cancel& evt, atomic_flag& flag) -> frame_t {
// wait for set or cancel
// `co_await` will forward `GetLastError` if canceled.
if (DWORD ec = co_await evt) {
cerr << system_category().message(ec) << endl;
co_return;
}
flag.test_and_set();
}
auto set_after_sleep(HANDLE ev, uint32_t ms) -> frame_t {
// move to background thread ...
co_await continue_on_thread_pool{};
SleepEx(ms, true);
// if failed, print error message
if (SetEvent(ev) == FALSE)
cerr << system_category().message(GetLastError()) << endl;
}