forked from wermipls/octomino-sdl-input
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrundown_protection.cpp
75 lines (63 loc) · 1.43 KB
/
rundown_protection.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
67
68
69
70
71
72
73
74
75
#include "rundown_protection.h"
#include <condition_variable>
#include <future>
#include <mutex>
// FYI this rundown protection is super barebones and just uses mutex instead of being lockfree
// but for this application, it will be perfectly sufficient.
static bool srp_active = false;
static bool srp_has_user = false;
static std::mutex srp_mutex;
static std::condition_variable srp_cv;
DWORD srp_main_thread_id = 0;
void rp_activate()
{
std::lock_guard<std::mutex> lck(srp_mutex);
srp_active = true;
}
static void rp_deactivate_wait_impl()
{
std::unique_lock<std::mutex> lck(srp_mutex);
srp_active = false;
srp_cv.wait(lck, [] { return !srp_has_user; });
}
void rp_deactivate_wait()
{
if (!srp_active)
return;
bool main = GetCurrentThreadId() == srp_main_thread_id;
if (main)
{
bool running = true;
auto future = std::async(std::launch::async, [&] {
rp_deactivate_wait_impl();
running = false;
PostThreadMessage(srp_main_thread_id, WM_APP + 1, 0, 0);
});
MSG msg;
while (running && GetMessage(&msg, 0, 0, 0))
{
if (msg.message == WM_APP + 1 && !running)
break;
DispatchMessage(&msg);
}
}
else
{
rp_deactivate_wait_impl();
}
}
bool rp_protect()
{
std::lock_guard<std::mutex> lck(srp_mutex);
if (!srp_active)
return false;
srp_has_user = true;
return true;
}
void rp_unprotect()
{
std::lock_guard<std::mutex> lck(srp_mutex);
srp_has_user = false;
if (!srp_active)
srp_cv.notify_all();
}