-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunloop.hpp
More file actions
47 lines (32 loc) · 709 Bytes
/
runloop.hpp
File metadata and controls
47 lines (32 loc) · 709 Bytes
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
#pragma once
#include <thread_local.hpp>
#include <atomic>
#include <functional>
#include <iostream>
#include <queue>
class Runloop {
public:
using WorkTask = std::function<void()>;
static Runloop *Get() {
return current().get();
}
Runloop();
~Runloop();
void run();
void wake();
void stop();
void invoke(WorkTask &&fn);
bool running() {
return _running;
}
private:
static ThreadLocal<Runloop> ¤t() {
static ThreadLocal<Runloop> runloop;
return runloop;
}
private:
class Impl;
std::unique_ptr<Impl> _impl;
std::queue<std::shared_ptr<WorkTask>> _queue;
std::atomic_bool _running{false};
};