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