-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActor.cpp
More file actions
144 lines (119 loc) · 3.14 KB
/
Copy pathActor.cpp
File metadata and controls
144 lines (119 loc) · 3.14 KB
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
#include "Actor.h"
#include <thread>
#include <mutex>
#include <condition_variable>
#include "EventSource.h"
namespace {
struct GlibUnref {
void operator() (GMainContext* context)
{ g_main_context_unref(context); }
void operator() (GMainLoop* loop)
{ g_main_loop_unref(loop); }
void operator() (GAsyncQueue* queue)
{ g_async_queue_unref(queue); }
};
typedef
std::unique_ptr<
GMainContext,
GlibUnref> GMainContextPtr;
typedef
std::unique_ptr<
GMainLoop,
GlibUnref> GMainLoopPtr;
typedef
std::unique_ptr<
GAsyncQueue,
GlibUnref> GAsyncQueuePtr;
struct Action {
Actor::Action action;
};
void OnEvent(GAsyncQueue* queue) noexcept
{
while(gpointer item = g_async_queue_try_pop(queue)) {
std::unique_ptr<Action>(static_cast<Action*>(item))->action();
}
}
void ActorMain(
GMainContext* mainContext,
GMainLoop* mainLoop,
GAsyncQueue* queue,
EventSource* notifier,
const std::shared_ptr<Actor::Context>& context) noexcept
{
g_main_context_push_thread_default(mainContext);
if(context)
context->activate();
notifier->subscribe(std::bind(&OnEvent, queue));
g_main_loop_run(mainLoop);
if(context)
context->deactivate();
}
}
struct Actor::Private {
Private(const std::shared_ptr<Context>&) noexcept;
GMainContextPtr mainContextPtr;
GMainLoopPtr mainLoopPtr;
GAsyncQueuePtr queuePtr;
EventSource notifier;
std::thread actorThread;
};
Actor::Private::Private(const std::shared_ptr<Context>& context) noexcept :
mainContextPtr(g_main_context_new()),
mainLoopPtr(g_main_loop_new(mainContextPtr.get(), FALSE)),
queuePtr(g_async_queue_new()),
notifier(mainContextPtr.get()),
actorThread(
ActorMain,
mainContextPtr.get(),
mainLoopPtr.get(),
queuePtr.get(),
¬ifier,
context)
{
}
Actor::Actor(const std::shared_ptr<Context>& context) noexcept :
_p(std::make_unique<Private>(context))
{
}
Actor::~Actor() noexcept
{
if(_p->actorThread.joinable()) {
postAction([loop = _p->mainLoopPtr.get()] () {
g_main_loop_quit(loop);
});
_p->actorThread.join();
}
}
void Actor::postAction(const Action& action) noexcept
{
g_async_queue_push(
_p->queuePtr.get(),
new ::Action { action });
_p->notifier.postEvent();
}
void Actor::postAction(Action&& action) noexcept
{
g_async_queue_push(
_p->queuePtr.get(),
new ::Action { std::move(action) });
_p->notifier.postEvent();
}
void Actor::sendAction(const Action& action) noexcept
{
std::mutex guard;
std::condition_variable conditional;
bool handled = false;
std::unique_lock guardLock(guard);
g_async_queue_push(
_p->queuePtr.get(),
new ::Action {
[&guard, &conditional, &action, &handled] () {
std::unique_lock guardLock(guard);
action();
handled = true;
conditional.notify_one();
}
});
_p->notifier.postEvent();
conditional.wait(guardLock, [&handled] () { return handled; });
}