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