forked from luncliff/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows_on_apc_self.cpp
36 lines (28 loc) · 971 Bytes
/
windows_on_apc_self.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
/**
* @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 call_on_known_thread(HANDLE thread, HANDLE event) -> frame_t {
co_await continue_on_apc{thread};
if (SetEvent(event) == FALSE)
cerr << system_category().message(GetLastError()) << endl;
}
int main(int, char*[]) {
HANDLE event = CreateEvent(nullptr, false, false, nullptr);
assert(event != INVALID_HANDLE_VALUE);
auto on_return_1 = gsl::finally([event]() { CloseHandle(event); });
HANDLE worker = GetCurrentThread();
call_on_known_thread(worker, event);
auto ec = WaitForSingleObjectEx(event, INFINITE, true);
// expect the wait is cancelled by APC (WAIT_IO_COMPLETION)
assert(ec == WAIT_OBJECT_0 || ec == WAIT_IO_COMPLETION);
return EXIT_SUCCESS;
}