1SD_EVENT_NEW(3) sd_event_new SD_EVENT_NEW(3)
2
3
4
6 sd_event_new, sd_event_default, sd_event_ref, sd_event_unref,
7 sd_event_unrefp, sd_event_get_tid, sd_event - Acquire and release an
8 event loop object
9
11 #include <systemd/sd-event.h>
12
13 typedef struct sd_event sd_event;
14
15 int sd_event_new(sd_event **event);
16
17 int sd_event_default(sd_event **event);
18
19 sd_event *sd_event_ref(sd_event *event);
20
21 sd_event *sd_event_unref(sd_event *event);
22
23 void sd_event_unrefp(sd_event **event);
24
25 int sd_event_get_tid(sd_event *event, pid_t *tid);
26
28 sd_event_new() allocates a new event loop object. The event loop object
29 is returned in the event parameter. After use, drop the returned
30 reference with sd_event_unref(). When the last reference is dropped,
31 the object is freed.
32
33 sd_event_default() acquires a reference to the default event loop
34 object of the calling thread, possibly allocating a new object if no
35 default event loop object has been allocated yet for the thread. After
36 use, drop the returned reference with sd_event_unref(). When the last
37 reference is dropped, the event loop is freed. If this function is
38 called while the object returned from a previous call from the same
39 thread is still referenced, the same object is returned again, but the
40 reference is increased by one. It is recommended to use this call
41 instead of sd_event_new() in order to share event loop objects between
42 various components that are dispatched in the same thread. All threads
43 have exactly either zero or one default event loop objects associated,
44 but never more.
45
46 After allocating an event loop object, add event sources to it with
47 sd_event_add_io(3), sd_event_add_time(3), sd_event_add_signal(3),
48 sd_event_add_child(3), sd_event_add_inotify(3), sd_event_add_defer(3),
49 sd_event_add_post(3) or sd_event_add_exit(3), and then execute the
50 event loop using sd_event_loop(3).
51
52 sd_event_ref() increases the reference count of the specified event
53 loop object by one.
54
55 sd_event_unref() decreases the reference count of the specified event
56 loop object by one. If the count hits zero, the object is freed. Note
57 that it is freed regardless of whether it is the default event loop
58 object for a thread or not. This means that allocating an event loop
59 with sd_event_default(), then releasing it, and then acquiring a new
60 one with sd_event_default() will result in two distinct objects. Note
61 that, in order to free an event loop object, all remaining event
62 sources of the event loop also need to be freed as each keeps a
63 reference to it.
64
65 sd_event_unrefp() is similar to sd_event_unref() but takes a pointer to
66 a pointer to an sd_event object. This call is useful in conjunction
67 with GCC's and LLVM's Clean-up Variable Attribute[1]. Note that this
68 function is defined as inline function. Use a declaration like the
69 following, in order to allocate an event loop object that is freed
70 automatically as the code block is left:
71
72 {
73 __attribute__((cleanup(sd_event_unrefp))) sd_event *event = NULL;
74 int r;
75 ...
76 r = sd_event_default(&event);
77 if (r < 0) {
78 errno = -r;
79 fprintf(stderr, "Failed to allocate event loop: %m\n");
80 }
81 ...
82 }
83
84 sd_event_ref(), sd_event_unref() and sd_event_unrefp() execute no
85 operation if the passed in event loop object is NULL.
86
87 sd_event_get_tid() retrieves the thread identifier ("TID") of the
88 thread the specified event loop object is associated with. This call is
89 only supported for event loops allocated with sd_event_default(), and
90 returns the identifier for the thread the event loop is the default
91 event loop of. See gettid(2) for more information on thread
92 identifiers.
93
95 On success, sd_event_new(), sd_event_default() and sd_event_get_tid()
96 return 0 or a positive integer. On failure, they return a negative
97 errno-style error code. sd_event_ref() always returns a pointer to the
98 event loop object passed in. sd_event_unref() always returns NULL.
99
100 Errors
101 Returned errors may indicate the following problems:
102
103 -ENOMEM
104 Not enough memory to allocate the object.
105
106 -EMFILE
107 The maximum number of event loops has been allocated.
108
109 -ENXIO
110 sd_event_get_tid() was invoked on an event loop object that was not
111 allocated with sd_event_default().
112
114 These APIs are implemented as a shared library, which can be compiled
115 and linked to with the libsystemd pkg-config(1) file.
116
118 systemd(1), sd-event(3), sd_event_add_io(3), sd_event_add_time(3),
119 sd_event_add_signal(3), sd_event_add_child(3), sd_event_add_inotify(3),
120 sd_event_add_defer(3), sd_event_run(3), gettid(2)
121
123 1. Clean-up Variable Attribute
124 https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
125
126
127
128systemd 253 SD_EVENT_NEW(3)