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