1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
'\" t
.TH "SD_EVENT_WAIT" "3" "" "systemd 255" "sd_event_wait"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
sd_event_wait, sd_event_prepare, sd_event_dispatch, sd_event_get_state, sd_event_get_iteration, SD_EVENT_INITIAL, SD_EVENT_PREPARING, SD_EVENT_ARMED, SD_EVENT_PENDING, SD_EVENT_RUNNING, SD_EVENT_EXITING, SD_EVENT_FINISHED \- Low\-level event loop operations
.SH "SYNOPSIS"
.sp
.ft B
.nf
#include <systemd/sd\-event\&.h>
.fi
.ft
.sp
.ft B
.nf
enum {
\fBSD_EVENT_INITIAL\fR,
\fBSD_EVENT_PREPARING\fR,
\fBSD_EVENT_ARMED\fR,
\fBSD_EVENT_PENDING\fR,
\fBSD_EVENT_RUNNING\fR,
\fBSD_EVENT_EXITING\fR,
\fBSD_EVENT_FINISHED\fR,
};
.fi
.ft
.HP \w'int\ sd_event_prepare('u
.BI "int sd_event_prepare(sd_event\ *" "event" ");"
.HP \w'int\ sd_event_wait('u
.BI "int sd_event_wait(sd_event\ *" "event" ", uint64_t\ " "usec" ");"
.HP \w'int\ sd_event_dispatch('u
.BI "int sd_event_dispatch(sd_event\ *" "event" ");"
.HP \w'int\ sd_event_get_state('u
.BI "int sd_event_get_state(sd_event\ *" "event" ");"
.HP \w'int\ sd_event_get_iteration('u
.BI "int sd_event_get_iteration(sd_event\ *" "event" ", uint64_t\ *" "ret" ");"
.SH "DESCRIPTION"
.PP
The low\-level
\fBsd_event_prepare()\fR,
\fBsd_event_wait()\fR
and
\fBsd_event_dispatch()\fR
functions may be used to execute specific phases of an event loop\&. See
\fBsd_event_run\fR(3)
and
\fBsd_event_loop\fR(3)
for higher\-level functions that execute individual but complete iterations of an event loop or run it continuously\&.
.PP
\fBsd_event_prepare()\fR
checks for pending events and arms necessary timers\&. If any events are ready to be processed ("pending"), it returns a positive, non\-zero value, and the caller should process these events with
\fBsd_event_dispatch()\fR\&.
.PP
\fBsd_event_dispatch()\fR
dispatches the highest priority event source that has a pending event\&. On success,
\fBsd_event_dispatch()\fR
returns either zero, which indicates that no further event sources may be dispatched and exiting of the event loop was requested via
\fBsd_event_exit\fR(3); or a positive non\-zero value, which means that an event source was dispatched and the loop returned to its initial state, and the caller should initiate the next event loop iteration by invoking
\fBsd_event_prepare()\fR
again\&.
.PP
In case
\fBsd_event_prepare()\fR
returned zero,
\fBsd_event_wait()\fR
should be called to wait for further events or a timeout\&. If any events are ready to be processed, it returns a positive, non\-zero value, and the events should be dispatched with
\fBsd_event_dispatch()\fR\&. Otherwise, the event loop returned to its initial state and the next event loop iteration should be initiated by invoking
\fBsd_event_prepare()\fR
again\&.
.PP
\fBsd_event_get_state()\fR
may be used to determine the state the event loop is currently in\&. It returns one of the states described below\&.
.PP
\fBsd_event_get_iteration()\fR
may be used to determine the current iteration of the event loop\&. It returns an unsigned 64\-bit integer containing a counter that increases monotonically with each iteration of the event loop, starting with 0\&. The counter is increased at the time of the
\fBsd_event_prepare()\fR
invocation\&.
.PP
All five functions take, as the first argument, the event loop object
\fIevent\fR
that has been created with
\fBsd_event_new()\fR\&. The timeout for
\fBsd_event_wait()\fR
is specified in
\fIusec\fR
in microseconds\&.
\fB(uint64_t) \-1\fR
may be used to specify an infinite timeout\&.
.SH "STATE MACHINE"
.PP
The event loop knows the following states, that may be queried with
\fBsd_event_get_state()\fR\&.
.PP
\fBSD_EVENT_INITIAL\fR
.RS 4
The initial state the event loop is in, before each event loop iteration\&. Use
\fBsd_event_prepare()\fR
to transition the event loop into the
\fBSD_EVENT_ARMED\fR
or
\fBSD_EVENT_PENDING\fR
states\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_PREPARING\fR
.RS 4
An event source is currently being prepared, i\&.e\&. the preparation handler is currently being executed, as set with
\fBsd_event_source_set_prepare\fR(3)\&. This state is only seen in the event source preparation handler that is invoked from the
\fBsd_event_prepare()\fR
call and is immediately followed by
\fBSD_EVENT_ARMED\fR
or
\fBSD_EVENT_PENDING\fR\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_ARMED\fR
.RS 4
\fBsd_event_prepare()\fR
has been called and no event sources were ready to be dispatched\&. Use
\fBsd_event_wait()\fR
to wait for new events, and transition into
\fBSD_EVENT_PENDING\fR
or back into
\fBSD_EVENT_INITIAL\fR\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_PENDING\fR
.RS 4
\fBsd_event_prepare()\fR
or
\fBsd_event_wait()\fR
have been called and there were event sources with events pending\&. Use
\fBsd_event_dispatch()\fR
to dispatch the highest priority event source and transition back to
\fBSD_EVENT_INITIAL\fR, or
\fBSD_EVENT_FINISHED\fR\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_RUNNING\fR
.RS 4
A regular event source is currently being dispatched\&. This state is only seen in the event source handler that is invoked from the
\fBsd_event_dispatch()\fR
call, and is immediately followed by
\fBSD_EVENT_INITIAL\fR
or
\fBSD_EVENT_FINISHED\fR
as soon the event source handler returns\&. Note that during dispatching of exit event sources the
\fBSD_EVENT_EXITING\fR
state is seen instead\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_EXITING\fR
.RS 4
Similar to
\fBSD_EVENT_RUNNING\fR
but is the state in effect while dispatching exit event sources\&. It is followed by
\fBSD_EVENT_INITIAL\fR
or
\fBSD_EVENT_FINISHED\fR
as soon as the event handler returns\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_FINISHED\fR
.RS 4
The event loop has exited\&. All exit event sources have run\&. If the event loop is in this state it serves no purpose anymore, and should be freed\&.
.sp
Added in version 229\&.
.RE
.PP
A simplified flow chart of the states and the calls to transition between them is shown below\&. Note that
\fBSD_EVENT_PREPARING\fR,
\fBSD_EVENT_RUNNING\fR
and
\fBSD_EVENT_EXITING\fR
are not shown here\&.
.sp
.if n \{\
.RS 4
.\}
.nf
INITIAL \-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-\e
| |
| ^
| |
v ret == 0 |
sd_event_prepare() >\-\-\->\-\-\->\-\-\->\-\-\->\- ARMED |
| | ^
| ret > 0 | |
| | |
v v ret == 0 |
PENDING <\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-< sd_event_wait() >\-\-\->\-\-\->\-\-+
| ret > 0 ^
| |
| |
v |
sd_event_dispatch() >\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->/
| ret > 0
| ret == 0
|
v
FINISHED
.fi
.if n \{\
.RE
.\}
.SH "RETURN VALUE"
.PP
On success, these functions return 0 or a positive integer\&. On failure, they return a negative errno\-style error code\&. In case of
\fBsd_event_prepare()\fR
and
\fBsd_event_wait()\fR, a positive, non\-zero return code indicates that events are ready to be processed and zero indicates that no events are ready\&. In case of
\fBsd_event_dispatch()\fR, a positive, non\-zero return code indicates that the event loop returned to its initial state and zero indicates the event loop has exited\&.
\fBsd_event_get_state()\fR
returns a positive or zero state on success\&.
.SS "Errors"
.PP
Returned errors may indicate the following problems:
.PP
\fB\-EINVAL\fR
.RS 4
The
\fIevent\fR
parameter is invalid or
\fBNULL\fR\&.
.RE
.PP
\fB\-EBUSY\fR
.RS 4
The event loop object is not in the right state\&.
.RE
.PP
\fB\-ESTALE\fR
.RS 4
The event loop is already terminated\&.
.RE
.PP
\fB\-ECHILD\fR
.RS 4
The event loop has been created in a different process, library or module instance\&.
.RE
.PP
Other errors are possible, too\&.
.SH "NOTES"
.PP
Functions described here are available as a shared library, which can be compiled against and linked to with the
\fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
file\&.
.PP
The code described here uses
\fBgetenv\fR(3), which is declared to be not multi\-thread\-safe\&. This means that the code calling the functions described here must not call
\fBsetenv\fR(3)
from a parallel thread\&. It is recommended to only do calls to
\fBsetenv()\fR
from an early phase of the program when no other threads have been started\&.
.SH "HISTORY"
.PP
\fBsd_event_prepare()\fR,
\fBsd_event_wait()\fR, and
\fBsd_event_dispatch()\fR
were added in version 220\&.
.PP
\fBsd_event_get_state()\fR
was added in version 229\&.
.PP
\fBsd_event_get_iteration()\fR
was added in version 231\&.
.SH "SEE ALSO"
.PP
\fBsystemd\fR(1),
\fBsd_event_new\fR(3),
\fBsd_event_add_io\fR(3),
\fBsd_event_add_time\fR(3),
\fBsd_event_add_signal\fR(3),
\fBsd_event_add_child\fR(3),
\fBsd_event_add_inotify\fR(3),
\fBsd_event_add_defer\fR(3),
\fBsd_event_run\fR(3),
\fBsd_event_get_fd\fR(3),
\fBsd_event_source_set_prepare\fR(3)
|