-
Notifications
You must be signed in to change notification settings - Fork 43
/
windows_event_cancel.cpp
60 lines (50 loc) · 1.67 KB
/
windows_event_cancel.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
/**
* @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*[]) {
HANDLE e = CreateEventEx(nullptr, nullptr, //
CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
assert(e != NULL);
auto on_return_1 = gsl::finally([e]() { CloseHandle(e); });
ResetEvent(e);
set_or_cancel evt{e};
atomic_flag flag = ATOMIC_FLAG_INIT;
auto frame = wait_an_event(evt, flag);
auto on_return_2 = gsl::finally([&frame]() { frame.destroy(); });
// cancel by unregister
if (auto ec = evt.unregister()) {
cerr << system_category().message(ec) << endl;
return __LINE__;
}
// give time to windows threads
SleepEx(300, true);
return flag.test_and_set() == true ? EXIT_FAILURE : 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;
}