]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/sd_event_add_time.xml
Merge pull request #17185 from yuwata/ethtool-update
[thirdparty/systemd.git] / man / sd_event_add_time.xml
1 <?xml version='1.0'?>
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1+ -->
5
6 <refentry id="sd_event_add_time" xmlns:xi="http://www.w3.org/2001/XInclude">
7
8 <refentryinfo>
9 <title>sd_event_add_time</title>
10 <productname>systemd</productname>
11 </refentryinfo>
12
13 <refmeta>
14 <refentrytitle>sd_event_add_time</refentrytitle>
15 <manvolnum>3</manvolnum>
16 </refmeta>
17
18 <refnamediv>
19 <refname>sd_event_add_time</refname>
20 <refname>sd_event_source_get_time</refname>
21 <refname>sd_event_source_set_time</refname>
22 <refname>sd_event_source_get_time_accuracy</refname>
23 <refname>sd_event_source_set_time_accuracy</refname>
24 <refname>sd_event_source_get_time_clock</refname>
25 <refname>sd_event_time_handler_t</refname>
26
27 <refpurpose>Add a timer event source to an event loop</refpurpose>
28 </refnamediv>
29
30 <refsynopsisdiv>
31 <funcsynopsis>
32 <funcsynopsisinfo>#include &lt;systemd/sd-event.h&gt;</funcsynopsisinfo>
33
34 <funcsynopsisinfo><token>typedef</token> struct sd_event_source sd_event_source;</funcsynopsisinfo>
35
36 <funcprototype>
37 <funcdef>typedef int (*<function>sd_event_time_handler_t</function>)</funcdef>
38 <paramdef>sd_event_source *<parameter>s</parameter></paramdef>
39 <paramdef>uint64_t <parameter>usec</parameter></paramdef>
40 <paramdef>void *<parameter>userdata</parameter></paramdef>
41 </funcprototype>
42
43 <funcprototype>
44 <funcdef>int <function>sd_event_add_time</function></funcdef>
45 <paramdef>sd_event *<parameter>event</parameter></paramdef>
46 <paramdef>sd_event_source **<parameter>source</parameter></paramdef>
47 <paramdef>clockid_t <parameter>clock</parameter></paramdef>
48 <paramdef>uint64_t <parameter>usec</parameter></paramdef>
49 <paramdef>uint64_t <parameter>accuracy</parameter></paramdef>
50 <paramdef>sd_event_time_handler_t <parameter>handler</parameter></paramdef>
51 <paramdef>void *<parameter>userdata</parameter></paramdef>
52 </funcprototype>
53
54 <funcprototype>
55 <funcdef>int <function>sd_event_add_time_relative</function></funcdef>
56 <paramdef>sd_event *<parameter>event</parameter></paramdef>
57 <paramdef>sd_event_source **<parameter>source</parameter></paramdef>
58 <paramdef>clockid_t <parameter>clock</parameter></paramdef>
59 <paramdef>uint64_t <parameter>usec</parameter></paramdef>
60 <paramdef>uint64_t <parameter>accuracy</parameter></paramdef>
61 <paramdef>sd_event_time_handler_t <parameter>handler</parameter></paramdef>
62 <paramdef>void *<parameter>userdata</parameter></paramdef>
63 </funcprototype>
64
65 <funcprototype>
66 <funcdef>int <function>sd_event_source_get_time</function></funcdef>
67 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
68 <paramdef>uint64_t *<parameter>usec</parameter></paramdef>
69 </funcprototype>
70
71 <funcprototype>
72 <funcdef>int <function>sd_event_source_set_time</function></funcdef>
73 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
74 <paramdef>uint64_t <parameter>usec</parameter></paramdef>
75 </funcprototype>
76
77 <funcprototype>
78 <funcdef>int <function>sd_event_source_set_time_relative</function></funcdef>
79 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
80 <paramdef>uint64_t <parameter>usec</parameter></paramdef>
81 </funcprototype>
82
83 <funcprototype>
84 <funcdef>int <function>sd_event_source_get_time_accuracy</function></funcdef>
85 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
86 <paramdef>uint64_t *<parameter>usec</parameter></paramdef>
87 </funcprototype>
88
89 <funcprototype>
90 <funcdef>int <function>sd_event_source_set_time_accuracy</function></funcdef>
91 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
92 <paramdef>uint64_t <parameter>usec</parameter></paramdef>
93 </funcprototype>
94
95 <funcprototype>
96 <funcdef>int <function>sd_event_source_get_time_clock</function></funcdef>
97 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
98 <paramdef>clockid_t *<parameter>clock</parameter></paramdef>
99 </funcprototype>
100
101 </funcsynopsis>
102 </refsynopsisdiv>
103
104 <refsect1>
105 <title>Description</title>
106
107 <para><function>sd_event_add_time()</function> adds a new timer event source to an event loop. The event loop
108 object is specified in the <parameter>event</parameter> parameter, the event source object is returned in the
109 <parameter>source</parameter> parameter. The <parameter>clock</parameter> parameter takes a clock identifier, one
110 of <constant>CLOCK_REALTIME</constant>, <constant>CLOCK_MONOTONIC</constant>, <constant>CLOCK_BOOTTIME</constant>,
111 <constant>CLOCK_REALTIME_ALARM</constant>, or <constant>CLOCK_BOOTTIME_ALARM</constant>. See
112 <citerefentry><refentrytitle>timerfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry> for details
113 regarding the various types of clocks. The <parameter>usec</parameter> parameter specifies the earliest time, in
114 microseconds (µs), relative to the clock's epoch, when the timer shall be triggered. If a time already in the past
115 is specified (including <constant>0</constant>), this timer source "fires" immediately and is ready to be
116 dispatched. If the parameter is specified as <constant>UINT64_MAX</constant> the timer event will never elapse,
117 which may be used as an alternative to explicitly disabling a timer event source with
118 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>. The
119 <parameter>accuracy</parameter> parameter specifies an additional accuracy value in µs specifying how much the
120 timer event may be delayed. Use <constant>0</constant> to select the default accuracy (250ms). Use 1µs for maximum
121 accuracy. Consider specifying 60000000µs (1min) or larger for long-running events that may be delayed
122 substantially. Picking higher accuracy values allows the system to coalesce timer events more aggressively,
123 improving power efficiency. The <parameter>handler</parameter> parameter shall reference a function to call when
124 the timer elapses. The handler function will be passed the <parameter>userdata</parameter> pointer, which may be
125 chosen freely by the caller. The handler is also passed the configured trigger time, even if it is actually called
126 slightly later, subject to the specified accuracy value, the kernel timer slack (see
127 <citerefentry><refentrytitle>prctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>), and additional
128 scheduling latencies. To query the actual time the handler was called use
129 <citerefentry><refentrytitle>sd_event_now</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
130
131 <para>By default, the timer will elapse once
132 (<constant>SD_EVENT_ONESHOT</constant>), but this may be changed
133 with
134 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
135 If the handler function returns a negative error code, it will be
136 disabled after the invocation, even if the
137 <constant>SD_EVENT_ON</constant> mode was requested before. Note
138 that a timer event set to <constant>SD_EVENT_ON</constant> will
139 fire continuously unless its configured time is updated using
140 <function>sd_event_source_set_time()</function>.
141 </para>
142
143 <para><function>sd_event_add_time_relative()</function> is like <function>sd_event_add_time()</function>,
144 but takes a relative time specification. It's relative to the current time of the event loop iteration,
145 as returned by
146 <citerefentry><refentrytitle>sd_event_now</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
147
148 <para>To destroy an event source object use
149 <citerefentry><refentrytitle>sd_event_source_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
150 but note that the event source is only removed from the event loop
151 when all references to the event source are dropped. To make sure
152 an event source does not fire anymore, even if it is still referenced,
153 disable the event source using
154 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
155 with <constant>SD_EVENT_OFF</constant>.</para>
156
157 <para>If the second parameter of
158 <function>sd_event_add_time()</function> is
159 <constant>NULL</constant> no reference to the event source object
160 is returned. In this case the event source is considered
161 "floating", and will be destroyed implicitly when the event loop
162 itself is destroyed.</para>
163
164 <para>If the <parameter>handler</parameter> parameter to <function>sd_event_add_time()</function> is
165 <constant>NULL</constant>, and the event source fires, this will be considered a request to exit the
166 event loop. In this case, the <parameter>userdata</parameter> parameter, cast to an integer, is passed as
167 the exit code parameter to
168 <citerefentry><refentrytitle>sd_event_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
169
170 <para>Use <constant>CLOCK_BOOTTIME_ALARM</constant> and
171 <constant>CLOCK_REALTIME_ALARM</constant> to define event sources
172 that may wake up the system from suspend.</para>
173
174 <para>In order to set up relative timers (that is, relative to the
175 current time), retrieve the current time via
176 <citerefentry><refentrytitle>sd_event_now</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
177 add the desired timespan to it, and use the result as
178 the <parameter>usec</parameter> parameter to
179 <function>sd_event_add_time()</function>.</para>
180
181 <para>In order to set up repetitive timers (that is, timers that
182 are triggered in regular intervals), set up the timer normally,
183 for the first invocation. Each time the event handler is invoked,
184 update the timer's trigger time with
185 <citerefentry><refentrytitle>sd_event_source_set_time</refentrytitle><manvolnum>3</manvolnum></citerefentry> for the next timer
186 iteration, and reenable the timer using
187 <function>sd_event_source_set_enabled()</function>. To calculate
188 the next point in time to pass to
189 <function>sd_event_source_set_time()</function>, either use as
190 base the <parameter>usec</parameter> parameter passed to the timer
191 callback, or the timestamp returned by
192 <function>sd_event_now()</function>. In the former case timer
193 events will be regular, while in the latter case the scheduling
194 latency will keep accumulating on the timer.</para>
195
196 <para><function>sd_event_source_get_time()</function> retrieves the configured time value of an event
197 source created previously with <function>sd_event_add_time()</function> or
198 <function>sd_event_add_time_relative()</function>. It takes the event source object and a pointer to a
199 variable to store the time in, relative to the selected clock's epoch, in µs. The returned value is
200 relative to the epoch, even if the event source was created with a relative time via
201 <function>sd_event_add_time_relative()</function>.</para>
202
203 <para><function>sd_event_source_set_time()</function> changes the time of an event source created
204 previously with <function>sd_event_add_time()</function> or
205 <function>sd_event_add_time_relative()</function>. It takes the event source object and a time relative
206 to the selected clock's epoch, in µs.</para>
207
208 <para><function>sd_event_source_set_time_relative()</function> is similar to
209 <function>sd_event_source_set_time()</function>, but takes a time relative to the current time of the
210 event loop iteration, as returned by <function>sd_event_now()</function>.</para>
211
212 <para><function>sd_event_source_get_time_accuracy()</function>
213 retrieves the configured accuracy value of an event source
214 created previously with <function>sd_event_add_time()</function>. It
215 takes the event source object and a pointer to a variable to store
216 the accuracy in. The accuracy is specified in µs.</para>
217
218 <para><function>sd_event_source_set_time_accuracy()</function>
219 changes the configured accuracy of a timer event source created
220 previously with <function>sd_event_add_time()</function>. It takes
221 the event source object and accuracy, in µs.</para>
222
223 <para><function>sd_event_source_get_time_clock()</function>
224 retrieves the configured clock of an event source created
225 previously with <function>sd_event_add_time()</function>. It takes
226 the event source object and a pointer to a variable to store the
227 clock identifier in.</para>
228 </refsect1>
229
230 <refsect1>
231 <title>Return Value</title>
232
233 <para>On success, these functions return 0 or a positive
234 integer. On failure, they return a negative errno-style error
235 code. </para>
236
237 <refsect2>
238 <title>Errors</title>
239
240 <para>Returned values may indicate the following problems:</para>
241
242 <variablelist>
243 <varlistentry>
244 <term><constant>-ENOMEM</constant></term>
245
246 <listitem><para>Not enough memory to allocate an object.</para></listitem>
247 </varlistentry>
248
249 <varlistentry>
250 <term><constant>-EINVAL</constant></term>
251
252 <listitem><para>An invalid argument has been passed.</para></listitem>
253
254 </varlistentry>
255
256 <varlistentry>
257 <term><constant>-ESTALE</constant></term>
258
259 <listitem><para>The event loop is already terminated.</para></listitem>
260
261 </varlistentry>
262
263 <varlistentry>
264 <term><constant>-ECHILD</constant></term>
265
266 <listitem><para>The event loop has been created in a different process.</para></listitem>
267
268 </varlistentry>
269
270 <varlistentry>
271 <term><constant>-EOPNOTSUPP</constant></term>
272
273 <listitem><para>The selected clock is not supported by the event loop implementation.</para></listitem>
274
275 </varlistentry>
276
277 <varlistentry>
278 <term><constant>-EDOM</constant></term>
279
280 <listitem><para>The passed event source is not a timer event source.</para></listitem>
281 </varlistentry>
282
283 <varlistentry>
284 <term><constant>-EOVERFLOW</constant></term>
285
286 <listitem><para>The passed relative time is outside of the allowed range for time values (i.e. the
287 specified value added to the current time is outside the 64 bit unsigned integer range).</para></listitem>
288 </varlistentry>
289 </variablelist>
290 </refsect2>
291 </refsect1>
292
293 <xi:include href="libsystemd-pkgconfig.xml" />
294
295 <refsect1>
296 <title>See Also</title>
297
298 <para>
299 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
300 <citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
301 <citerefentry><refentrytitle>sd_event_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
302 <citerefentry><refentrytitle>sd_event_now</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
303 <citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
304 <citerefentry><refentrytitle>sd_event_add_signal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
305 <citerefentry><refentrytitle>sd_event_add_child</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
306 <citerefentry><refentrytitle>sd_event_add_inotify</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
307 <citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
308 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
309 <citerefentry><refentrytitle>sd_event_source_set_priority</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
310 <citerefentry><refentrytitle>sd_event_source_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
311 <citerefentry><refentrytitle>sd_event_source_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
312 <citerefentry><refentrytitle>sd_event_source_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
313 <citerefentry project='man-pages'><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
314 <citerefentry project='man-pages'><refentrytitle>timerfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
315 <citerefentry project='man-pages'><refentrytitle>prctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>
316 </para>
317 </refsect1>
318
319 </refentry>