forked from ambrop72/aipstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoop.cpp
More file actions
627 lines (502 loc) · 16.9 KB
/
Copy pathEventLoop.cpp
File metadata and controls
627 lines (502 loc) · 16.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* Copyright (c) 2018 Ambroz Bizjak
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cstdint>
#include <mutex>
#include <aipstack/misc/Assert.h>
#include <aipstack/misc/OneOf.h>
#include <aipstack/misc/MinMax.h>
#include <aipstack/misc/Hints.h>
#include <aipstack/misc/Use.h>
#include <aipstack/structure/Accessor.h>
#include <aipstack/event_loop/EventLoop.h>
#if AIPSTACK_EVENT_LOOP_HAS_IOCP
#include <cstdio>
#include <utility>
#include <memory>
#include <stdexcept>
#endif
namespace AIpStack {
struct EventLoopPriv::TimerHeapNodeAccessor :
public MemberAccessor<EventLoopTimer, TimerHeapNode, &EventLoopTimer::m_heap_node> {};
struct EventLoopPriv::TimerCompare {
AIPSTACK_USE_TYPES(TimerLinkModel, (State, Ref))
AIPSTACK_USE_TYPES(EventLoop, (TimerState))
inline static int compareEntries (State, Ref ref1, Ref ref2)
{
EventLoopTimer &tim1 = *ref1;
EventLoopTimer &tim2 = *ref2;
if (tim1.m_state != tim2.m_state) {
return (tim1.m_state < tim2.m_state) ? -1 : 1;
}
if (tim1.m_time != tim2.m_time) {
return (tim1.m_time < tim2.m_time) ? -1 : 1;
}
return 0;
}
inline static int compareKeyEntry (State, EventLoopTime time1, Ref ref2)
{
TimerState state1 = TimerState::Pending;
EventLoopTimer &tim2 = *ref2;
if (state1 != tim2.m_state) {
return (state1 < tim2.m_state) ? -1 : 1;
}
if (time1 != tim2.m_time) {
return (time1 < tim2.m_time) ? -1 : 1;
}
return 0;
}
};
struct EventLoopPriv::AsyncSignalNodeAccessor : public MemberAccessor<
AsyncSignalNode, AsyncSignalListNode, &AsyncSignalNode::m_list_node> {};
EventLoopMembers::EventLoopMembers() :
m_stop(false),
m_recheck_async_signals(false),
m_event_time(EventLoop::getTime()),
m_num_timers(0),
m_num_async_signals(0)
#if AIPSTACK_EVENT_LOOP_HAS_FD
,m_num_fd_notifiers(0)
#endif
#if AIPSTACK_EVENT_LOOP_HAS_IOCP
,m_num_iocp_notifiers(0)
,m_num_iocp_resources(0)
#endif
{
EventLoop::AsyncSignalList::initLonely(m_pending_async_list);
EventLoop::AsyncSignalList::initLonely(m_dispatch_async_list);
}
EventLoop::EventLoop () :
EventLoopMembers(),
EventProvider()
{}
EventLoop::~EventLoop ()
{
AIPSTACK_ASSERT(m_num_timers == 0);
AIPSTACK_ASSERT(m_timer_heap.isEmpty());
AIPSTACK_ASSERT(m_num_async_signals == 0);
AIPSTACK_ASSERT(AsyncSignalList::isLonely(m_pending_async_list));
AIPSTACK_ASSERT(AsyncSignalList::isLonely(m_dispatch_async_list));
#if AIPSTACK_EVENT_LOOP_HAS_FD
AIPSTACK_ASSERT(m_num_fd_notifiers == 0);
#endif
#if AIPSTACK_EVENT_LOOP_HAS_IOCP
AIPSTACK_ASSERT(m_num_iocp_notifiers == 0);
#endif
#if AIPSTACK_EVENT_LOOP_HAS_IOCP
try {
wait_for_final_iocp_results();
} catch (std::runtime_error const &ex) {
// Should not happen. Here we leak IocpResource's including user_resource's.
std::fprintf(stderr, "EventLoop: exception in wait_for_final_iocp_results "
"(memory leaked): %s\n", ex.what());
}
#endif
}
void EventLoop::stop ()
{
m_stop = true;
}
void EventLoop::run ()
{
if (m_stop) {
return;
}
while (true) {
m_event_time = getTime();
prepare_timers_for_dispatch(m_event_time);
if (!dispatch_timers()) {
return;
}
if (AIPSTACK_UNLIKELY(m_recheck_async_signals)) {
if (!dispatch_async_signals()) {
return;
}
}
if (!EventProvider::dispatchEvents()) {
return;
}
EventLoopTime wait_time = get_timers_wait_time();
EventProvider::waitForEvents(wait_time);
}
}
void EventLoop::prepare_timers_for_dispatch (EventLoopTime now)
{
bool changed = false;
// Find all Pending timers which are expired with respect to 'now' and change their
// state to Dispatch.
m_timer_heap.findAllLesserOrEqual(now, [&](EventLoopTimer *tim) {
AIPSTACK_ASSERT(tim->m_state == OneOfHeapTimerStates);
if (tim->m_state == TimerState::Pending) {
tim->m_state = TimerState::Dispatch;
changed = true;
}
});
// It is important to understand that the changes performed (taken together) must not
// break the structure of the heap. Specifically, given any two timers, their relative
// order must remain the same or they become equal. This is satisfied because all
// Dispatch state timers compare equal among themselves and less than any Pending
// timer.
if (changed) {
m_timer_heap.assertValidHeap();
}
}
bool EventLoop::dispatch_timers ()
{
while (EventLoopTimer *tim = m_timer_heap.first()) {
AIPSTACK_ASSERT(tim->m_state == OneOfHeapTimerStates);
if (tim->m_state != TimerState::Dispatch) {
break;
}
m_timer_heap.remove(*tim);
tim->m_state = TimerState::Idle;
tim->m_handler();
if (AIPSTACK_UNLIKELY(m_stop)) {
return false;
}
}
return true;
}
EventLoopTime EventLoop::get_timers_wait_time () const
{
EventLoopTimer *tim = m_timer_heap.first();
if (tim == nullptr) {
return EventLoopTime::max();
}
AIPSTACK_ASSERT(tim->m_state == TimerState::Pending);
return tim->m_time;
}
bool EventLoop::dispatch_async_signals ()
{
// This flag is used to prevent the possibility of forgetting to dispatch a pending
// signal in case an exception occurs during dispatching below and run() is called
// again afterward. If this occurs, any next run() would see this flag to be true and
// call this function before waitForEvents.
m_recheck_async_signals = true;
{
std::unique_lock<std::mutex> lock(m_async_signal_mutex);
// Move any signals in the pending list to the end of the dispatch list.
if (!AsyncSignalList::isLonely(m_pending_async_list)) {
AsyncSignalList::moveOtherNodesBefore(
m_pending_async_list, m_dispatch_async_list);
}
// Dispatch signals in the dispatch list.
while (true) {
// Get the next signal, if any (note the list is circular).
AsyncSignalNode *node = AsyncSignalList::next(m_dispatch_async_list);
if (node == &m_dispatch_async_list) {
break;
}
EventLoopAsyncSignal &asig = *static_cast<EventLoopAsyncSignal *>(node);
AIPSTACK_ASSERT(&asig.m_loop == this);
AIPSTACK_ASSERT(!AsyncSignalList::isRemoved(asig));
// Remove the signal from the list.
AsyncSignalList::remove(asig);
AsyncSignalList::markRemoved(asig);
// Unlock the mutex while calling the handler.
lock.unlock();
asig.m_handler();
if (AIPSTACK_UNLIKELY(m_stop)) {
return false;
}
// Lock mutex again before looking at the dispatch list.
lock.lock();
}
}
// No exception occurred, clear this flag.
m_recheck_async_signals = false;
return true;
}
bool EventProviderBase::dispatchAsyncSignals ()
{
auto &event_loop = static_cast<EventLoop &>(*this);
return event_loop.dispatch_async_signals();
}
#if AIPSTACK_EVENT_LOOP_HAS_IOCP
bool EventProviderBase::handleIocpResult (void *completion_key, OVERLAPPED *overlapped)
{
auto &event_loop = static_cast<EventLoop &>(*this);
return event_loop.handle_iocp_result(completion_key, overlapped);
}
#endif
EventLoopTimer::EventLoopTimer (EventLoop &loop, TimerHandler handler) :
m_loop(loop),
m_handler(handler),
m_time(EventLoopTime()),
m_state(TimerState::Idle)
{
m_loop.m_num_timers++;
}
EventLoopTimer::~EventLoopTimer ()
{
if (m_state != TimerState::Idle) {
m_loop.m_timer_heap.remove(*this);
}
AIPSTACK_ASSERT(m_loop.m_num_timers > 0);
m_loop.m_num_timers--;
}
void EventLoopTimer::unset ()
{
if (m_state != TimerState::Idle) {
m_loop.m_timer_heap.remove(*this);
m_state = TimerState::Idle;
}
}
void EventLoopTimer::setAt (EventLoopTime time)
{
m_time = time;
TimerState old_state = m_state;
m_state = TimerState::Pending;
if (old_state == TimerState::Idle) {
m_loop.m_timer_heap.insert(*this);
} else {
m_loop.m_timer_heap.fixup(*this);
}
}
void EventLoopTimer::setAfter (EventLoopDuration duration)
{
return setAt(m_loop.getEventTime() + duration);
}
#if AIPSTACK_EVENT_LOOP_HAS_FD
EventLoopFdWatcher::EventLoopFdWatcher (EventLoop &loop, FdEventHandler handler) :
EventLoopFdWatcherMembers{
/*m_loop=*/loop,
/*m_handler=*/handler,
/*m_watched_fd=*/-1,
/*m_events=*/EventLoopFdEvents()
},
EventProviderFd()
{
m_loop.m_num_fd_notifiers++;
}
EventLoopFdWatcher::~EventLoopFdWatcher ()
{
if (m_watched_fd >= 0) {
EventProviderFd::resetImpl();
}
AIPSTACK_ASSERT(m_loop.m_num_fd_notifiers > 0);
m_loop.m_num_fd_notifiers--;
}
void EventLoopFdWatcher::initFd (int fd, EventLoopFdEvents events)
{
AIPSTACK_ASSERT(m_watched_fd == -1);
AIPSTACK_ASSERT(fd >= 0);
AIPSTACK_ASSERT((events & ~EventLoopFdEvents::All) == Enum0);
EventProviderFd::initFdImpl(fd, events);
// Update these after initFdImpl so they remain unchanged in case of exception.
m_watched_fd = fd;
m_events = events;
}
void EventLoopFdWatcher::updateEvents (EventLoopFdEvents events)
{
AIPSTACK_ASSERT(m_watched_fd >= 0);
AIPSTACK_ASSERT((events & ~EventLoopFdEvents::All) == Enum0);
EventProviderFd::updateEventsImpl(events);
// Update these after updateEventsImpl so they remain unchanged in case of exception.
m_events = events;
}
void EventLoopFdWatcher::reset ()
{
if (m_watched_fd >= 0) {
EventProviderFd::resetImpl();
m_watched_fd = -1;
m_events = EventLoopFdEvents();
}
}
EventProviderBase & EventProviderFdBase::getProvider () const
{
auto &fd_watcher = static_cast<EventLoopFdWatcher const &>(*this);
return fd_watcher.m_loop;
}
void EventProviderFdBase::sanityCheck () const
{
auto &fd_watcher = static_cast<EventLoopFdWatcher const &>(*this);
AIPSTACK_ASSERT(fd_watcher.m_watched_fd >= 0);
AIPSTACK_ASSERT((fd_watcher.m_events & ~EventLoopFdEvents::All) == Enum0);
}
int EventProviderFdBase::getFd () const
{
auto &fd_watcher = static_cast<EventLoopFdWatcher const &>(*this);
return fd_watcher.m_watched_fd;
}
EventLoopFdEvents EventProviderFdBase::getFdEvents () const
{
auto &fd_watcher = static_cast<EventLoopFdWatcher const &>(*this);
return fd_watcher.m_events;
}
bool EventProviderFdBase::callFdEventHandler (EventLoopFdEvents events)
{
auto &fd_watcher = static_cast<EventLoopFdWatcher &>(*this);
fd_watcher.m_handler(events);
if (AIPSTACK_UNLIKELY(fd_watcher.m_loop.m_stop)) {
return false;
}
return true;
}
#endif
#if AIPSTACK_EVENT_LOOP_HAS_IOCP
EventLoopIocpNotifier::EventLoopIocpNotifier (EventLoop &loop, IocpEventHandler handler) :
m_loop(loop),
m_handler(handler),
m_iocp_resource(nullptr),
m_busy(false)
{
m_loop.m_num_iocp_notifiers++;
}
EventLoopIocpNotifier::~EventLoopIocpNotifier ()
{
reset();
AIPSTACK_ASSERT(m_loop.m_num_iocp_notifiers > 0);
m_loop.m_num_iocp_notifiers--;
}
void EventLoopIocpNotifier::prepare ()
{
AIPSTACK_ASSERT(m_iocp_resource == nullptr);
AIPSTACK_ASSERT(!m_busy);
auto temp_iocp_resource = std::make_unique<IocpResource>();
temp_iocp_resource->overlapped = {};
temp_iocp_resource->loop = &m_loop;
temp_iocp_resource->notifier = this;
m_iocp_resource = temp_iocp_resource.release();
m_loop.m_num_iocp_resources++;
}
void EventLoopIocpNotifier::reset ()
{
if (m_iocp_resource != nullptr) {
if (m_busy) {
m_iocp_resource->notifier = nullptr;
} else {
AIPSTACK_ASSERT(m_loop.m_num_iocp_resources > 0);
m_loop.m_num_iocp_resources--;
delete m_iocp_resource;
}
m_iocp_resource = nullptr;
m_busy = false;
}
}
void EventLoopIocpNotifier::ioStarted (std::shared_ptr<void> user_resource)
{
AIPSTACK_ASSERT(m_iocp_resource != nullptr);
AIPSTACK_ASSERT(!m_busy);
m_iocp_resource->user_resource = std::move(user_resource);
m_busy = true;
}
OVERLAPPED & EventLoopIocpNotifier::getOverlapped ()
{
AIPSTACK_ASSERT(m_iocp_resource != nullptr);
return m_iocp_resource->overlapped;
}
bool EventLoop::addHandleToIocp (HANDLE handle, DWORD &out_error)
{
auto iocp_res = ::CreateIoCompletionPort(
handle, EventProvider::getIocpHandle(),
/*CompletionKey=*/reinterpret_cast<ULONG_PTR>(this),
/*NumberOfConcurrentThreads=*/0);
if (iocp_res == nullptr) {
out_error = ::GetLastError();
return false;
}
return true;
}
bool EventLoop::handle_iocp_result (void *completion_key, OVERLAPPED *overlapped)
{
AIPSTACK_ASSERT(completion_key == this);
IocpResource *iocp_resource = reinterpret_cast<IocpResource *>(overlapped);
AIPSTACK_ASSERT(iocp_resource->loop == this);
iocp_resource->user_resource.reset();
EventLoopIocpNotifier *notifier = iocp_resource->notifier;
if (notifier == nullptr) {
AIPSTACK_ASSERT(m_num_iocp_resources > 0);
m_num_iocp_resources--;
delete iocp_resource;
} else {
AIPSTACK_ASSERT(¬ifier->m_loop == this);
AIPSTACK_ASSERT(notifier->m_busy);
AIPSTACK_ASSERT(notifier->m_iocp_resource == iocp_resource);
notifier->m_busy = false;
notifier->m_handler();
if (AIPSTACK_UNLIKELY(m_stop)) {
return false;
}
}
return true;
}
void EventLoop::wait_for_final_iocp_results ()
{
bool first_try = true;
while (m_num_iocp_resources > 0) {
// Call waitForEvents only on non-first iterations, after having just called
// dispatchEvents. This is because we must not call waitForEvents before all
// available events have been dispatched.
if (!first_try) {
EventProvider::waitForEvents(EventLoopTime::max());
}
first_try = false;
// Call dispatchEvents to wait for IOCP operations to complete.
bool dispatch_res = EventProvider::dispatchEvents();
// dispatchEvents only returns false if it observed m_stop after having called
// an event handler. This cannot happen here because there are no event handlers
// that could be called.
AIPSTACK_ASSERT(dispatch_res);
}
}
#endif
EventLoopAsyncSignal::EventLoopAsyncSignal (EventLoop &loop, SignalEventHandler handler) :
m_loop(loop),
m_handler(handler)
{
AsyncSignalList::markRemoved(*this);
m_loop.m_num_async_signals++;
}
EventLoopAsyncSignal::~EventLoopAsyncSignal ()
{
reset();
AIPSTACK_ASSERT(m_loop.m_num_async_signals > 0);
m_loop.m_num_async_signals--;
}
void EventLoopAsyncSignal::signal ()
{
bool inserted_first = false;
{
std::lock_guard<std::mutex> lock(m_loop.m_async_signal_mutex);
if (AsyncSignalList::isRemoved(*this)) {
inserted_first = AsyncSignalList::isLonely(m_loop.m_pending_async_list);
AsyncSignalList::initBefore(*this, m_loop.m_pending_async_list);
}
}
if (inserted_first) {
m_loop.EventProvider::signalToCheckAsyncSignals();
}
}
void EventLoopAsyncSignal::reset ()
{
{
std::lock_guard<std::mutex> lock(m_loop.m_async_signal_mutex);
if (!AsyncSignalList::isRemoved(*this)) {
AsyncSignalList::remove(*this);
AsyncSignalList::markRemoved(*this);
}
}
}
}
#include AIPSTACK_EVENT_PROVIDER_IMPL_FILE