forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_service.cpp
More file actions
1020 lines (875 loc) · 25.9 KB
/
Copy pathio_service.cpp
File metadata and controls
1020 lines (875 loc) · 25.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/io_service.hpp>
#include <cppcoro/on_scope_exit.hpp>
#include <system_error>
#include <cassert>
#include <algorithm>
#include <thread>
#if CPPCORO_OS_WINNT
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <WinSock2.h>
# include <WS2tcpip.h>
# include <MSWSock.h>
# include <Windows.h>
#endif
namespace
{
#if CPPCORO_OS_WINNT
cppcoro::detail::win32::safe_handle create_io_completion_port(std::uint32_t concurrencyHint)
{
HANDLE handle = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, concurrencyHint);
if (handle == NULL)
{
DWORD errorCode = ::GetLastError();
throw std::system_error
{
static_cast<int>(errorCode),
std::system_category(),
"Error creating io_service: CreateIoCompletionPort"
};
}
return cppcoro::detail::win32::safe_handle{ handle };
}
cppcoro::detail::win32::safe_handle create_auto_reset_event()
{
HANDLE eventHandle = ::CreateEventW(nullptr, FALSE, FALSE, nullptr);
if (eventHandle == NULL)
{
const DWORD errorCode = ::GetLastError();
throw std::system_error
{
static_cast<int>(errorCode),
std::system_category(),
"Error creating manual reset event: CreateEventW"
};
}
return cppcoro::detail::win32::safe_handle{ eventHandle };
}
cppcoro::detail::win32::safe_handle create_waitable_timer_event()
{
const BOOL isManualReset = FALSE;
HANDLE handle = ::CreateWaitableTimerW(nullptr, isManualReset, nullptr);
if (handle == nullptr)
{
const DWORD errorCode = ::GetLastError();
throw std::system_error
{
static_cast<int>(errorCode),
std::system_category()
};
}
return cppcoro::detail::win32::safe_handle{ handle };
}
#endif
}
/// \brief
/// A queue of pending timers that supports efficiently determining
/// and dequeueing the earliest-due timers in the queue.
///
/// Implementation utilises a heap-sorted vector of entries with an
/// additional sorted linked-list that can be used as a fallback in
/// cases that there was insufficient memory to store all timer
/// entries in the vector.
///
/// This fallback is required to guarantee that all operations on this
/// queue are noexcept.s
class cppcoro::io_service::timer_queue
{
public:
using time_point = std::chrono::high_resolution_clock::time_point;
timer_queue() noexcept;
~timer_queue();
bool is_empty() const noexcept;
time_point earliest_due_time() const noexcept;
void enqueue_timer(cppcoro::io_service::timed_schedule_operation* timer) noexcept;
void dequeue_due_timers(
time_point currentTime,
cppcoro::io_service::timed_schedule_operation*& timerList) noexcept;
void remove_cancelled_timers(
cppcoro::io_service::timed_schedule_operation*& timerList) noexcept;
private:
struct timer_entry
{
timer_entry(cppcoro::io_service::timed_schedule_operation* timer)
: m_dueTime(timer->m_resumeTime)
, m_timer(timer)
{}
time_point m_dueTime;
cppcoro::io_service::timed_schedule_operation* m_timer;
};
static bool compare_entries(const timer_entry& a, const timer_entry& b) noexcept
{
return a.m_dueTime > b.m_dueTime;
}
// A heap-sorted list of active timer entries
// Earliest due timer is at the front of the queue
std::vector<timer_entry> m_timerEntries;
// Linked-list of overflow timer entries used in case there was
// insufficient memory available to grow m_timerEntries.
// List is sorted in ascending order of due-time using insertion-sort.
// This is required to support the noexcept guarantee of enqueue_timer().
cppcoro::io_service::timed_schedule_operation* m_overflowTimers;
};
cppcoro::io_service::timer_queue::timer_queue() noexcept
: m_timerEntries()
, m_overflowTimers(nullptr)
{}
cppcoro::io_service::timer_queue::~timer_queue()
{
assert(is_empty());
}
bool cppcoro::io_service::timer_queue::is_empty() const noexcept
{
return m_timerEntries.empty() && m_overflowTimers == nullptr;
}
cppcoro::io_service::timer_queue::time_point
cppcoro::io_service::timer_queue::earliest_due_time() const noexcept
{
if (!m_timerEntries.empty())
{
if (m_overflowTimers != nullptr)
{
return std::min(
m_timerEntries.front().m_dueTime,
m_overflowTimers->m_resumeTime);
}
return m_timerEntries.front().m_dueTime;
}
else if (m_overflowTimers != nullptr)
{
return m_overflowTimers->m_resumeTime;
}
return time_point::max();
}
void cppcoro::io_service::timer_queue::enqueue_timer(
cppcoro::io_service::timed_schedule_operation* timer) noexcept
{
try
{
m_timerEntries.emplace_back(timer);
std::push_heap(m_timerEntries.begin(), m_timerEntries.end(), compare_entries);
}
catch (...)
{
const auto& newDueTime = timer->m_resumeTime;
auto** current = &m_overflowTimers;
while ((*current) != nullptr && (*current)->m_resumeTime <= newDueTime)
{
current = &(*current)->m_next;
}
timer->m_next = *current;
*current = timer;
}
}
void cppcoro::io_service::timer_queue::dequeue_due_timers(
time_point currentTime,
cppcoro::io_service::timed_schedule_operation*& timerList) noexcept
{
while (!m_timerEntries.empty() && m_timerEntries.front().m_dueTime <= currentTime)
{
auto* timer = m_timerEntries.front().m_timer;
std::pop_heap(m_timerEntries.begin(), m_timerEntries.end(), compare_entries);
m_timerEntries.pop_back();
timer->m_next = timerList;
timerList = timer;
}
while (m_overflowTimers != nullptr && m_overflowTimers->m_resumeTime <= currentTime)
{
auto* timer = m_overflowTimers;
m_overflowTimers = timer->m_next;
timer->m_next = timerList;
timerList = timer;
}
}
void cppcoro::io_service::timer_queue::remove_cancelled_timers(
cppcoro::io_service::timed_schedule_operation*& timerList) noexcept
{
// Perform a linear scan of all timers looking for any that have
// had cancellation requested.
const auto addTimerToList = [&](timed_schedule_operation* timer)
{
timer->m_next = timerList;
timerList = timer;
};
const auto isTimerCancelled = [](const timer_entry& entry)
{
return entry.m_timer->m_cancellationToken.is_cancellation_requested();
};
auto firstCancelledEntry = std::find_if(
m_timerEntries.begin(),
m_timerEntries.end(),
isTimerCancelled);
if (firstCancelledEntry != m_timerEntries.end())
{
auto nonCancelledEnd = firstCancelledEntry;
addTimerToList(nonCancelledEnd->m_timer);
for (auto iter = firstCancelledEntry + 1;
iter != m_timerEntries.end();
++iter)
{
if (isTimerCancelled(*iter))
{
addTimerToList(iter->m_timer);
}
else
{
*nonCancelledEnd++ = std::move(*iter);
}
}
m_timerEntries.erase(nonCancelledEnd, m_timerEntries.end());
std::make_heap(
m_timerEntries.begin(),
m_timerEntries.end(),
compare_entries);
}
{
timed_schedule_operation** current = &m_overflowTimers;
while ((*current) != nullptr)
{
auto* timer = (*current);
if (timer->m_cancellationToken.is_cancellation_requested())
{
*current = timer->m_next;
addTimerToList(timer);
}
else
{
current = &timer->m_next;
}
}
}
}
class cppcoro::io_service::timer_thread_state
{
public:
timer_thread_state();
~timer_thread_state();
timer_thread_state(const timer_thread_state& other) = delete;
timer_thread_state& operator=(const timer_thread_state& other) = delete;
void request_timer_cancellation() noexcept;
void run() noexcept;
void wake_up_timer_thread() noexcept;
#if CPPCORO_OS_WINNT
detail::win32::safe_handle m_wakeUpEvent;
detail::win32::safe_handle m_waitableTimerEvent;
#endif
std::atomic<io_service::timed_schedule_operation*> m_newlyQueuedTimers;
std::atomic<bool> m_timerCancellationRequested;
std::atomic<bool> m_shutDownRequested;
std::thread m_thread;
};
cppcoro::io_service::io_service()
: io_service(0)
{
}
cppcoro::io_service::io_service(std::uint32_t concurrencyHint)
: m_threadState(0)
, m_workCount(0)
#if CPPCORO_OS_WINNT
, m_iocpHandle(create_io_completion_port(concurrencyHint))
, m_winsockInitialised(false)
, m_winsockInitialisationMutex()
#endif
, m_scheduleOperations(nullptr)
, m_timerState(nullptr)
{
}
cppcoro::io_service::~io_service()
{
assert(m_scheduleOperations.load(std::memory_order_relaxed) == nullptr);
assert(m_threadState.load(std::memory_order_relaxed) < active_thread_count_increment);
delete m_timerState.load(std::memory_order_relaxed);
#if CPPCORO_OS_WINNT
if (m_winsockInitialised.load(std::memory_order_relaxed))
{
// TODO: Should we be checking return-code here?
// Don't want to throw from the destructor, so perhaps just log an error?
(void)::WSACleanup();
}
#endif
}
cppcoro::io_service::schedule_operation cppcoro::io_service::schedule() noexcept
{
return schedule_operation{ *this };
}
std::uint64_t cppcoro::io_service::process_events()
{
std::uint64_t eventCount = 0;
if (try_enter_event_loop())
{
auto exitLoop = on_scope_exit([&] { exit_event_loop(); });
constexpr bool waitForEvent = true;
while (try_process_one_event(waitForEvent))
{
++eventCount;
}
}
return eventCount;
}
std::uint64_t cppcoro::io_service::process_pending_events()
{
std::uint64_t eventCount = 0;
if (try_enter_event_loop())
{
auto exitLoop = on_scope_exit([&] { exit_event_loop(); });
constexpr bool waitForEvent = false;
while (try_process_one_event(waitForEvent))
{
++eventCount;
}
}
return eventCount;
}
std::uint64_t cppcoro::io_service::process_one_event()
{
std::uint64_t eventCount = 0;
if (try_enter_event_loop())
{
auto exitLoop = on_scope_exit([&] { exit_event_loop(); });
constexpr bool waitForEvent = true;
if (try_process_one_event(waitForEvent))
{
++eventCount;
}
}
return eventCount;
}
std::uint64_t cppcoro::io_service::process_one_pending_event()
{
std::uint64_t eventCount = 0;
if (try_enter_event_loop())
{
auto exitLoop = on_scope_exit([&] { exit_event_loop(); });
constexpr bool waitForEvent = false;
if (try_process_one_event(waitForEvent))
{
++eventCount;
}
}
return eventCount;
}
void cppcoro::io_service::stop() noexcept
{
const auto oldState = m_threadState.fetch_or(stop_requested_flag, std::memory_order_release);
if ((oldState & stop_requested_flag) == 0)
{
for (auto activeThreadCount = oldState / active_thread_count_increment;
activeThreadCount > 0;
--activeThreadCount)
{
post_wake_up_event();
}
}
}
void cppcoro::io_service::reset()
{
const auto oldState = m_threadState.fetch_and(~stop_requested_flag, std::memory_order_relaxed);
// Check that there were no active threads running the event loop.
assert(oldState == stop_requested_flag);
}
bool cppcoro::io_service::is_stop_requested() const noexcept
{
return (m_threadState.load(std::memory_order_acquire) & stop_requested_flag) != 0;
}
void cppcoro::io_service::notify_work_started() noexcept
{
m_workCount.fetch_add(1, std::memory_order_relaxed);
}
void cppcoro::io_service::notify_work_finished() noexcept
{
if (m_workCount.fetch_sub(1, std::memory_order_relaxed) == 1)
{
stop();
}
}
cppcoro::detail::win32::handle_t cppcoro::io_service::native_iocp_handle() noexcept
{
return m_iocpHandle.handle();
}
#if CPPCORO_OS_WINNT
void cppcoro::io_service::ensure_winsock_initialised()
{
if (!m_winsockInitialised.load(std::memory_order_acquire))
{
std::lock_guard<std::mutex> lock(m_winsockInitialisationMutex);
if (!m_winsockInitialised.load(std::memory_order_acquire))
{
const WORD requestedVersion = MAKEWORD(2, 2);
WSADATA winsockData;
const int result = ::WSAStartup(requestedVersion, &winsockData);
if (result == SOCKET_ERROR)
{
const int errorCode = ::WSAGetLastError();
throw std::system_error(
errorCode,
std::system_category(),
"Error initialsing winsock: WSAStartup");
}
m_winsockInitialised.store(true, std::memory_order_release);
}
}
}
#endif // CPPCORO_OS_WINNT
void cppcoro::io_service::schedule_impl(schedule_operation* operation) noexcept
{
#if CPPCORO_OS_WINNT
const BOOL ok = ::PostQueuedCompletionStatus(
m_iocpHandle.handle(),
0,
reinterpret_cast<ULONG_PTR>(operation->m_awaiter.address()),
nullptr);
if (!ok)
{
// Failed to post to the I/O completion port.
//
// This is most-likely because the queue is currently full.
//
// We'll queue up the operation to a linked-list using a lock-free
// push and defer the dispatch to the completion port until some I/O
// thread next enters its event loop.
auto* head = m_scheduleOperations.load(std::memory_order_acquire);
do
{
operation->m_next = head;
} while (!m_scheduleOperations.compare_exchange_weak(
head,
operation,
std::memory_order_release,
std::memory_order_acquire));
}
#endif
}
void cppcoro::io_service::try_reschedule_overflow_operations() noexcept
{
#if CPPCORO_OS_WINNT
auto* operation = m_scheduleOperations.exchange(nullptr, std::memory_order_acquire);
while (operation != nullptr)
{
auto* next = operation->m_next;
BOOL ok = ::PostQueuedCompletionStatus(
m_iocpHandle.handle(),
0,
reinterpret_cast<ULONG_PTR>(operation->m_awaiter.address()),
nullptr);
if (!ok)
{
// Still unable to queue these operations.
// Put them back on the list of overflow operations.
auto* tail = operation;
while (tail->m_next != nullptr)
{
tail = tail->m_next;
}
schedule_operation* head = nullptr;
while (!m_scheduleOperations.compare_exchange_weak(
head,
operation,
std::memory_order_release,
std::memory_order_relaxed))
{
tail->m_next = head;
}
return;
}
operation = next;
}
#endif
}
bool cppcoro::io_service::try_enter_event_loop() noexcept
{
auto currentState = m_threadState.load(std::memory_order_relaxed);
do
{
if ((currentState & stop_requested_flag) != 0)
{
return false;
}
} while (!m_threadState.compare_exchange_weak(
currentState,
currentState + active_thread_count_increment,
std::memory_order_relaxed));
return true;
}
void cppcoro::io_service::exit_event_loop() noexcept
{
m_threadState.fetch_sub(active_thread_count_increment, std::memory_order_relaxed);
}
bool cppcoro::io_service::try_process_one_event(bool waitForEvent)
{
#if CPPCORO_OS_WINNT
if (is_stop_requested())
{
return false;
}
const DWORD timeout = waitForEvent ? INFINITE : 0;
while (true)
{
// Check for any schedule_operation objects that were unable to be
// queued to the I/O completion port and try to requeue them now.
try_reschedule_overflow_operations();
DWORD numberOfBytesTransferred = 0;
ULONG_PTR completionKey = 0;
LPOVERLAPPED overlapped = nullptr;
BOOL ok = ::GetQueuedCompletionStatus(
m_iocpHandle.handle(),
&numberOfBytesTransferred,
&completionKey,
&overlapped,
timeout);
if (overlapped != nullptr)
{
DWORD errorCode = ok ? ERROR_SUCCESS : ::GetLastError();
auto* state = static_cast<detail::win32::io_state*>(
reinterpret_cast<detail::win32::overlapped*>(overlapped));
state->m_callback(
state,
errorCode,
numberOfBytesTransferred,
completionKey);
return true;
}
else if (ok)
{
if (completionKey != 0)
{
// This was a coroutine scheduled via a call to
// io_service::schedule().
std::experimental::coroutine_handle<>::from_address(
reinterpret_cast<void*>(completionKey)).resume();
return true;
}
// Empty event is a wake-up request, typically associated with a
// request to exit the event loop.
// However, there may be spurious such events remaining in the queue
// from a previous call to stop() that has since been reset() so we
// need to check whether stop is still required.
if (is_stop_requested())
{
return false;
}
}
else
{
const DWORD errorCode = ::GetLastError();
if (errorCode == WAIT_TIMEOUT)
{
return false;
}
throw std::system_error
{
static_cast<int>(errorCode),
std::system_category(),
"Error retrieving item from io_service queue: GetQueuedCompletionStatus"
};
}
}
#endif
}
void cppcoro::io_service::post_wake_up_event() noexcept
{
#if CPPCORO_OS_WINNT
// We intentionally ignore the return code here.
//
// Assume that if posting an event failed that it failed because the queue was full
// and the system is out of memory. In this case threads should find other events
// in the queue next time they check anyway and thus wake-up.
(void)::PostQueuedCompletionStatus(m_iocpHandle.handle(), 0, 0, nullptr);
#endif
}
cppcoro::io_service::timer_thread_state*
cppcoro::io_service::ensure_timer_thread_started()
{
auto* timerState = m_timerState.load(std::memory_order_acquire);
if (timerState == nullptr)
{
auto newTimerState = std::make_unique<timer_thread_state>();
if (m_timerState.compare_exchange_strong(
timerState,
newTimerState.get(),
std::memory_order_release,
std::memory_order_acquire))
{
// We managed to install our timer_thread_state before some
// other thread did, don't free it here - it will be freed in
// the io_service destructor.
timerState = newTimerState.release();
}
}
return timerState;
}
cppcoro::io_service::timer_thread_state::timer_thread_state()
#if CPPCORO_OS_WINNT
: m_wakeUpEvent(create_auto_reset_event())
, m_waitableTimerEvent(create_waitable_timer_event())
#endif
, m_newlyQueuedTimers(nullptr)
, m_timerCancellationRequested(false)
, m_shutDownRequested(false)
, m_thread([this] { this->run(); })
{
}
cppcoro::io_service::timer_thread_state::~timer_thread_state()
{
m_shutDownRequested.store(true, std::memory_order_release);
wake_up_timer_thread();
m_thread.join();
}
void cppcoro::io_service::timer_thread_state::request_timer_cancellation() noexcept
{
const bool wasTimerCancellationAlreadyRequested =
m_timerCancellationRequested.exchange(true, std::memory_order_release);
if (!wasTimerCancellationAlreadyRequested)
{
wake_up_timer_thread();
}
}
void cppcoro::io_service::timer_thread_state::run() noexcept
{
#if CPPCORO_OS_WINNT
using clock = std::chrono::high_resolution_clock;
using time_point = clock::time_point;
timer_queue timerQueue;
const DWORD waitHandleCount = 2;
const HANDLE waitHandles[waitHandleCount] =
{
m_wakeUpEvent.handle(),
m_waitableTimerEvent.handle()
};
time_point lastSetWaitEventTime = time_point::max();
timed_schedule_operation* timersReadyToResume = nullptr;
DWORD timeout = INFINITE;
while (!m_shutDownRequested.load(std::memory_order_relaxed))
{
const DWORD waitResult = ::WaitForMultipleObjectsEx(
waitHandleCount,
waitHandles,
FALSE, // waitAll
timeout,
FALSE); // alertable
if (waitResult == WAIT_OBJECT_0 || waitResult == WAIT_FAILED)
{
// Wake-up event (WAIT_OBJECT_0)
//
// We are only woken up for:
// - handling timer cancellation
// - handling newly queued timers
// - shutdown
//
// We also handle WAIT_FAILED here so that we remain responsive
// to new timers and cancellation even if the OS fails to perform
// the wait operation for some reason.
// Handle cancelled timers
if (m_timerCancellationRequested.exchange(false, std::memory_order_acquire))
{
timerQueue.remove_cancelled_timers(timersReadyToResume);
}
// Handle newly queued timers
auto* newTimers = m_newlyQueuedTimers.exchange(nullptr, std::memory_order_acquire);
while (newTimers != nullptr)
{
auto* timer = newTimers;
newTimers = timer->m_next;
if (timer->m_cancellationToken.is_cancellation_requested())
{
timer->m_next = timersReadyToResume;
timersReadyToResume = timer;
}
else
{
timerQueue.enqueue_timer(timer);
}
}
}
else if (waitResult == (WAIT_OBJECT_0 + 1))
{
lastSetWaitEventTime = time_point::max();
}
if (!timerQueue.is_empty())
{
time_point currentTime = clock::now();
timerQueue.dequeue_due_timers(currentTime, timersReadyToResume);
if (!timerQueue.is_empty())
{
auto earliestDueTime = timerQueue.earliest_due_time();
assert(earliestDueTime > currentTime);
// Set the waitable timer before trying to schedule any of the ready-to-run
// timers to avoid the concept of 'current time' on which we calculate the
// amount of time to wait until the next timer is ready.
if (earliestDueTime != lastSetWaitEventTime)
{
using ticks = std::chrono::duration<LONGLONG, std::ratio<1, 10'000'000>>;
auto timeUntilNextDueTime = earliestDueTime - currentTime;
// Negative value indicates relative time.
LARGE_INTEGER dueTime;
dueTime.QuadPart = -std::chrono::duration_cast<ticks>(timeUntilNextDueTime).count();
// Period of 0 indicates no repeat on the timer.
const LONG period = 0;
// Don't wake the system from a suspended state just to
// raise the timer event.
const BOOL resumeFromSuspend = FALSE;
const BOOL ok = ::SetWaitableTimer(
m_waitableTimerEvent.handle(),
&dueTime,
period,
nullptr,
nullptr,
resumeFromSuspend);
if (ok)
{
lastSetWaitEventTime = earliestDueTime;
timeout = INFINITE;
}
else
{
// Not sure what could cause the call to SetWaitableTimer()
// to fail here but we'll just try falling back to using
// the timeout parameter of the WaitForMultipleObjects() call.
//
// wake-up at least once every second and retry setting
// the timer at that point.
using namespace std::literals::chrono_literals;
if (timeUntilNextDueTime > 1s)
{
timeout = 1000;
}
else if (timeUntilNextDueTime > 1ms)
{
timeout = static_cast<DWORD>(
std::chrono::duration_cast<std::chrono::milliseconds>(
timeUntilNextDueTime).count());
}
else
{
timeout = 1;
}
}
}
}
}
// Now schedule any ready-to-run timers.
while (timersReadyToResume != nullptr)
{
auto* timer = timersReadyToResume;
auto* nextTimer = timer->m_next;
// Use 'release' memory order to ensure that any prior writes to
// m_next "happen before" any potential uses of that same memory
// back on the thread that is executing timed_schedule_operation::await_suspend()
// which has the synchronising 'acquire' semantics.
if (timer->m_refCount.fetch_sub(1, std::memory_order_release) == 1)
{
timer->m_scheduleOperation.m_service.schedule_impl(
&timer->m_scheduleOperation);
}
timersReadyToResume = nextTimer;
}
}
#endif
}
void cppcoro::io_service::timer_thread_state::wake_up_timer_thread() noexcept
{
#if CPPCORO_OS_WINNT
(void)::SetEvent(m_wakeUpEvent.handle());
#endif
}
void cppcoro::io_service::schedule_operation::await_suspend(
std::experimental::coroutine_handle<> awaiter) noexcept
{
m_awaiter = awaiter;
m_service.schedule_impl(this);
}
cppcoro::io_service::timed_schedule_operation::timed_schedule_operation(
io_service& service,
std::chrono::high_resolution_clock::time_point resumeTime,
cppcoro::cancellation_token cancellationToken) noexcept
: m_scheduleOperation(service)
, m_resumeTime(resumeTime)
, m_cancellationToken(std::move(cancellationToken))
, m_refCount(2)
{
}
cppcoro::io_service::timed_schedule_operation::timed_schedule_operation(
timed_schedule_operation&& other) noexcept
: m_scheduleOperation(std::move(other.m_scheduleOperation))
, m_resumeTime(std::move(other.m_resumeTime))
, m_cancellationToken(std::move(other.m_cancellationToken))
, m_refCount(2)
{
}
cppcoro::io_service::timed_schedule_operation::~timed_schedule_operation()
{
}
bool cppcoro::io_service::timed_schedule_operation::await_ready() const noexcept
{
return m_cancellationToken.is_cancellation_requested();
}
void cppcoro::io_service::timed_schedule_operation::await_suspend(
std::experimental::coroutine_handle<> awaiter)
{
m_scheduleOperation.m_awaiter = awaiter;
auto& service = m_scheduleOperation.m_service;
// Ensure the timer state is initialised and the timer thread started.
auto* timerState = service.ensure_timer_thread_started();
if (m_cancellationToken.can_be_cancelled())
{
m_cancellationRegistration.emplace(m_cancellationToken, [timerState]
{
timerState->request_timer_cancellation();
});
}
// Queue the timer schedule to the queue of incoming new timers.
//
// We need to do a careful dance here because it could be possible
// that immediately after queueing the timer this thread could be
// context-switched out, the timer thread could pick it up and
// schedule it to be resumed, it could be resumed on an I/O thread
// and complete its work and the io_service could be destructed.
// All before we get to execute timerState.wake_up_timer_thread()
// below. To work around this race we use a reference-counter
// with initial value 2 and have both the timer thread and this
// thread decrement the count once the awaiter is ready to be
// rescheduled. Whichever thread decrements the ref-count to 0
// is responsible for scheduling the awaiter for resumption.
// Not sure if we need 'acquire' semantics on this load and
// on the failure-case of the compare_exchange below.
//
// It could potentially be made 'release' if we can guarantee
// that a read-with 'acquire' semantics in the timer thread
// of the latest value will synchronise with all prior writes
// to that value that used 'release' semantics.
auto* prev = timerState->m_newlyQueuedTimers.load(std::memory_order_acquire);
do
{
m_next = prev;
} while (!timerState->m_newlyQueuedTimers.compare_exchange_weak(
prev,
this,
std::memory_order_release,
std::memory_order_acquire));
if (prev == nullptr)
{