-
Notifications
You must be signed in to change notification settings - Fork 1
/
dispatch_main.cc
123 lines (88 loc) · 3.46 KB
/
dispatch_main.cc
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
#include <oyoung/event.hpp>
#include <iostream>
struct RunLoop {
void on(const std::string& event, const std::function<void(const oyoung::any&)>& func) {
m_listeners[event].emplace_back(func);
}
void off(const std::string& event) {
m_listeners[event].clear();
}
void emit(const std::string&event, const oyoung::any& args) {
std::unique_lock<std::mutex> lock(m_task_mutex);
m_events.emplace(std::make_tuple(event, args));
m_task_not_empty.notify_all();
}
void stop(int user_code = 0) {
m_user_code = user_code;
m_running = false;
}
int exec() {
m_running = true;
while (m_running) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(m_task_mutex);
m_task_not_empty.wait(lock, [=] {
return !m_events.empty() || !m_running;
});
if(!m_events.empty()) {
auto event = m_events.front();
m_events.pop();
task = [=] {
for(const auto& func: m_listeners[std::get<0>(event)]) {
if(func) func(std::get<1>(event));
}
};
}
}
if(task) {
try {
task();
} catch (const std::exception& e) {
emit("exception", e.what());
}
}
}
return m_user_code;
}
private:
bool m_running{false};
int m_user_code{0};
std::map<std::string, std::vector<std::function<void(const oyoung::any&)>>> m_listeners;
std::mutex m_task_mutex;
std::condition_variable m_task_not_empty;
std::queue<std::tuple<std::string, oyoung::any>> m_events;
};
using MainQueue = oyoung::dispatch_main_queue<RunLoop>;
int main(int argc, char **argv)
{
RunLoop loop{};
auto queue = oyoung::dispatch_queue_create("name");
oyoung::events::emitter emitter;
MainQueue::set_dispatch_main_queue(std::make_shared<MainQueue >(loop));
oyoung::async(*queue, [=] {
std::cout << "dispatch async calling" << std::endl;
std::cout << "dispatch thread: " << std::this_thread::get_id() << std::endl;
std::cout << "dispatch sync will call" << std::endl;
auto n = oyoung::sync(oyoung::dispatch_get_main_queue(), [=] {
std::cout << "dispatch sync calling" << std::endl;
if(std::this_thread::is_main_thread()) {
std::cout << "dispatch thread(" << std::this_thread::get_id() <<") is main thread" << std::endl;
} else {
std::cout << "dispatch thread(" << std::this_thread::get_id() <<") is not main thread" << std::endl;
}
return std::this_thread::get_id();
});
std::cout << "dispatch sync called, result: " << n << std::endl;
std::cout << "dispatch main queue called" << std::endl;
oyoung::sync(oyoung::dispatch_get_main_queue(), [=] {
std::cout << "dispacth sync call no return" << std::endl;
});
if(std::this_thread::is_main_thread()) {
std::cout << "dispatch thread(" << std::this_thread::get_id() <<") is main thread" << std::endl;
} else {
std::cout << "dispatch thread(" << std::this_thread::get_id() <<") is not main thread" << std::endl;
}
});
return loop.exec();
}