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. The handler parameter shall
68 reference a function to call when the timer elapses. The handler
69 function will be passed the userdata pointer, which may be chosen
70 freely by the caller. The handler is also passed the configured trigger
71 time, even if it is actually called slightly later, subject to the
72 specified accuracy value, the kernel timer slack (see prctl(2)), and
73 additional scheduling latencies. To query the actual time the handler
74 was called use sd_event_now(3).
75
76 By default, the timer will elapse once (SD_EVENT_ONESHOT), but this may
77 be changed with sd_event_source_set_enabled(3). If the handler function
78 returns a negative error code, it will be disabled after the
79 invocation, even if the SD_EVENT_ON mode was requested before. Note
80 that a timer event set to SD_EVENT_ON will fire continuously unless its
81 configured time is updated using sd_event_source_set_time().
82
83 sd_event_add_time_relative() is like sd_event_add_time(), but takes a
84 relative time specification. It's relative to the current time of the
85 event loop iteration, as returned by sd_event_now(3).
86
87 To destroy an event source object use sd_event_source_unref(3), but
88 note that the event source is only removed from the event loop when all
89 references to the event source are dropped. To make sure an event
90 source does not fire anymore, even if it is still referenced, disable
91 the event source using sd_event_source_set_enabled(3) with
92 SD_EVENT_OFF.
93
94 If the second parameter of sd_event_add_time() is NULL no reference to
95 the event source object is returned. In this case the event source is
96 considered "floating", and will be destroyed implicitly when the event
97 loop itself is destroyed.
98
99 If the handler parameter to sd_event_add_time() is NULL, and the event
100 source fires, this will be considered a request to exit the event loop.
101 In this case, the userdata parameter, cast to an integer, is passed as
102 the exit code parameter to sd_event_exit(3).
103
104 Use CLOCK_BOOTTIME_ALARM and CLOCK_REALTIME_ALARM to define event
105 sources that may wake up the system from suspend.
106
107 In order to set up relative timers (that is, relative to the current
108 time), retrieve the current time via sd_event_now(3), add the desired
109 timespan to it, and use the result as the usec parameter to
110 sd_event_add_time().
111
112 In order to set up repetitive timers (that is, timers that are
113 triggered in regular intervals), set up the timer normally, for the
114 first invocation. Each time the event handler is invoked, update the
115 timer's trigger time with sd_event_source_set_time(3) for the next
116 timer iteration, and reenable the timer using
117 sd_event_source_set_enabled(). To calculate the next point in time to
118 pass to sd_event_source_set_time(), either use as base the usec
119 parameter passed to the timer callback, or the timestamp returned by
120 sd_event_now(). In the former case timer events will be regular, while
121 in the latter case the scheduling latency will keep accumulating on the
122 timer.
123
124 sd_event_source_get_time() retrieves the configured time value of an
125 event source created previously with sd_event_add_time() or
126 sd_event_add_time_relative(). It takes the event source object and a
127 pointer to a variable to store the time in, relative to the selected
128 clock's epoch, in µs. The returned value is relative to the epoch, even
129 if the event source was created with a relative time via
130 sd_event_add_time_relative().
131
132 sd_event_source_set_time() changes the time of an event source created
133 previously with sd_event_add_time() or sd_event_add_time_relative(). It
134 takes the event source object and a time relative to the selected
135 clock's epoch, in µs.
136
137 sd_event_source_set_time_relative() is similar to
138 sd_event_source_set_time(), but takes a time relative to the current
139 time of the event loop iteration, as returned by sd_event_now().
140
141 sd_event_source_get_time_accuracy() retrieves the configured accuracy
142 value of an event source created previously with sd_event_add_time().
143 It takes the event source object and a pointer to a variable to store
144 the accuracy in. The accuracy is specified in µs.
145
146 sd_event_source_set_time_accuracy() changes the configured accuracy of
147 a timer event source created previously with sd_event_add_time(). It
148 takes the event source object and accuracy, in µs.
149
150 sd_event_source_get_time_clock() retrieves the configured clock of an
151 event source created previously with sd_event_add_time(). It takes the
152 event source object and a pointer to a variable to store the clock
153 identifier in.
154
156 On success, these functions return 0 or a positive integer. On failure,
157 they return a negative errno-style error code.
158
159 Errors
160 Returned values may indicate the following problems:
161
162 -ENOMEM
163 Not enough memory to allocate an object.
164
165 -EINVAL
166 An invalid argument has been passed.
167
168 -ESTALE
169 The event loop is already terminated.
170
171 -ECHILD
172 The event loop has been created in a different process.
173
174 -EOPNOTSUPP
175 The selected clock is not supported by the event loop
176 implementation.
177
178 -EDOM
179 The passed event source is not a timer event source.
180
181 -EOVERFLOW
182 The passed relative time is outside of the allowed range for time
183 values (i.e. the specified value added to the current time is
184 outside the 64 bit unsigned integer range).
185
187 These APIs are implemented as a shared library, which can be compiled
188 and linked to with the libsystemd pkg-config(1) file.
189
191 systemd(1), sd-event(3), sd_event_new(3), sd_event_now(3),
192 sd_event_add_io(3), sd_event_add_signal(3), sd_event_add_child(3),
193 sd_event_add_inotify(3), sd_event_add_defer(3),
194 sd_event_source_set_enabled(3), sd_event_source_set_priority(3),
195 sd_event_source_set_userdata(3), sd_event_source_set_description(3),
196 sd_event_source_set_floating(3), clock_gettime(2), timerfd_create(2),
197 prctl(2)
198
199
200
201systemd 248 SD_EVENT_ADD_TIME(3)