-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_worker.kqueue.cpp
More file actions
296 lines (259 loc) · 8.9 KB
/
Copy pathevent_worker.kqueue.cpp
File metadata and controls
296 lines (259 loc) · 8.9 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Any copyright is dedicated to the Public Domain.
* https://creativecommons.org/publicdomain/zero/1.0/ */
#include "event_worker.hpp"
#include "event_channel_base.hpp"
#include "logger.hpp"
#include "serv_exception.hpp"
#include "serv_types.hpp"
#include <smart_ptr/shared_ptr.hpp>
#include <thread/condition_variable.hpp>
#include <thread/mutex.hpp>
#include <thread/thread.hpp>
#include <sys/event.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <exception>
#include <map>
#include <string>
#include <vector>
namespace ft
{
namespace serv
{
typedef dynamic_array<struct ::kevent>::type event_list;
static const event_list::size_type MAX_EVENTS = FT_SERV_MAX_EVENT_SIZE;
enum
{
USER_EVENT_WAKEUP,
USER_EVENT_SHUTDOWN,
NUMBER_OF_USER_EVENT
};
static void _kqueue_operation(event_list& changes, int flags_add, int flags_del, event_channel_base& channel) throw()
{
const bool force = flags_add == 0 || flags_del == 0;
const ident_t ident = channel.get_ident();
struct ::kevent change[2];
event_list::size_type count = 0;
bool interested[2];
bool changed[2];
channel.load_interested(interested, changed);
if (changed[0] || force)
{
EV_SET(&change[count++], ident, EVFILT_READ, interested[0] ? flags_add : flags_del, 0, 0, null);
}
if (changed[1] || force)
{
EV_SET(&change[count++], ident, EVFILT_WRITE, interested[1] ? flags_add : flags_del, 0, 0, null);
}
changes.insert(changes.end(), beginof(change), &change[count]);
channel.store_interested();
}
static void _trigger_user_event(ident_t loop_ident, ident_t target_ident) throw()
{
struct ::kevent change[1];
EV_SET(&change[0], target_ident, EVFILT_USER, 0, NOTE_TRIGGER | NOTE_FFNOP, 0, null);
if (::kevent(loop_ident, beginof(change), countof(change), null, 0, null) < 0)
{
const syscall_failed e;
if (e.error() != EINTR)
{
// ignore errors
}
}
}
}
}
ft::serv::event_worker::event_worker()
: lock(),
cond(),
active(),
loop_ident(::kqueue()),
event_ident(0),
loop_list(),
channels(),
tasks(),
task_closed(),
working_thread()
{
if (this->loop_ident < 0)
{
throw syscall_failed();
}
struct ::kevent change[NUMBER_OF_USER_EVENT];
for (std::size_t i = 0; i < countof(change); i++)
{
EV_SET(&change[i], this->event_ident + i, EVFILT_USER, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_FFNOP, 0, null);
}
if (::kevent(this->loop_ident, beginof(change), countof(change), null, 0, null) < 0)
{
const syscall_failed e;
::close(this->loop_ident);
throw e;
}
logger::trace("Create Event Worker (%d)", this->loop_ident);
}
ft::serv::event_worker::~event_worker()
{
logger::trace("Destroy Event Worker (%d)", this->loop_ident);
::close(this->loop_ident);
}
void ft::serv::event_worker::add_channel(const ft::shared_ptr<event_channel_base>& channel)
{
assert(this->is_in_event_loop());
const ident_t ident = channel->get_ident();
const bool success = this->channels.insert(std::make_pair(ident, channel)).second;
assert(success); // NOTE: duplicate identity
logger::trace("Event Worker (%d): Add Event Channel (%d)", this->loop_ident, ident);
_kqueue_operation(*static_cast<event_list*>(this->loop_list), EV_ADD | EV_ENABLE | EV_CLEAR, 0, *channel);
}
void ft::serv::event_worker::remove_channel(const ident_t ident)
{
assert(this->is_in_event_loop());
channel_dictionary::/*const_*/ iterator it = this->channels.find(ident);
if (it != this->channels.end())
{
const ft::shared_ptr<event_channel_base>& channel = it->second;
_kqueue_operation(*static_cast<event_list*>(this->loop_list), 0, EV_DELETE | EV_DISABLE, *channel);
logger::trace("Event Worker (%d): Remove Event Channel (%d): Success", this->loop_ident, ident);
this->channels.erase(it);
}
else
{
// ignore
logger::trace("Event Worker (%d): Remove Event Channel (%d): Not Found", this->loop_ident, ident);
}
}
void ft::serv::event_worker::watch_ability(event_channel_base& channel)
{
assert(this->is_in_event_loop());
const ident_t ident = channel.get_ident();
_kqueue_operation(*static_cast<event_list*>(this->loop_list), EV_ENABLE, EV_DISABLE, channel);
logger::trace("Event Worker (%d): Watch Ability Changed (%d): R=%d, W=%d", this->loop_ident, ident, channel.is_readability_enabled(), channel.is_writability_enabled());
this->wake_up();
}
void ft::serv::event_worker::loop()
{
this->working_thread = ft::thread::self();
synchronized (this->lock)
{
this->active = true;
}
this->cond.notify_all();
event_list events;
event_list changes;
events.resize(MAX_EVENTS);
changes.reserve(MAX_EVENTS);
this->loop_list = &changes;
for (;;)
{
::time_t timeout_sec;
synchronized (this->lock)
{
// if has task then polling
timeout_sec = this->tasks.empty() ? 1 : 0;
}
const struct ::timespec timeout = {timeout_sec, 0};
const int n = ::kevent(this->loop_ident, changes.data(), changes.size(), events.data(), events.size(), &timeout);
if (n < 0)
{
const syscall_failed e;
if (e.error() == EINTR)
{
// When kevent() call fails with EINTR error, all changes in the changelist have been applied.
// https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
changes.clear();
}
else
{
logger::warn("Event Worker (%d): Error on kevent: %s", this->loop_ident, e.what());
::sleep(1);
}
continue;
}
changes.clear();
if (n != 0)
{
this->process_events(&events, n);
}
if (!this->execute_tasks())
{
break;
}
if (static_cast<event_list::size_type>(n) == events.size())
{
// double
events.resize(n << 1);
}
}
}
void ft::serv::event_worker::wake_up() throw()
{
if (!this->is_in_event_loop())
{
_trigger_user_event(this->loop_ident, this->event_ident + USER_EVENT_WAKEUP);
}
}
void ft::serv::event_worker::shutdown_loop() throw()
{
_trigger_user_event(this->loop_ident, this->event_ident + USER_EVENT_SHUTDOWN);
}
void ft::serv::event_worker::process_events(void* list, int n) throw()
{
event_list& events = *static_cast<event_list*>(list);
for (int i = 0; i < n; i++)
{
const struct ::kevent& evi = events[i];
// evi.udata: event_invoker
if (evi.filter == EVFILT_USER)
{
logger::trace("Event Worker (%d): Kqueue Wakeup Event", this->loop_ident);
if (static_cast<ident_t>(evi.ident) == this->event_ident + USER_EVENT_SHUTDOWN)
{
synchronized (this->lock)
{
this->active = false;
}
break;
}
continue;
}
if (evi.flags & EV_ERROR)
{
const syscall_failed e(evi.data);
if (e.error() == ENOENT)
{
// double delete (delete after auto-delete on close):
// The event could not be found to be modified or deleted. (https://man.freebsd.org/cgi/man.cgi?kqueue)
// ignore
}
else
{
logger::trace("Event Worker (%d): Error (%d): %s", this->loop_ident, evi.ident, e.what());
}
continue;
}
const ident_t ident = evi.ident;
const channel_dictionary::const_iterator it_channel = this->channels.find(ident);
if (it_channel == this->channels.end())
{
logger::warn("Event Worker (%d): Channel Not Found (%d)", this->loop_ident, ident);
continue;
}
ft::shared_ptr<event_channel_base> channel = it_channel->second; // lock ref count
if (evi.filter == EVFILT_WRITE)
{
logger::trace("Event Worker (%d): Kqueue Write Event (%d): %d, %x", this->loop_ident, ident, evi.data, evi.flags);
channel->trigger_write();
}
else if (evi.filter == EVFILT_READ)
{
// can use `evi.data`, `evi.flags & EV_EOF`
logger::trace("Event Worker (%d): Kqueue Read Event (%d): %d, %x", this->loop_ident, ident, evi.data, evi.flags);
channel->trigger_read();
}
}
}