]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/sd_event_new.xml
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / man / sd_event_new.xml
CommitLineData
3802a3d3 1<?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
faae655d 2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
12b42c76 3"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
faae655d
LP
4
5<!--
572eb058
ZJS
6 SPDX-License-Identifier: LGPL-2.1+
7
b975b0d5 8 Copyright 2014 Lennart Poettering
faae655d
LP
9-->
10
dc83f27a 11<refentry id="sd_event_new" xmlns:xi="http://www.w3.org/2001/XInclude">
faae655d
LP
12
13 <refentryinfo>
14 <title>sd_event_new</title>
15 <productname>systemd</productname>
16
17 <authorgroup>
18 <author>
19 <contrib>Developer</contrib>
20 <firstname>Lennart</firstname>
21 <surname>Poettering</surname>
22 <email>lennart@poettering.net</email>
23 </author>
24 </authorgroup>
25 </refentryinfo>
26
27 <refmeta>
28 <refentrytitle>sd_event_new</refentrytitle>
29 <manvolnum>3</manvolnum>
30 </refmeta>
31
32 <refnamediv>
33 <refname>sd_event_new</refname>
34 <refname>sd_event_default</refname>
35 <refname>sd_event_ref</refname>
36 <refname>sd_event_unref</refname>
4afd3348 37 <refname>sd_event_unrefp</refname>
dc83f27a
LP
38 <refname>sd_event_get_tid</refname>
39 <refname>sd_event</refname>
faae655d
LP
40
41 <refpurpose>Acquire and release an event loop object</refpurpose>
42 </refnamediv>
43
44 <refsynopsisdiv>
45 <funcsynopsis>
dc83f27a
LP
46 <funcsynopsisinfo>#include &lt;systemd/sd-event.h&gt;</funcsynopsisinfo>
47
48 <funcsynopsisinfo><token>typedef</token> struct sd_event sd_event;</funcsynopsisinfo>
faae655d
LP
49
50 <funcprototype>
51 <funcdef>int <function>sd_event_new</function></funcdef>
dc83f27a 52 <paramdef>sd_event **<parameter>event</parameter></paramdef>
faae655d
LP
53 </funcprototype>
54
55 <funcprototype>
56 <funcdef>int <function>sd_event_default</function></funcdef>
dc83f27a
LP
57 <paramdef>sd_event **<parameter>event</parameter></paramdef>
58 </funcprototype>
59
60 <funcprototype>
61 <funcdef>sd_event *<function>sd_event_ref</function></funcdef>
62 <paramdef>sd_event *<parameter>event</parameter></paramdef>
faae655d
LP
63 </funcprototype>
64
65 <funcprototype>
dc83f27a
LP
66 <funcdef>sd_event *<function>sd_event_unref</function></funcdef>
67 <paramdef>sd_event *<parameter>event</parameter></paramdef>
faae655d
LP
68 </funcprototype>
69
4afd3348
LP
70 <funcprototype>
71 <funcdef>void <function>sd_event_unrefp</function></funcdef>
72 <paramdef>sd_event **<parameter>event</parameter></paramdef>
73 </funcprototype>
74
faae655d 75 <funcprototype>
dc83f27a
LP
76 <funcdef>int <function>sd_event_get_tid</function></funcdef>
77 <paramdef>sd_event *<parameter>event</parameter></paramdef>
78 <paramdef>pid_t *<parameter>tid</parameter></paramdef>
faae655d
LP
79 </funcprototype>
80
81 </funcsynopsis>
82 </refsynopsisdiv>
83
84 <refsect1>
85 <title>Description</title>
86
87 <para><function>sd_event_new()</function> allocates a new event
88 loop object. The event loop object is returned in the
b8bde116 89 <parameter>event</parameter> parameter. After use, drop
faae655d
LP
90 the returned reference with
91 <function>sd_event_unref()</function>. When the last reference is
bfe6c07e 92 dropped, the object is freed.</para>
faae655d
LP
93
94 <para><function>sd_event_default()</function> acquires a reference
95 to the default event loop object of the calling thread, possibly
96 allocating a new object if no default event loop object has been
b8bde116 97 allocated yet for the thread. After use, drop the returned
a6ad1458 98 reference with <function>sd_event_unref()</function>. When the
b8bde116 99 last reference is dropped, the event loop is freed. If this
faae655d
LP
100 function is called while the object returned from a previous call
101 from the same thread is still referenced, the same object is
102 returned again, but the reference is increased by one. It is
103 recommended to use this call instead of
104 <function>sd_event_new()</function> in order to share event loop
105 objects between various components that are dispatched in the same
bfe6c07e
ZJS
106 thread. All threads have exactly either zero or one default event loop
107 objects associated, but never more.</para>
faae655d 108
dc83f27a
LP
109 <para>After allocating an event loop object, add event sources to
110 it with
111 <citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
112 <citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
113 <citerefentry><refentrytitle>sd_event_add_signal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
114 <citerefentry><refentrytitle>sd_event_add_child</refentrytitle><manvolnum>3</manvolnum></citerefentry>
115 or
116 <citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
117 and then execute the event loop using
118 <citerefentry><refentrytitle>sd_event_run</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
119
faae655d 120 <para><function>sd_event_ref()</function> increases the reference
bfe6c07e 121 count of the specified event loop object by one.</para>
faae655d
LP
122
123 <para><function>sd_event_unref()</function> decreases the
bfe6c07e
ZJS
124 reference count of the specified event loop object by one. If
125 the count hits zero, the object is freed. Note that it
dca348bc 126 is freed regardless of whether it is the default event loop object for a
faae655d 127 thread or not. This means that allocating an event loop with
b8bde116 128 <function>sd_event_default()</function>, then releasing it, and
faae655d
LP
129 then acquiring a new one with
130 <function>sd_event_default()</function> will result in two
b938cb90 131 distinct objects. Note that, in order to free an event loop object,
faae655d 132 all remaining event sources of the event loop also need to be
bfe6c07e 133 freed as each keeps a reference to it.</para>
dc83f27a 134
4afd3348
LP
135 <para><function>sd_event_unrefp()</function> is similar to
136 <function>sd_event_unref()</function> but takes a pointer to a
137 pointer to an <type>sd_event</type> object. This call is useful in
138 conjunction with GCC's and LLVM's <ulink
139 url="https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html">Clean-up
140 Variable Attribute</ulink>. Note that this function is defined as
141 inline function. Use a declaration like the following,
142 in order to allocate an event loop object that is freed
143 automatically as the code block is left:</para>
144
145 <programlisting>{
146 __attribute__((cleanup(sd_event_unrefp)) sd_event *event = NULL;
147 int r;
148
149 r = sd_event_default(&amp;event);
150 if (r &lt; 0)
151 fprintf(stderr, "Failed to allocate event loop: %s\n", strerror(-r));
152
153}</programlisting>
154
155 <para><function>sd_event_ref()</function>,
156 <function>sd_event_unref()</function> and
157 <function>sd_event_unrefp()</function> execute no operation if the
dc83f27a
LP
158 passed in event loop object is <constant>NULL</constant>.</para>
159
160 <para><function>sd_event_get_tid()</function> retrieves the thread
161 identifier ("TID") of the thread the specified event loop object
162 is associated with. This call is only supported for event loops
163 allocated with <function>sd_event_default()</function>, and
164 returns the identifier for the thread the event loop is the
165 default event loop of. See <citerefentry
166 project='man-pages'><refentrytitle>gettid</refentrytitle><manvolnum>2</manvolnum></citerefentry>
167 for more information on thread identifiers.</para>
faae655d
LP
168 </refsect1>
169
170 <refsect1>
171 <title>Return Value</title>
172
6d6e4ddd
ME
173 <para>On success, <function>sd_event_new()</function>,
174 <function>sd_event_default()</function> and
175 <function>sd_event_get_tid()</function> return 0 or a positive
faae655d
LP
176 integer. On failure, they return a negative errno-style error
177 code. <function>sd_event_ref()</function> always returns a pointer
178 to the event loop object passed
179 in. <function>sd_event_unref()</function> always returns
bfe6c07e 180 <constant>NULL</constant>.</para>
faae655d
LP
181 </refsect1>
182
183 <refsect1>
184 <title>Errors</title>
185
186 <para>Returned errors may indicate the following problems:</para>
187
188 <variablelist>
189 <varlistentry>
8474b70c 190 <term><constant>-ENOMEM</constant></term>
faae655d 191
bfe6c07e 192 <listitem><para>Not enough memory to allocate the object.</para></listitem>
faae655d
LP
193 </varlistentry>
194
195 <varlistentry>
8474b70c 196 <term><constant>-EMFILE</constant></term>
faae655d
LP
197
198 <listitem><para>The maximum number of event loops has been allocated.</para></listitem>
199
200 </varlistentry>
faae655d 201
dc83f27a
LP
202 <varlistentry>
203 <term><constant>-ENXIO</constant></term>
faae655d 204
dc83f27a
LP
205 <listitem><para><function>sd_event_get_tid()</function> was
206 invoked on an event loop object that was not allocated with
207 <function>sd_event_default()</function>.</para></listitem>
208 </varlistentry>
209
210 </variablelist>
faae655d
LP
211 </refsect1>
212
dc83f27a
LP
213 <xi:include href="libsystemd-pkgconfig.xml" />
214
faae655d
LP
215 <refsect1>
216 <title>See Also</title>
217
218 <para>
219 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
220 <citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
221 <citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
222 <citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
223 <citerefentry><refentrytitle>sd_event_add_signal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
224 <citerefentry><refentrytitle>sd_event_add_child</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
225 <citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
226 <citerefentry><refentrytitle>sd_event_add_post</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
dc83f27a
LP
227 <citerefentry><refentrytitle>sd_event_add_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
228 <citerefentry><refentrytitle>sd_event_run</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
229 <citerefentry project='man-pages'><refentrytitle>gettid</refentrytitle><manvolnum>2</manvolnum></citerefentry>
faae655d
LP
230 </para>
231 </refsect1>
232
233</refentry>