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