1SD_EVENT_ADD_TIME(3) sd_event_add_time SD_EVENT_ADD_TIME(3)
2
3
4
6 sd_event_add_time, sd_event_source_get_time, sd_event_source_set_time,
7 sd_event_source_get_time_accuracy, sd_event_source_set_time_accuracy,
8 sd_event_source_get_time_clock, sd_event_time_handler_t - Add a timer
9 event source to an event loop
10
12 #include <systemd/sd-event.h>
13
14 typedef struct sd_event_source sd_event_source;
15
16 typedef int (*sd_event_time_handler_t)(sd_event_source *s,
17 uint64_t usec, void *userdata);
18
19 int sd_event_add_time(sd_event *event, sd_event_source **source,
20 clockid_t clock, uint64_t usec,
21 uint64_t accuracy,
22 sd_event_time_handler_t handler, void *userdata);
23
24 int sd_event_source_get_time(sd_event_source *source, uint64_t *usec);
25
26 int sd_event_source_set_time(sd_event_source *source, uint64_t usec);
27
28 int sd_event_source_get_time_accuracy(sd_event_source *source,
29 uint64_t *usec);
30
31 int sd_event_source_set_time_accuracy(sd_event_source *source,
32 uint64_t usec);
33
34 int sd_event_source_get_time_clock(sd_event_source *source,
35 clockid_t *clock);
36
38 sd_event_add_time() adds a new timer event source to an event loop. The
39 event loop object is specified in the event parameter, the event source
40 object is returned in the source parameter. The clock parameter takes a
41 clock identifier, one of CLOCK_REALTIME, CLOCK_MONOTONIC,
42 CLOCK_BOOTTIME, CLOCK_REALTIME_ALARM, or CLOCK_BOOTTIME_ALARM. See
43 timerfd_create(2) for details regarding the various types of clocks.
44 The usec parameter specifies the earliest time, in microseconds (µs),
45 relative to the clock's epoch, when the timer shall be triggered. If a
46 time already in the past is specified (including 0), this timer source
47 "fires" immediately and is ready to be dispatched. If the parameter is
48 specified as UINT64_MAX the timer event will never elapse, which may be
49 used as an alternative to explicitly disabling a timer event source
50 with sd_event_source_set_enabled(3). The accuracy parameter specifies
51 an additional accuracy value in µs specifying how much the timer event
52 may be delayed. Use 0 to select the default accuracy (250ms). Use 1µs
53 for maximum accuracy. Consider specifying 60000000µs (1min) or larger
54 for long-running events that may be delayed substantially. Picking
55 higher accuracy values allows the system to coalesce timer events more
56 aggressively, improving power efficiency. The handler parameter shall
57 reference a function to call when the timer elapses. The handler
58 function will be passed the userdata pointer, which may be chosen
59 freely by the caller. The handler is also passed the configured trigger
60 time, even if it is actually called slightly later, subject to the
61 specified accuracy value, the kernel timer slack (see prctl(2)), and
62 additional scheduling latencies. To query the actual time the handler
63 was called use sd_event_now(3).
64
65 By default, the timer will elapse once (SD_EVENT_ONESHOT), but this may
66 be changed with sd_event_source_set_enabled(3). If the handler function
67 returns a negative error code, it will be disabled after the
68 invocation, even if the SD_EVENT_ON mode was requested before. Note
69 that a timer event set to SD_EVENT_ON will fire continuously unless its
70 configured time is updated using sd_event_source_set_time().
71
72 To destroy an event source object use sd_event_source_unref(3), but
73 note that the event source is only removed from the event loop when all
74 references to the event source are dropped. To make sure an event
75 source does not fire anymore, even if it is still referenced, disable
76 the event source using sd_event_source_set_enabled(3) with
77 SD_EVENT_OFF.
78
79 If the second parameter of sd_event_add_time() is NULL no reference to
80 the event source object is returned. In this case the event source is
81 considered "floating", and will be destroyed implicitly when the event
82 loop itself is destroyed.
83
84 If the handler to sd_event_add_time() is NULL, and the event source
85 fires, this will be considered a request to exit the event loop. In
86 this case, the userdata parameter, cast to an integer, is used for the
87 exit code passed to sd_event_exit(3).
88
89 Use CLOCK_BOOTTIME_ALARM and CLOCK_REALTIME_ALARM to define event
90 sources that may wake up the system from suspend.
91
92 In order to set up relative timers (that is, relative to the current
93 time), retrieve the current time via sd_event_now(3), add the desired
94 timespan to it, and use the result as the usec parameter to
95 sd_event_add_time().
96
97 In order to set up repetitive timers (that is, timers that are
98 triggered in regular intervals), set up the timer normally, for the
99 first invocation. Each time the event handler is invoked, update the
100 timer's trigger time with sd_event_source_set_time(3) for the next
101 timer iteration, and reenable the timer using
102 sd_event_source_set_enabled(). To calculate the next point in time to
103 pass to sd_event_source_set_time(), either use as base the usec
104 parameter passed to the timer callback, or the timestamp returned by
105 sd_event_now(). In the former case timer events will be regular, while
106 in the latter case the scheduling latency will keep accumulating on the
107 timer.
108
109 sd_event_source_get_time() retrieves the configured time value of an
110 event source created previously with sd_event_add_time(). It takes the
111 event source object and a pointer to a variable to store the time in,
112 relative to the selected clock's epoch, in µs.
113
114 sd_event_source_set_time() changes the time of an event source created
115 previously with sd_event_add_time(). It takes the event source object
116 and a time relative to the selected clock's epoch, in µs.
117
118 sd_event_source_get_time_accuracy() retrieves the configured accuracy
119 value of an event source created previously with sd_event_add_time().
120 It takes the event source object and a pointer to a variable to store
121 the accuracy in. The accuracy is specified in µs.
122
123 sd_event_source_set_time_accuracy() changes the configured accuracy of
124 a timer event source created previously with sd_event_add_time(). It
125 takes the event source object and accuracy, in µs.
126
127 sd_event_source_get_time_clock() retrieves the configured clock of an
128 event source created previously with sd_event_add_time(). It takes the
129 event source object and a pointer to a variable to store the clock
130 identifier in.
131
133 On success, these functions return 0 or a positive integer. On failure,
134 they return a negative errno-style error code.
135
136 Errors
137 Returned values may indicate the following problems:
138
139 -ENOMEM
140 Not enough memory to allocate an object.
141
142 -EINVAL
143 An invalid argument has been passed.
144
145 -ESTALE
146 The event loop is already terminated.
147
148 -ECHILD
149 The event loop has been created in a different process.
150
151 -EOPNOTSUPP
152 The selected clock is not supported by the event loop
153 implementation.
154
155 -EDOM
156 The passed event source is not a timer event source.
157
159 These APIs are implemented as a shared library, which can be compiled
160 and linked to with the libsystemd pkg-config(1) file.
161
163 systemd(1), sd-event(3), sd_event_new(3), sd_event_now(3),
164 sd_event_add_io(3), sd_event_add_signal(3), sd_event_add_child(3),
165 sd_event_add_inotify(3), sd_event_add_defer(3),
166 sd_event_source_set_enabled(3), sd_event_source_set_priority(3),
167 sd_event_source_set_userdata(3), sd_event_source_set_description(3),
168 sd_event_source_set_floating(3), clock_gettime(2), timerfd_create(2),
169 prctl(2)
170
171
172
173systemd 245 SD_EVENT_ADD_TIME(3)