-
Notifications
You must be signed in to change notification settings - Fork 2
/
platform.cpp
215 lines (183 loc) · 5.16 KB
/
platform.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "platform.h"
#ifdef GIO_PLATFORM_LINUX
#include <csignal>
#include <atomic>
#include <cstdlib>
#include <unistd.h>
#include <sys/resource.h>
#include <cstdio>
#include <pthread.h>
#include <sys/syscall.h>
#include <iostream>
std::vector< int > signals = { SIGTERM, SIGHUP, SIGUSR1 };
sigset_t create_sigset() {
sigset_t sigset;
int res = sigemptyset(&sigset);
if (res) {
throw "Error when calling sigemptyset()";
}
for (const auto signal : signals) {
res = sigaddset(&sigset, signal);
if (res) {
throw "Error when calling sigaddset()";
}
}
return sigset;
}
bool platform_webmmpp_init(int argc, char *argv[]) {
(void) argc;
(void) argv;
/*struct sigaction act;
act.sa_handler = int_handler;
act.sa_flags = 0;
sigfillset(&act.sa_mask);
sigaction(SIGINT, &act, nullptr);
sigaction(SIGTERM, &act, nullptr);*/
/* Block the signals we want to receive synchronously;
* this should be done before any thread is created,
* so that all threads inherit the same blocking mask (I hope) */
int res;
sigset_t sigset = create_sigset();
res = sigprocmask(SIG_BLOCK, &sigset, nullptr);
if (res) {
throw "Error when calling sigprocmask()";
}
return true;
}
void platform_webmmpp_main_loop(const std::function<void()> &new_session_callback) {
sigset_t sigset = create_sigset();
bool running = true;
while (running) {
siginfo_t siginfo;
int res = sigwaitinfo(&sigset, &siginfo);
if (res < 0) {
continue;
}
switch (siginfo.si_signo) {
case SIGTERM:
std::cerr << "SIGTERM received!" << std::endl;
running = false;
break;
case SIGHUP:
std::cerr << "SIGHUP received!" << std::endl;
running = false;
break;
case SIGUSR1:
std::cerr << "SIGUSR1 received!" << std::endl;
new_session_callback();
break;
}
}
}
boost::filesystem::path platform_get_resources_base() {
return boost::filesystem::path(PROJ_DIR) / "resources";
}
#endif
#ifdef GIO_PLATFORM_MACOS
#include <csignal>
#include <atomic>
#include <cstdlib>
#include <unistd.h>
#include <sys/resource.h>
#include <cstdio>
#include <pthread.h>
#include <mach/mach.h>
#include <iostream>
std::vector< int > signals = { SIGTERM, SIGUSR1 };
sigset_t create_sigset() {
sigset_t sigset;
int res = sigemptyset(&sigset);
if (res) {
throw "Error when calling sigemptyset()";
}
for (const auto signal : signals) {
res = sigaddset(&sigset, signal);
if (res) {
throw "Error when calling sigaddset()";
}
}
return sigset;
}
bool platform_webmmpp_init(int argc, char *argv[]) {
(void) argc;
(void) argv;
/*struct sigaction act;
act.sa_handler = int_handler;
act.sa_flags = 0;
sigfillset(&act.sa_mask);
sigaction(SIGINT, &act, nullptr);
sigaction(SIGTERM, &act, nullptr);*/
/* Block the signals we want to receive synchronously;
* this should be done before any thread is created,
* so that all threads inherit the same blocking mask (I hope) */
int res;
sigset_t sigset = create_sigset();
res = sigprocmask(SIG_BLOCK, &sigset, nullptr);
if (res) {
throw "Error when calling sigprocmask()";
}
return true;
}
void platform_webmmpp_main_loop(const std::function<void()> &new_session_callback) {
sigset_t sigset = create_sigset();
bool running = true;
while (running) {
int signo;
int res = sigwait(&sigset, &signo);
if (res) {
throw "Error when calling sigwait()";
}
switch (signo) {
case SIGTERM:
std::cerr << "SIGTERM received!" << std::endl;
running = false;
break;
case SIGUSR1:
std::cerr << "SIGUSR1 received!" << std::endl;
new_session_callback();
break;
}
}
}
boost::filesystem::path platform_get_resources_base() {
return boost::filesystem::path(PROJ_DIR) / "resources";
}
#endif
#ifdef GIO_PLATFORM_WIN32
#include <future>
#include <iostream>
#include <windows.h>
#include <psapi.h>
std::promise< void > exit_promise;
std::future< void > exit_future;
bool promise_set = false;
BOOL ctrl_handler(DWORD type) {
if (type == CTRL_C_EVENT) {
if (!promise_set) {
std::cerr << "Ctrl-C received!" << std::endl;
exit_promise.set_value();
promise_set = true;
}
return true;
}
return false;
}
bool platform_webmmpp_init(int argc, char *argv[]) {
(void) argc;
(void) argv;
exit_future = exit_promise.get_future();
SetConsoleCtrlHandler((PHANDLER_ROUTINE) ctrl_handler, TRUE);
return true;
}
void platform_webmmpp_main_loop(const std::function<void ()> &new_session_callback) {
(void) new_session_callback;
assert(exit_future.valid());
exit_future.wait();
}
boost::filesystem::path platform_get_resources_base() {
return boost::filesystem::path(PROJ_DIR) / "resources";
}
#endif
#ifdef GIO_PLATFORM_UNKNOWN
#error Current platform is not supported.
#endif