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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
#include "event_loop.h"
#include "network.h"
#include "thread_extra.h"
#include <library/cpp/deprecated/atomic/atomic.h>
#include <util/generic/hash.h>
#include <util/network/pair.h>
#include <util/network/poller.h>
#include <util/system/event.h>
#include <util/system/mutex.h>
#include <util/system/thread.h>
#include <util/system/yassert.h>
#include <util/thread/lfqueue.h>
#include <errno.h>
using namespace NEventLoop;
namespace {
enum ERunningState {
EVENT_LOOP_CREATED,
EVENT_LOOP_RUNNING,
EVENT_LOOP_STOPPED,
};
enum EOperation {
OP_READ = 1,
OP_WRITE = 2,
OP_READ_WRITE = OP_READ | OP_WRITE,
};
}
class TChannel::TImpl {
public:
TImpl(TEventLoop::TImpl* eventLoop, TSocket socket, TEventHandlerPtr, void* cookie);
~TImpl();
void EnableRead();
void DisableRead();
void EnableWrite();
void DisableWrite();
void Unregister();
SOCKET GetSocket() const;
TSocket GetSocketPtr() const;
void Update(int pollerFlags, bool enable);
void CallHandler();
TEventLoop::TImpl* EventLoop;
TSocket Socket;
TEventHandlerPtr EventHandler;
void* Cookie;
TMutex Mutex;
int CurrentFlags;
bool Close;
};
class TEventLoop::TImpl {
public:
TImpl(const char* name);
void Run();
void Wakeup();
void Stop();
TChannelPtr Register(TSocket socket, TEventHandlerPtr eventHandler, void* cookie);
void Unregister(SOCKET socket);
typedef THashMap<SOCKET, TChannelPtr> TData;
void AddToPoller(SOCKET socket, void* cookie, int flags);
TMutex Mutex;
const char* Name;
TAtomic RunningState;
TAtomic StopSignal;
TSystemEvent StoppedEvent;
TData Data;
TLockFreeQueue<SOCKET> SocketsToRemove;
TSocketPoller Poller;
TSocketHolder WakeupReadSocket;
TSocketHolder WakeupWriteSocket;
};
TChannel::~TChannel() {
}
void TChannel::EnableRead() {
Impl->EnableRead();
}
void TChannel::DisableRead() {
Impl->DisableRead();
}
void TChannel::EnableWrite() {
Impl->EnableWrite();
}
void TChannel::DisableWrite() {
Impl->DisableWrite();
}
void TChannel::Unregister() {
Impl->Unregister();
}
SOCKET TChannel::GetSocket() const {
return Impl->GetSocket();
}
TSocket TChannel::GetSocketPtr() const {
return Impl->GetSocketPtr();
}
TChannel::TChannel(TImpl* impl)
: Impl(impl)
{
}
TEventLoop::TEventLoop(const char* name)
: Impl(new TImpl(name))
{
}
TEventLoop::~TEventLoop() {
}
void TEventLoop::Run() {
Impl->Run();
}
void TEventLoop::Stop() {
Impl->Stop();
}
bool TEventLoop::IsRunning() {
return AtomicGet(Impl->RunningState) == EVENT_LOOP_RUNNING;
}
TChannelPtr TEventLoop::Register(TSocket socket, TEventHandlerPtr eventHandler, void* cookie) {
return Impl->Register(socket, eventHandler, cookie);
}
TChannel::TImpl::TImpl(TEventLoop::TImpl* eventLoop, TSocket socket, TEventHandlerPtr eventHandler, void* cookie)
: EventLoop(eventLoop)
, Socket(socket)
, EventHandler(eventHandler)
, Cookie(cookie)
, CurrentFlags(0)
, Close(false)
{
}
TChannel::TImpl::~TImpl() {
Y_ASSERT(Close);
}
void TChannel::TImpl::EnableRead() {
Update(OP_READ, true);
}
void TChannel::TImpl::DisableRead() {
Update(OP_READ, false);
}
void TChannel::TImpl::EnableWrite() {
Update(OP_WRITE, true);
}
void TChannel::TImpl::DisableWrite() {
Update(OP_WRITE, false);
}
void TChannel::TImpl::Unregister() {
TGuard<TMutex> guard(Mutex);
if (Close) {
return;
}
Close = true;
if (CurrentFlags != 0) {
EventLoop->Poller.Unwait(Socket);
CurrentFlags = 0;
}
EventHandler.Drop();
EventLoop->SocketsToRemove.Enqueue(Socket);
EventLoop->Wakeup();
}
void TChannel::TImpl::Update(int flags, bool enable) {
TGuard<TMutex> guard(Mutex);
if (Close) {
return;
}
int newFlags = enable ? (CurrentFlags | flags) : (CurrentFlags & ~flags);
if (CurrentFlags == newFlags) {
return;
}
if (!newFlags) {
EventLoop->Poller.Unwait(Socket);
} else {
void* cookie = reinterpret_cast<void*>(this);
EventLoop->AddToPoller(Socket, cookie, newFlags);
}
CurrentFlags = newFlags;
}
SOCKET TChannel::TImpl::GetSocket() const {
return Socket;
}
TSocket TChannel::TImpl::GetSocketPtr() const {
return Socket;
}
void TChannel::TImpl::CallHandler() {
TEventHandlerPtr handler;
{
TGuard<TMutex> guard(Mutex);
// other thread may have re-added socket to epoll
// so even if CurrentFlags is 0, epoll may fire again
// so please use non-blocking operations
CurrentFlags = 0;
if (Close) {
return;
}
handler = EventHandler;
}
if (!!handler) {
handler->HandleEvent(Socket, Cookie);
}
}
TEventLoop::TImpl::TImpl(const char* name)
: Name(name)
, RunningState(EVENT_LOOP_CREATED)
, StopSignal(0)
{
SOCKET wakeupSockets[2];
if (SocketPair(wakeupSockets) < 0) {
Y_ABORT("failed to create socket pair for wakeup sockets: %s", LastSystemErrorText());
}
TSocketHolder wakeupReadSocket(wakeupSockets[0]);
TSocketHolder wakeupWriteSocket(wakeupSockets[1]);
WakeupReadSocket.Swap(wakeupReadSocket);
WakeupWriteSocket.Swap(wakeupWriteSocket);
SetNonBlock(WakeupWriteSocket, true);
SetNonBlock(WakeupReadSocket, true);
Poller.WaitRead(WakeupReadSocket,
reinterpret_cast<void*>(this));
}
void TEventLoop::TImpl::Run() {
bool res = AtomicCas(&RunningState, EVENT_LOOP_RUNNING, EVENT_LOOP_CREATED);
Y_ABORT_UNLESS(res, "Invalid mbus event loop state");
if (!!Name) {
SetCurrentThreadName(Name);
}
while (AtomicGet(StopSignal) == 0) {
void* cookies[1024];
const size_t count = Poller.WaitI(cookies, Y_ARRAY_SIZE(cookies));
void** end = cookies + count;
for (void** c = cookies; c != end; ++c) {
TChannel::TImpl* s = reinterpret_cast<TChannel::TImpl*>(*c);
if (*c == this) {
char buf[0x1000];
if (NBus::NPrivate::SocketRecv(WakeupReadSocket, buf) < 0) {
Y_ABORT("failed to recv from wakeup socket: %s", LastSystemErrorText());
}
continue;
}
s->CallHandler();
}
SOCKET socket = -1;
while (SocketsToRemove.Dequeue(&socket)) {
TGuard<TMutex> guard(Mutex);
Y_ABORT_UNLESS(Data.erase(socket) == 1, "must be removed once");
}
}
{
TGuard<TMutex> guard(Mutex);
for (auto& it : Data) {
it.second->Unregister();
}
// release file descriptors
Data.clear();
}
res = AtomicCas(&RunningState, EVENT_LOOP_STOPPED, EVENT_LOOP_RUNNING);
Y_ABORT_UNLESS(res);
StoppedEvent.Signal();
}
void TEventLoop::TImpl::Stop() {
AtomicSet(StopSignal, 1);
if (AtomicGet(RunningState) == EVENT_LOOP_RUNNING) {
Wakeup();
StoppedEvent.WaitI();
}
}
TChannelPtr TEventLoop::TImpl::Register(TSocket socket, TEventHandlerPtr eventHandler, void* cookie) {
Y_ABORT_UNLESS(socket != INVALID_SOCKET, "must be a valid socket");
TChannelPtr channel = new TChannel(new TChannel::TImpl(this, socket, eventHandler, cookie));
TGuard<TMutex> guard(Mutex);
Y_ABORT_UNLESS(Data.insert(std::make_pair(socket, channel)).second, "must not be already inserted");
return channel;
}
void TEventLoop::TImpl::Wakeup() {
if (NBus::NPrivate::SocketSend(WakeupWriteSocket, TArrayRef<const char>("", 1)) < 0) {
if (LastSystemError() != EAGAIN) {
Y_ABORT("failed to send to wakeup socket: %s", LastSystemErrorText());
}
}
}
void TEventLoop::TImpl::AddToPoller(SOCKET socket, void* cookie, int flags) {
if (flags == OP_READ) {
Poller.WaitReadOneShot(socket, cookie);
} else if (flags == OP_WRITE) {
Poller.WaitWriteOneShot(socket, cookie);
} else if (flags == OP_READ_WRITE) {
Poller.WaitReadWriteOneShot(socket, cookie);
} else {
Y_ABORT("Wrong flags: %d", int(flags));
}
}
|