-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.cpp
51 lines (40 loc) · 1.44 KB
/
main.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
#include <ichor/event_queues/PriorityQueue.h>
#include <ichor/DependencyManager.h>
#include <ichor/services/timer/TimerFactoryFactory.h>
#include <csignal>
using namespace Ichor;
std::atomic<bool> quit{};
void siginthandler(int) {
quit = true;
}
class SigIntService final {
public:
SigIntService(ITimerFactory *factory) {
// Setup a timer that fires every 100 milliseconds
auto &timer = factory->createTimer();
timer.setChronoInterval(100ms);
timer.setCallback([]() {
// If sigint has been fired, send a quit to the event loop.
// This can't be done from within the siginthandler itself, as the mutex surrounding pushEvent might already be locked, resulting in a deadlock!
if(quit) {
GetThreadLocalEventQueue().pushEvent<QuitEvent>(0);
}
});
timer.startTimer();
// Register sigint handler
auto r = ::signal(SIGINT, siginthandler);
if(r == SIG_ERR) {
std::terminate();
}
}
};
int main(int argc, char *argv[]) {
std::locale::global(std::locale("en_US.UTF-8")); // some loggers require having a locale
auto queue = std::make_unique<PriorityQueue>();
auto &dm = queue->createManager();
dm.createServiceManager<SigIntService>();
dm.createServiceManager<TimerFactoryFactory>();
// Start manager, consumes current thread.
queue->start(DoNotCaptureSigInt);
return 0;
}