-
Notifications
You must be signed in to change notification settings - Fork 3
/
eventloop_timer_utilities.h
102 lines (90 loc) · 4.71 KB
/
eventloop_timer_utilities.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
/* Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License. */
#pragma once
#include <time.h>
#include <unistd.h>
#include <applibs/eventloop.h>
/// <summary>
/// Opaque handle. Obtain via <see cref="CreateEventLoopPeriodicTimer" />
/// or <see cref="CreateEventLoopDisarmedTimer" /> and dispose of via
/// <see cref="DisposeEventLoopTimer" />.
/// </summary>
typedef struct EventLoopTimer EventLoopTimer;
/// <summary>
/// Applications implement a function with this signature to be
/// notified when a timer expires.
/// </summary>
/// <param name="timer">The timer which has expired.</param>
/// <seealso cref="CreateEventLoopPeriodicTimer" />
/// <seealso cref="CreateEventLoopDisarmedTimer" />
typedef void (*EventLoopTimerHandler)(EventLoopTimer *timer);
/// <summary>
/// Create a periodic timer which is invoked on the event loop. The timer
/// will begin firing immediately.
/// </summary>
/// <param name="eventLoop">Event loop to which the timer will be added.</param>
/// <param name="handler">Callback to invoke when the timer expires.</param>
/// <param name="period">Timer period.</param>
/// <returns>On success, pointer to new EventLoopTimer, which should be disposed of
/// with <see cref="DisposeEventLoopTimer" />. On failure, returns NULL, with more
/// information available in errno.</returns>.
EventLoopTimer *CreateEventLoopPeriodicTimer(EventLoop *eventLoop, EventLoopTimerHandler handler,
const struct timespec *period);
/// <summary>
/// Create a disarmed timer. After the timer has been allocated, call
/// <see cref="SetEventLoopTimerPeriod" /> or <see cref="SetEventLoopTimerOneShot" />
/// to arm the timer.
/// </summary>
/// <param name="eventLoop">Event loop to which the timer will be added.</param>
/// <param name="handler">Callback to invoke when the timer expires.</param>
/// <returns>On success, pointer to new EventLoopTimer, which should be disposed of
/// with <see cref="DisposeEventLoopTimer" />. On failure, returns NULL, with more
/// information available in errno.</returns>.
EventLoopTimer *CreateEventLoopDisarmedTimer(EventLoop *eventLoop, EventLoopTimerHandler handler);
/// <summary>
/// Dispose of a timer which was allocated with <see cref="CreateEventLoopPeriodicTimer" />
/// or <see cref="CreateEventLoopDisarmedTimer" />.
/// It is safe to call this function with a NULL pointer.
/// </summary>
/// <param name="timer">Successfully allocated event loop timer, or NULL.</param>
void DisposeEventLoopTimer(EventLoopTimer *timer);
/// <summary>
/// The timer callback should call this function to consume the timer event.
/// </summary>
/// <param name="timer">Successfully allocated timer.</param>
/// <returns>0 on success, -1 on failure, in which case errno contains more information.</returns>
int ConsumeEventLoopTimerEvent(EventLoopTimer *timer);
/// <summary>
/// Change the timer's period. This function should only be called to change an existing
/// timer's period. It does not have to be called to set the initial period - that is
/// handled by <see cref="CreateEventLoopPeriodicTimer" />.
/// </summary>
/// <param name="timer">Timer previously allocated with <see cref="CreateEventLoopPeriodicTimer" />
/// or <see cref="CreateEventLoopDisarmedTimer" />.</param>
/// <param name="period">New timer period.</param>
/// <returns>0 on success, -1 on failure, in which case errno contains more information.</returns>
/// <seealso cref="SetEventLoopTimerOneShot" />
/// <seealso cref="DisarmEventLoopTimer" />
int SetEventLoopTimerPeriod(EventLoopTimer *timer, const struct timespec *period);
/// <summary>
/// Set the timer to expire one after a specified period.
/// </summary>
/// <returns>0 on succcess, -1 on failure, in which case errno contains more information.</returns>
/// <param name="timer">Timer previously allocated with <see cref="CreateEventLoopPeriodicTimer" />
/// or <see cref="CreateEventLoopDisarmedTimer" />.</param>
/// <param name="delay">Period to wait before timer expires.</param>
/// <returns>0 on success, -1 on failure, in which case errno contains more
/// information.</returns>
/// <seealso cref="SetEventLoopTimerPeriod" />
/// <seealso cref="DisarmEventLoopTimer" />
int SetEventLoopTimerOneShot(EventLoopTimer *timer, const struct timespec *delay);
/// <summary>
/// Disarm an existing event loop timer.
/// </summary>
/// <param name="timer">Timer previously allocated with <see cref="CreateEventLoopPeriodicTimer" />
/// or <see cref="CreateEventLoopDisarmedTimer" />.</param>
/// <returns>0 on success; -1 on failure, in which case errno contains more
/// information.</returns>
/// <seealso cref="SetEventLoopTimerOneShot" />
/// <seealso cref="SetEventLoopTimerPeriod" />
int DisarmEventLoopTimer(EventLoopTimer *timer);