-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathntci_timer.h
833 lines (811 loc) · 29.2 KB
/
ntci_timer.h
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
// Copyright 2020-2023 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef INCLUDED_NTCI_TIMER
#define INCLUDED_NTCI_TIMER
#include <bsls_ident.h>
BSLS_IDENT("$Id: $")
#include <ntccfg_platform.h>
#include <ntci_strand.h>
#include <ntcscm_version.h>
#include <ntsa_error.h>
#include <bdlb_nullablevalue.h>
#include <bslmt_threadutil.h>
#include <bsls_timeinterval.h>
#include <bsls_types.h>
#include <bsl_memory.h>
namespace BloombergLP {
namespace ntci {
class Timer;
}
namespace ntci {
/// Provide an interface to a one-shot or periodic timer.
///
/// @details
/// Schedulers of timers are notified of the expiration deadline, recurrence,
/// and optionally, the cancellation and closure of the timer, according to the
/// timer options specified when the timer is created.
///
/// @par Accuracy and Precision
/// Timer deadlines may be specified with nanosecond precision, but the
/// effective precision and accuracy varies depending on the operating system
/// running the current process and the driver implementing the timer. On Linux
/// using 'epoll', and on Darwin/FreeBSD using 'kqueue', microsecond precision
/// is supported. Users of all other operating systems and drivers should only
/// expect millisecond precision. The effective accuracy of the timers is
/// high on bare-metal Linux, but lower on other operating systems and timer
/// drivers. Users on bare-metal Linux using 'epoll' as the timer driver may
/// expect up to an accuracy of a few dozen microseconds. Users on other
/// operating system and drivers should only expect an accuracy of several
/// milliseconds. Accuracy is generally much lower on virtual machines. Users
/// may access the "drift", or time between the scheduled deadline and when the
/// timer deadline event callback is invoked, through the timer context
/// specified to the timer deadline event callback.
///
/// @par Closing
/// Each 'ntci::Timer' is shared between the user and this library's
/// asynchronous machinery. It is not sufficient for users to simply release
/// their reference counts on a timer object to close and destroy it. Users
/// *must* explicitly close each non-one-shot 'ntci::Timer'. One-shot timers
/// are automatically closed after they fire. Closing a timer is asynchronous,
/// and may race with announcement of a timer's deadline event by another
/// thread. If such a race needs to be resolved, users must wait until either
/// the timer callback is invoked with a timer event of type
/// 'ntca::TimerEventType::e_CLOSED' or the
/// 'ntci::TimerSession::processTimerClosed' function is invoked (depending on
/// which notification strategy is registered when the timer is created) before
/// assuming the timer is completely closed. After a timer is closed, the timer
/// remains in a valid state but all member functions with failure modes will
/// return an error. The timer object will be destroyed only after it has
/// been closed and all references are released.
///
/// @par Thread Safety
/// This class is thread safe.
///
/// @par Usage Example 1: Scheduling a one-shot timer
/// This example illustrates how to schedule a one-shot deadline of a timer.
///
/// First, initialize the library.
///
/// ntcf::System::initialize();
/// ntcf::System::ignore(ntscfg::Signal::e_PIPE);
///
/// Next, acquire an implementation of the 'ntci::TimerFactory' abstract class.
/// For this example, create and start an 'ntci::Interface'. Note that timers
/// may also be created from other objects; see the component documentation for
/// more details.
///
/// ntca::InterfaceConfig interfaceConfig;
/// interfaceConfig.setThreadName("example");
/// interfaceConfig.setMinThreads(1);
/// interfaceConfig.setMaxThreads(1);
///
/// bsl::shared_ptr<ntci::Interface> interface =
/// ntcf::System::createInterface(interfaceConfig);
///
/// interface->start();
///
/// Define the timer configuration for a one-shot timer.
///
/// ntca::TimerOptions timerOptions;
///
/// timerOptions.showEvent(ntca::TimerEventType::e_DEADLINE);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CANCELED);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CLOSED);
/// timerOptions.setOneShot(true);
///
/// Create a timer whose callback arrives at a timer future.
///
/// ntci::TimerFuture timerFuture;
///
/// bsl::shared_ptr<ntci::Timer> timer =
/// interface->createTimer(timerOptions, timerFuture);
///
/// Schedule the timer 100ms from now.
///
/// bsls::TimeInterval duration;
/// duration.setTotalMilliseconds(100);
///
/// error = timer->schedule(timer->currentTime() + duration,
/// bsls::TimeInterval());
/// BSLS_ASSERT(!error);
///
/// Wait for an arrival of the timer future.
///
/// ntci::TimerResult timerResult;
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer's deadline has
/// arrived.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_DEADLINE);
///
/// Note that the timer does not need to be explicitly closed since it
/// has been configured as a one-shot timer.
///
/// Finally, stop the interface.
///
/// interface->shutdown();
/// interface->linger();
///
///
/// @par Usage Example 2: Scheduling a periodic timer
/// This example illustrates how to schedule a periodically-recurring deadline
/// of a timer.
///
/// First, initialize the library.
///
/// ntcf::System::initialize();
/// ntcf::System::ignore(ntscfg::Signal::e_PIPE);
///
/// Next, acquire an implementation of the 'ntci::TimerFactory' abstract class.
/// For this example, create and start an 'ntci::Interface'. Note that timers
/// may also be created from other objects; see the component documentation for
/// more details.
///
/// ntca::InterfaceConfig interfaceConfig;
/// interfaceConfig.setThreadName("example");
/// interfaceConfig.setMinThreads(1);
/// interfaceConfig.setMaxThreads(1);
///
/// bsl::shared_ptr<ntci::Interface> interface =
/// ntcf::System::createInterface(interfaceConfig);
///
/// interface->start();
///
/// Define the timer configuration for a multiple-shot timer.
///
/// ntca::TimerOptions timerOptions;
///
/// timerOptions.showEvent(ntca::TimerEventType::e_DEADLINE);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CANCELED);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CLOSED);
/// timerOptions.setOneShot(false);
///
/// Create a timer whose callback arrives at a timer future.
///
/// ntci::TimerFuture timerFuture;
///
/// bsl::shared_ptr<ntci::Timer> timer =
/// interface->createTimer(timerOptions, timerFuture);
///
/// Schedule the timer 100ms from now that recurs every 20ms.
///
/// bsls::TimeInterval duration;
/// duration.setTotalMilliseconds(100);
///
/// bsls::TimeInterval period;
/// period.setTotalMilliseconds(20);
///
/// error = timer->schedule(timer->currentTime() + duration, period);
/// BSLS_ASSERT(!error);
///
/// Wait for several arrivals of the timer future and ensure each the timer
/// future's result indicates the timer's deadline has arrived.
///
/// for (bsl::size_t i = 0; i < 5; ++i) {
/// ntci::TimerResult timerResult;
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_DEADLINE);
/// }
///
/// Close the timer to stop it recurring.
///
/// timer->close();
///
/// Finally, stop the interface.
///
/// interface->shutdown();
/// interface->linger();
///
///
/// @par Usage Example 3: Rescheduling a one-shot timer while it is pending
/// This example illustrates how to reschedule the one-shot deadline for a
/// timer while a previously-scheduled deadline is still pending.
///
/// First, initialize the library.
///
/// ntcf::System::initialize();
/// ntcf::System::ignore(ntscfg::Signal::e_PIPE);
///
/// Next, acquire an implementation of the 'ntci::TimerFactory' abstract class.
/// For this example, create and start an 'ntci::Interface'. Note that timers
/// may also be created from other objects; see the component documentation for
/// more details.
///
/// ntca::InterfaceConfig interfaceConfig;
/// interfaceConfig.setThreadName("example");
/// interfaceConfig.setMinThreads(1);
/// interfaceConfig.setMaxThreads(1);
///
/// bsl::shared_ptr<ntci::Interface> interface =
/// ntcf::System::createInterface(interfaceConfig);
///
/// interface->start();
///
/// Define the timer configuration for a one-shot timer.
///
/// ntca::TimerOptions timerOptions;
///
/// timerOptions.showEvent(ntca::TimerEventType::e_DEADLINE);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CANCELED);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CLOSED);
/// timerOptions.setOneShot(true);
///
/// Create a timer whose callback arrives at a timer future.
///
/// ntci::TimerFuture timerFuture;
///
/// bsl::shared_ptr<ntci::Timer> timer =
/// interface->createTimer(timerOptions, timerFuture);
///
/// Initially schedule the timer far into the future.
///
/// error = timer->schedule(bsls::TimeInterval(86400),
/// bsls::TimeInterval());
/// BSLS_ASSERT(!error);
///
/// Reschedule the timer 100ms from now.
///
/// bsls::TimeInterval duration;
/// duration.setTotalMilliseconds(100);
///
/// error = timer->schedule(timer->currentTime() + duration,
/// bsls::TimeInterval());
/// BSLS_ASSERT(!error);
///
/// Wait for an arrival of the timer future.
///
/// ntci::TimerResult timerResult;
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer's deadline has
/// arrived.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_DEADLINE);
///
/// Note that the timer does not need to be explicitly closed since it
/// has been configured as a one-shot timer.
///
/// Finally, stop the interface.
///
/// interface->shutdown();
/// interface->linger();
///
///
/// @par Usage Example 4: Rescheduling a periodic timer while it is pending
/// This example illustrates how to reschedule the periodically-recurring
/// deadline for a timer while a previously-scheduled deadline is still
/// pending.
///
/// First, initialize the library.
///
/// ntcf::System::initialize();
/// ntcf::System::ignore(ntscfg::Signal::e_PIPE);
///
/// Next, acquire an implementation of the 'ntci::TimerFactory' abstract class.
/// For this example, create and start an 'ntci::Interface'. Note that timers
/// may also be created from other objects; see the component documentation for
/// more details.
///
/// ntca::InterfaceConfig interfaceConfig;
/// interfaceConfig.setThreadName("example");
/// interfaceConfig.setMinThreads(1);
/// interfaceConfig.setMaxThreads(1);
///
/// bsl::shared_ptr<ntci::Interface> interface =
/// ntcf::System::createInterface(interfaceConfig);
///
/// interface->start();
///
/// Define the timer configuration for a multiple-shot timer.
///
/// ntca::TimerOptions timerOptions;
///
/// timerOptions.showEvent(ntca::TimerEventType::e_DEADLINE);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CANCELED);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CLOSED);
/// timerOptions.setOneShot(false);
///
/// Create a timer whose callback arrives at a timer future.
///
/// ntci::TimerFuture timerFuture;
///
/// bsl::shared_ptr<ntci::Timer> timer =
/// interface->createTimer(timerOptions, timerFuture);
///
/// Initially schedule the timer far into the future.
///
/// error = timer->schedule(bsls::TimeInterval(86400),
/// bsls::TimeInterval());
/// BSLS_ASSERT(!error);
///
/// Rechedule the timer 100ms from now that recurs every 20ms.
///
/// bsls::TimeInterval duration;
/// duration.setTotalMilliseconds(100);
///
/// bsls::TimeInterval period;
/// period.setTotalMilliseconds(20);
///
/// error = timer->schedule(timer->currentTime() + duration, period);
/// BSLS_ASSERT(!error);
///
/// Wait for several arrivals of the timer future and ensure each the timer
/// future's result indicates the timer's deadline has arrived.
///
/// for (bsl::size_t i = 0; i < 5; ++i) {
/// ntci::TimerResult timerResult;
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_DEADLINE);
/// }
///
/// Close the timer to stop it recurring.
///
/// timer->close();
///
/// Finally, stop the interface.
///
/// interface->shutdown();
/// interface->linger();
///
///
/// @par Usage Example 5: Canceling a one-shot timer while it is pending
/// This example illustrates how to cancel a one-shot deadline of a timer while
/// a previously-scheduled deadline is still pending.
///
/// First, initialize the library.
///
/// ntcf::System::initialize();
/// ntcf::System::ignore(ntscfg::Signal::e_PIPE);
///
/// Next, acquire an implementation of the 'ntci::TimerFactory' abstract class.
/// For this example, create and start an 'ntci::Interface'. Note that timers
/// may also be created from other objects; see the component documentation for
/// more details.
///
/// ntca::InterfaceConfig interfaceConfig;
/// interfaceConfig.setThreadName("example");
/// interfaceConfig.setMinThreads(1);
/// interfaceConfig.setMaxThreads(1);
///
/// bsl::shared_ptr<ntci::Interface> interface =
/// ntcf::System::createInterface(interfaceConfig);
///
/// interface->start();
///
/// Define the timer configuration for a one-shot timer.
///
/// ntca::TimerOptions timerOptions;
///
/// timerOptions.showEvent(ntca::TimerEventType::e_DEADLINE);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CANCELED);
/// timerOptions.showEvent(ntca::TimerEventType::e_CLOSED);
/// timerOptions.setOneShot(true);
///
/// Create a timer whose callback arrives at a timer future.
///
/// ntci::TimerFuture timerFuture;
///
/// bsl::shared_ptr<ntci::Timer> timer =
/// interface->createTimer(timerOptions, timerFuture);
///
/// Initially schedule the timer far into the future.
///
/// error = timer->schedule(bsls::TimeInterval(86400),
/// bsls::TimeInterval());
/// BSLS_ASSERT(!error);
///
/// Close the timer. Note that the timer is closed instead of canceled
/// because the timer is not intended to be scheduled again; closing a
/// timer implicitly cancels it.
///
/// error = timer->close();
/// if (error) {
/// BSLS_ASSERT(error == ntsa::Error(ntsa::Error::e_CANCELLED));
/// }
///
/// Wait for an arrival of the timer future.
///
/// ntci::TimerResult timerResult;
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer was closed.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_CLOSED);
///
/// Finally, stop the interface.
///
/// interface->shutdown();
/// interface->linger();
///
///
/// @par Usage Example 6: Canceling a periodic timer while it is pending
/// This example illustrates how to cancel a periodically-recurring deadline
/// for a timer whlie a previously-scheduled deadline is still pending.
///
/// First, initialize the library.
///
/// ntcf::System::initialize();
/// ntcf::System::ignore(ntscfg::Signal::e_PIPE);
///
/// Next, acquire an implementation of the 'ntci::TimerFactory' abstract class.
/// For this example, create and start an 'ntci::Interface'. Note that timers
/// may also be created from other objects; see the component documentation for
/// more details.
///
/// ntca::InterfaceConfig interfaceConfig;
/// interfaceConfig.setThreadName("example");
/// interfaceConfig.setMinThreads(1);
/// interfaceConfig.setMaxThreads(1);
///
/// bsl::shared_ptr<ntci::Interface> interface =
/// ntcf::System::createInterface(interfaceConfig);
///
/// interface->start();
///
/// Define the timer configuration for a multiple-shot timer.
///
/// ntca::TimerOptions timerOptions;
///
/// timerOptions.showEvent(ntca::TimerEventType::e_DEADLINE);
/// timerOptions.hideEvent(ntca::TimerEventType::e_CANCELED);
/// timerOptions.showEvent(ntca::TimerEventType::e_CLOSED);
/// timerOptions.setOneShot(false);
///
/// Create a timer whose callback arrives at a timer future.
///
/// ntci::TimerFuture timerFuture;
///
/// bsl::shared_ptr<ntci::Timer> timer =
/// interface->createTimer(timerOptions, timerFuture);
///
/// Initially schedule the timer far into the future.
///
/// error = timer->schedule(bsls::TimeInterval(86400),
/// bsls::TimeInterval());
/// BSLS_ASSERT(!error);
///
/// Close the timer. Note that the timer is closed instead of canceled
/// because the timer is not intended to be scheduled again; closing a
/// timer implicitly cancels it.
///
/// error = timer->close();
/// if (error) {
/// BSLS_ASSERT(error == ntsa::Error(ntsa::Error::e_CANCELLED));
/// }
///
/// Wait for an arrival of the timer future.
///
/// ntci::TimerResult timerResult;
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer was closed.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_CLOSED);
///
/// Finally, stop the interface.
///
/// interface->shutdown();
/// interface->linger();
///
///
/// @par Usage Example 7: Rescheduling a one-shot timer after cancellation
/// This example illustrates how to reschedule a one-shot deadline of a timer
/// after it has been canceled.
///
/// First, initialize the library.
///
/// ntcf::System::initialize();
/// ntcf::System::ignore(ntscfg::Signal::e_PIPE);
///
/// Next, acquire an implementation of the 'ntci::TimerFactory' abstract class.
/// For this example, create and start an 'ntci::Interface'. Note that timers
/// may also be created from other objects; see the component documentation for
/// more details.
///
/// ntca::InterfaceConfig interfaceConfig;
/// interfaceConfig.setThreadName("example");
/// interfaceConfig.setMinThreads(1);
/// interfaceConfig.setMaxThreads(1);
///
/// bsl::shared_ptr<ntci::Interface> interface =
/// ntcf::System::createInterface(interfaceConfig);
///
/// interface->start();
///
/// Define the timer configuration for a one-shot timer.
///
/// ntca::TimerOptions timerOptions;
///
/// timerOptions.showEvent(ntca::TimerEventType::e_DEADLINE);
/// timerOptions.showEvent(ntca::TimerEventType::e_CANCELED);
/// timerOptions.showEvent(ntca::TimerEventType::e_CLOSED);
/// timerOptions.setOneShot(true);
///
/// Create a timer whose callback arrives at a timer future.
///
/// ntci::TimerFuture timerFuture;
///
/// bsl::shared_ptr<ntci::Timer> timer =
/// interface->createTimer(timerOptions, timerFuture);
///
/// Initially schedule the timer far into the future.
///
/// error = timer->schedule(bsls::TimeInterval(86400),
/// bsls::TimeInterval());
/// BSLS_ASSERT(!error);
///
/// Cancel the timer. Note that the timer is canceled instead of closed
/// because the timer is intended to be scheduled again; while closing a
/// timer implicitly cancels it the additional consequence is the timer
/// cannot be rescheduled.
///
/// error = timer->cancel();
/// if (error) {
/// BSLS_ASSERT(error == ntsa::Error(ntsa::Error::e_CANCELLED));
/// }
///
/// Wait for an arrival of the timer future.
///
/// ntci::TimerResult timerResult;
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer was canceled.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_CANCELED);
///
/// Schedule the timer 100ms from now.
///
/// bsls::TimeInterval duration;
/// duration.setTotalMilliseconds(100);
///
/// error = timer->schedule(timer->currentTime() + duration,
/// bsls::TimeInterval());
/// BSLS_ASSERT(!error);
///
/// Wait for an arrival of the timer future.
///
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer's deadline has
/// arrived.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_DEADLINE);
///
/// Note that the timer does not need to be explicitly closed since it
/// has been configured as a one-shot timer.
///
/// Wait for an arrival of the timer future.
///
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer was closed.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_CLOSED);
///
/// Finally, stop the interface.
///
/// interface->shutdown();
/// interface->linger();
///
///
/// @par Usage Example 8: Rescheduling a periodic timer after cancellation
/// This example illustrates how to reschedule a periodically-recurring timer
/// after it has been canceled.
///
/// First, initialize the library.
///
/// ntcf::System::initialize();
/// ntcf::System::ignore(ntscfg::Signal::e_PIPE);
///
/// Next, acquire an implementation of the 'ntci::TimerFactory' abstract class.
/// For this example, create and start an 'ntci::Interface'. Note that timers
/// may also be created from other objects; see the component documentation for
/// more details.
///
/// ntca::InterfaceConfig interfaceConfig;
/// interfaceConfig.setThreadName("example");
/// interfaceConfig.setMinThreads(1);
/// interfaceConfig.setMaxThreads(1);
///
/// bsl::shared_ptr<ntci::Interface> interface =
/// ntcf::System::createInterface(interfaceConfig);
///
/// interface->start();
///
/// Define the timer configuration for a multiple-shot timer.
///
/// ntca::TimerOptions timerOptions;
///
/// timerOptions.showEvent(ntca::TimerEventType::e_DEADLINE);
/// timerOptions.showEvent(ntca::TimerEventType::e_CANCELED);
/// timerOptions.showEvent(ntca::TimerEventType::e_CLOSED);
/// timerOptions.setOneShot(false);
///
/// Create a timer whose callback arrives at a timer future.
///
/// ntci::TimerFuture timerFuture;
///
/// bsl::shared_ptr<ntci::Timer> timer =
/// interface->createTimer(timerOptions, timerFuture);
///
/// Initially schedule the timer far into the future.
///
/// error = timer->schedule(bsls::TimeInterval(86400),
/// bsls::TimeInterval());
/// BSLS_ASSERT(!error);
///
/// Cancel the timer. Note that the timer is canceled instead of closed
/// because the timer is intended to be scheduled again; while closing a
/// timer implicitly cancels it the additional consequence is the timer
/// cannot be rescheduled.
///
/// error = timer->cancel();
/// if (error) {
/// BSLS_ASSERT(error == ntsa::Error(ntsa::Error::e_CANCELLED));
/// }
///
/// Wait for an arrival of the timer future.
///
/// ntci::TimerResult timerResult;
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer was canceled.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_CANCELED);
///
/// Reschedule the timer 100ms from now that recurs every 20ms.
///
/// bsls::TimeInterval duration;
/// duration.setTotalMilliseconds(100);
///
/// bsls::TimeInterval period;
/// period.setTotalMilliseconds(20);
///
/// error = timer->schedule(timer->currentTime() + duration, period);
/// BSLS_ASSERT(!error);
///
/// Wait for several arrivals of the timer future and ensure each the timer
/// future's result indicates the timer's deadline has arrived.
///
/// for (bsl::size_t i = 0; i < 5; ++i) {
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_DEADLINE);
/// }
///
/// Close the timer to stop it recurring.
///
/// timer->close();
///
/// Wait for an arrival of the timer future.
///
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer was canceled.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_CANCELED);
///
/// Wait for an arrival of the timer future.
///
/// error = timerFuture.wait(&timerResult);
/// BSLS_ASSERT(!error);
///
/// Ensure the timer future's result indicates the timer was closed.
///
/// BSLS_ASSERT(timerResult.event().type() ==
/// ntca::TimerEventType::e_CLOSED);
///
/// Finally, stop the interface.
///
/// interface->shutdown();
/// interface->linger();
///
/// @ingroup module_ntci_timer
class Timer
{
public:
/// Destroy this object.
virtual ~Timer();
public:
/// Set or update the deadline of this timer to the specified
/// 'deadline'. Return the error.
ntsa::Error schedule(const bsls::TimeInterval& deadline);
/// Set or update the deadline of this timer to the specified
/// 'deadline', which, if the total number of milliseconds in the
/// specified 'period' is greater than zero, automatically advances and
/// recurs according to that 'period'. Return the error.
virtual ntsa::Error schedule(const bsls::TimeInterval& deadline,
const bsls::TimeInterval& period) = 0;
/// Cancel this timer if its deadline has not already occurred, but
/// allow this timer to be rescheduled. Return the error, notably
/// 'ntca::Error::e_CANCELED' if another occurrence of this timer's
/// deadline is scheduled but has not yet occurred (in which case the
/// timer will be announced as cancelled instead),
/// 'ntsa::Error::e_INVALID' if the 'timer' has not been registered or
/// already removed, and 'ntsa::Error::e_OK' if this timer's deadline
/// has already occurred, is not recurring nor has been rescheduled.
virtual ntsa::Error cancel() = 0;
/// Cancel this timer if its deadline has not already occurred, close the
/// timer for subsequent scheduling, and remove its resources. Return
/// the error, notably 'ntca::Error::e_CANCELED' if another occurrence
/// of this timer's deadline is scheduled but has not yet occurred (in
/// which case the timer will be announced as cancelled instead),
/// 'ntsa::Error::e_INVALID' if the 'timer' has not been registered or
/// already removed, and 'ntsa::Error::e_OK' if this timer's deadline
/// has already occurred, is not recurring nor has been rescheduled.
virtual ntsa::Error close() = 0;
/// Announce the arrival of the last specified 'deadline' of this timer,
/// computed using the specified 'now', and, if the timer is in one-shot
/// mode, also announce its automatic closure.
virtual void arrive(const bsl::shared_ptr<ntci::Timer>& self,
const bsls::TimeInterval& now,
const bsls::TimeInterval& deadline) = 0;
/// Return the user-defined handle of this timer.
virtual void* handle() const = 0;
/// Return the user-defined identifier assigned to this timer.
virtual int id() const = 0;
/// Return true if this timer is in one-shot mode, otherwise return
/// false.
virtual bool oneShot() const = 0;
/// Return the deadline, or null if no deadline is scheduled.
virtual bdlb::NullableValue<bsls::TimeInterval> deadline() const = 0;
/// Return the period, or null if the timer is not periodic.
virtual bdlb::NullableValue<bsls::TimeInterval> period() const = 0;
/// Return the handle of the thread that manages this socket, or
/// the default value if no such thread has been set.
virtual bslmt::ThreadUtil::Handle threadHandle() const = 0;
/// Return the index in the thread pool of the thread that manages this
/// socket, or 0 if no such thread has been set.
virtual bsl::size_t threadIndex() const = 0;
/// Return the strand on which this object's functions should be called.
virtual const bsl::shared_ptr<ntci::Strand>& strand() const = 0;
/// Return the current elapsed time since the Unix epoch.
virtual bsls::TimeInterval currentTime() const = 0;
};
NTCCFG_INLINE
ntsa::Error Timer::schedule(const bsls::TimeInterval& deadline)
{
return this->schedule(deadline, bsls::TimeInterval());
}
} // close package namespace
} // close enterprise namespace
#endif