1 <?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 SPDX-License-Identifier: LGPL-2.1+
9 <refentry id="sd_event_wait" xmlns:xi="http://www.w3.org/2001/XInclude">
12 <title>sd_event_wait</title>
13 <productname>systemd</productname>
17 <contrib>Developer</contrib>
18 <firstname>Tom</firstname>
19 <surname>Gundersen</surname>
20 <email>teg@jklm.no</email>
26 <refentrytitle>sd_event_wait</refentrytitle>
27 <manvolnum>3</manvolnum>
31 <refname>sd_event_wait</refname>
32 <refname>sd_event_prepare</refname>
33 <refname>sd_event_dispatch</refname>
34 <refname>sd_event_get_state</refname>
35 <refname>sd_event_get_iteration</refname>
36 <refname>SD_EVENT_INITIAL</refname>
37 <refname>SD_EVENT_PREPARING</refname>
38 <refname>SD_EVENT_ARMED</refname>
39 <refname>SD_EVENT_PENDING</refname>
40 <refname>SD_EVENT_RUNNING</refname>
41 <refname>SD_EVENT_EXITING</refname>
42 <refname>SD_EVENT_FINISHED</refname>
44 <refpurpose>Low-level event loop operations</refpurpose>
49 <funcsynopsisinfo>#include <systemd/sd-event.h></funcsynopsisinfo>
51 <funcsynopsisinfo><token>enum</token> {
52 <constant>SD_EVENT_INITIAL</constant>,
53 <constant>SD_EVENT_PREPARING</constant>,
54 <constant>SD_EVENT_ARMED</constant>,
55 <constant>SD_EVENT_PENDING</constant>,
56 <constant>SD_EVENT_RUNNING</constant>,
57 <constant>SD_EVENT_EXITING</constant>,
58 <constant>SD_EVENT_FINISHED</constant>,
62 <funcdef>int <function>sd_event_prepare</function></funcdef>
63 <paramdef>sd_event *<parameter>event</parameter></paramdef>
67 <funcdef>int <function>sd_event_wait</function></funcdef>
68 <paramdef>sd_event *<parameter>event</parameter></paramdef>
69 <paramdef>uint64_t <parameter>usec</parameter></paramdef>
73 <funcdef>int <function>sd_event_dispatch</function></funcdef>
74 <paramdef>sd_event *<parameter>event</parameter></paramdef>
78 <funcdef>int <function>sd_event_get_state</function></funcdef>
79 <paramdef>sd_event *<parameter>event</parameter></paramdef>
83 <funcdef>int <function>sd_event_get_iteration</function></funcdef>
84 <paramdef>sd_event *<parameter>event</parameter></paramdef>
85 <paramdef>uint64_t *<parameter>ret</parameter></paramdef>
92 <title>Description</title>
94 <para>The low-level <function>sd_event_prepare()</function>,
95 <function>sd_event_wait()</function> and
96 <function>sd_event_dispatch()</function> functions may be used to
97 execute specific phases of an event loop. See
98 <citerefentry><refentrytitle>sd_event_run</refentrytitle><manvolnum>3</manvolnum></citerefentry>
100 <citerefentry><refentrytitle>sd_event_loop</refentrytitle><manvolnum>3</manvolnum></citerefentry>
101 for higher-level functions that execute individual but complete
102 iterations of an event loop or run it continuously.</para>
104 <para><function>sd_event_prepare()</function> checks for pending
105 events and arms necessary timers. If any events are ready to be
106 processed ("pending"), it returns a positive, non-zero value, and the caller
107 should process these events with
108 <function>sd_event_dispatch()</function>.</para>
110 <para><function>sd_event_dispatch()</function> dispatches the
111 highest priority event source that has a pending event. On
112 success, <function>sd_event_dispatch()</function> returns either
113 zero, which indicates that no further event sources may be
114 dispatched and exiting of the event loop was requested via
115 <citerefentry><refentrytitle>sd_event_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry>;
116 or a positive non-zero value, which means that an event source was
117 dispatched and the loop returned to its initial state, and the
118 caller should initiate the next event loop iteration by invoking
119 <function>sd_event_prepare()</function> again.</para>
121 <para>In case <function>sd_event_prepare()</function> returned
122 zero, <function>sd_event_wait()</function> should be called to
123 wait for further events or a timeout. If any events are ready to
124 be processed, it returns a positive, non-zero value, and the
125 events should be dispatched with
126 <function>sd_event_dispatch()</function>. Otherwise, the event
127 loop returned to its initial state and the next event loop
128 iteration should be initiated by invoking
129 <function>sd_event_prepare()</function> again.</para>
131 <para><function>sd_event_get_state()</function> may be used to
132 determine the state the event loop is currently in. It returns one
133 of the states described below.</para>
135 <para><function>sd_event_get_iteration()</function> may be used to determine the current iteration of the event
136 loop. It returns an unsigned 64bit integer containing a counter that increases monotonically with each iteration of
137 the event loop, starting with 0. The counter is increased at the time of the
138 <function>sd_event_prepare()</function> invocation.</para>
140 <para>All five functions take, as the first argument, the event loop object <parameter>event</parameter> that has
141 been created with <function>sd_event_new()</function>. The timeout for <function>sd_event_wait()</function> is
142 specified in <parameter>usec</parameter> in microseconds. <constant>(uint64_t) -1</constant> may be used to
143 specify an infinite timeout.</para>
147 <title>State Machine</title>
149 <para>The event loop knows the following states, that may be
150 queried with <function>sd_event_get_state()</function>.</para>
154 <term><constant>SD_EVENT_INITIAL</constant></term>
156 <listitem><para>The initial state the event loop is in,
157 before each event loop iteration. Use
158 <function>sd_event_prepare()</function> to transition the
159 event loop into the <constant>SD_EVENT_ARMED</constant> or
160 <constant>SD_EVENT_PENDING</constant> states.</para></listitem>
164 <term><constant>SD_EVENT_PREPARING</constant></term>
166 <listitem><para>An event source is currently being prepared,
167 i.e. the preparation handler is currently being executed, as
169 <citerefentry><refentrytitle>sd_event_set_prepare</refentrytitle><manvolnum>3</manvolnum></citerefentry>. This
170 state is only seen in the event source preparation handler
171 that is invoked from the
172 <function>sd_event_prepare()</function> call and is
173 immediately followed by <constant>SD_EVENT_ARMED</constant> or
174 <constant>SD_EVENT_PENDING</constant>.</para></listitem>
178 <term><constant>SD_EVENT_ARMED</constant></term>
180 <listitem><para><function>sd_event_prepare()</function> has
181 been called and no event sources were ready to be
182 dispatched. Use <function>sd_event_wait()</function> to wait
183 for new events, and transition into
184 <constant>SD_EVENT_PENDING</constant> or back into
185 <constant>SD_EVENT_INITIAL</constant>.</para></listitem>
189 <term><constant>SD_EVENT_PENDING</constant></term>
191 <listitem><para><function>sd_event_prepare()</function> or
192 <function>sd_event_wait()</function> have been called and
193 there were event sources with events pending. Use
194 <function>sd_event_dispatch()</function> to dispatch the
195 highest priority event source and transition back to
196 <constant>SD_EVENT_INITIAL</constant>, or
197 <constant>SD_EVENT_FINISHED</constant>.</para></listitem>
201 <term><constant>SD_EVENT_RUNNING</constant></term>
203 <listitem><para>A regular event source is currently being
204 dispatched. This state is only seen in the event source
205 handler that is invoked from the
206 <function>sd_event_dispatch()</function> call, and is
207 immediately followed by <constant>SD_EVENT_INITIAL</constant>
208 or <constant>SD_EVENT_FINISHED</constant> as soon the event
209 source handler returns. Note that during dispatching of exit
210 event sources the <constant>SD_EVENT_EXITING</constant> state
211 is seen instead.</para></listitem>
215 <term><constant>SD_EVENT_EXITING</constant></term>
217 <listitem><para>Similar to
218 <constant>SD_EVENT_RUNNING</constant> but is the state in
219 effect while dispatching exit event sources. It is followed by
220 <constant>SD_EVENT_INITIAL</constant> or
221 <constant>SD_EVENT_FINISHED</constant> as soon as the event
222 handler returns.</para></listitem>
226 <term><constant>SD_EVENT_FINISHED</constant></term>
228 <listitem><para>The event loop has exited. All exit event
229 sources have run. If the event loop is in this state it serves
230 no purpose anymore, and should be freed.</para></listitem>
235 <para>A simplified flow chart of the states and the calls to
236 transition between them is shown below. Note that
237 <constant>SD_EVENT_PREPARING</constant>,
238 <constant>SD_EVENT_RUNNING</constant> and
239 <constant>SD_EVENT_EXITING</constant> are not shown here.</para>
242 INITIAL -<---<---<---<---<---<---<---<---<---<---<---<---\
247 sd_event_prepare() >--->--->--->--->- ARMED |
252 PENDING <---<---<---<---<---< sd_event_wait() >--->--->--+
257 sd_event_dispatch() >--->--->--->--->--->--->--->--->--->--->/
267 <title>Return Value</title>
269 <para>On success, these functions return 0 or a positive integer.
270 On failure, they return a negative errno-style error code. In case
271 of <function>sd_event_prepare()</function> and
272 <function>sd_event_wait()</function>, a positive, non-zero return
273 code indicates that events are ready to be processed and zero
274 indicates that no events are ready. In case of
275 <function>sd_event_dispatch()</function>, a positive, non-zero
276 return code indicates that the event loop returned to its initial
277 state and zero indicates the event loop has
278 exited. <function>sd_event_get_state()</function> returns a
279 positive or zero state on success.</para>
283 <title>Errors</title>
285 <para>Returned errors may indicate the following problems:</para>
289 <term><constant>-EINVAL</constant></term>
291 <listitem><para>The <parameter>event</parameter> parameter is
292 invalid or <constant>NULL</constant>.</para></listitem>
296 <term><constant>-EBUSY</constant></term>
298 <listitem><para>The event loop object is not in the right
299 state.</para></listitem>
303 <term><constant>-ESTALE</constant></term>
305 <listitem><para>The event loop is already terminated.</para></listitem>
310 <term><constant>-ECHILD</constant></term>
312 <listitem><para>The event loop has been created in a different process.</para></listitem>
318 <para>Other errors are possible, too.</para>
321 <xi:include href="libelogind-pkgconfig.xml" />
324 <title>See Also</title>
327 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
328 <citerefentry><refentrytitle>sd_event_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
329 <citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
330 <citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
331 <citerefentry><refentrytitle>sd_event_add_signal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
332 <citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
333 <citerefentry><refentrytitle>sd_event_add_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
334 <citerefentry><refentrytitle>sd_event_add_post</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
335 <citerefentry><refentrytitle>sd_event_run</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
336 <citerefentry><refentrytitle>sd_event_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
337 <citerefentry><refentrytitle>sd_event_source_set_prepare</refentrytitle><manvolnum>3</manvolnum></citerefentry>