]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/eventfd.2
epoll_create.2, epoll_ctl.2, epoll_wait.2, eventfd.2, ioprio_set.2, signalfd.2, spu_c...
[thirdparty/man-pages.git] / man2 / eventfd.2
1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
3 .\"
4 .\" This program is free software; you can redistribute it and/or modify
5 .\" it under the terms of the GNU General Public License as published by
6 .\" the Free Software Foundation; either version 2 of the License, or
7 .\" (at your option) any later version.
8 .\"
9 .\" This program is distributed in the hope that it will be useful,
10 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
11 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 .\" GNU General Public License for more details.
13 .\"
14 .\" You should have received a copy of the GNU General Public
15 .\" License along with this manual; if not, see
16 .\" <http://www.gnu.org/licenses/>.
17 .\"
18 .\" 2008-10-10, mtk: describe eventfd2(), and EFD_NONBLOCK and EFD_CLOEXEC
19 .\"
20 .TH EVENTFD 2 2010-08-30 Linux "Linux Programmer's Manual"
21 .SH NAME
22 eventfd \- create a file descriptor for event notification
23 .SH SYNOPSIS
24 .B #include <sys/eventfd.h>
25 .sp
26 .BI "int eventfd(unsigned int " initval ", int " flags );
27 .SH DESCRIPTION
28 .BR eventfd ()
29 creates an "eventfd object" that can be used as
30 an event wait/notify mechanism by user-space applications,
31 and by the kernel to notify user-space applications of events.
32 The object contains an unsigned 64-bit integer
33 .RI ( uint64_t )
34 counter that is maintained by the kernel.
35 This counter is initialized with the value specified in the argument
36 .IR initval .
37
38 The following values may be bitwise ORed in
39 .IR flags
40 to change the behaviour of
41 .BR eventfd ():
42 .TP
43 .BR EFD_CLOEXEC " (since Linux 2.6.27)"
44 Set the close-on-exec
45 .RB ( FD_CLOEXEC )
46 flag on the new file descriptor.
47 See the description of the
48 .B O_CLOEXEC
49 flag in
50 .BR open (2)
51 for reasons why this may be useful.
52 .TP
53 .BR EFD_NONBLOCK " (since Linux 2.6.27)"
54 Set the
55 .BR O_NONBLOCK
56 file status flag on the new open file description.
57 Using this flag saves extra calls to
58 .BR fcntl (2)
59 to achieve the same result.
60 .TP
61 .BR EFD_SEMAPHORE " (since Linux 2.6.30)"
62 Provide semaphore-like semantics for reads from the new file descriptor.
63 See below.
64 .PP
65 In Linux up to version 2.6.26, the
66 .I flags
67 argument is unused, and must be specified as zero.
68
69 As its return value,
70 .BR eventfd ()
71 returns a new file descriptor that can be used to refer to the
72 eventfd object.
73 The following operations can be performed on the file descriptor:
74 .TP
75 .BR read (2)
76 Each successful
77 .BR read (2)
78 returns an 8-byte integer.
79 A
80 .BR read (2)
81 will fail with the error
82 .B EINVAL
83 if the size of the supplied buffer is less than 8 bytes.
84 .IP
85 The value returned by
86 .BR read (2)
87 is in host byte order,
88 i.e., the native byte order for integers on the host machine.
89 .IP
90 The semantics of
91 .BR read (2)
92 depend on whether the eventfd counter currently has a nonzero value
93 and whether the
94 .BR EFD_SEMAPHORE
95 flag was specified when creating the eventfd file descriptor:
96 .RS
97 .IP * 3
98 If
99 .BR EFD_SEMAPHORE
100 was not specified and the eventfd counter has a nonzero value, then a
101 .BR read (2)
102 returns 8 bytes containing that value,
103 and the counter's value is reset to zero.
104 .IP *
105 If
106 .BR EFD_SEMAPHORE
107 was specified and the eventfd counter has a nonzero value, then a
108 .BR read (2)
109 returns 8 bytes containing the value 1,
110 and the counter's value is decremented by 1.
111 .IP *
112 If the eventfd counter is zero at the time of the call to
113 .BR read (2),
114 then the call either blocks until the counter becomes nonzero
115 (at which time, the
116 .BR read (2)
117 proceeds as described above)
118 or fails with the error
119 .B EAGAIN
120 if the file descriptor has been made nonblocking.
121 .RE
122 .TP
123 .BR write (2)
124 A
125 .BR write (2)
126 call adds the 8-byte integer value supplied in its
127 buffer to the counter.
128 The maximum value that may be stored in the counter is the largest
129 unsigned 64-bit value minus 1 (i.e., 0xfffffffffffffffe).
130 If the addition would cause the counter's value to exceed
131 the maximum, then the
132 .BR write (2)
133 either blocks until a
134 .BR read (2)
135 is performed on the file descriptor,
136 or fails with the error
137 .B EAGAIN
138 if the file descriptor has been made nonblocking.
139 .IP
140 A
141 .BR write (2)
142 will fail with the error
143 .B EINVAL
144 if the size of the supplied buffer is less than 8 bytes,
145 or if an attempt is made to write the value 0xffffffffffffffff.
146 .TP
147 .BR poll "(2), " select "(2) (and similar)"
148 The returned file descriptor supports
149 .BR poll (2)
150 (and analogously
151 .BR epoll (7))
152 and
153 .BR select (2),
154 as follows:
155 .RS
156 .IP * 3
157 The file descriptor is readable
158 (the
159 .BR select (2)
160 .I readfds
161 argument; the
162 .BR poll (2)
163 .B POLLIN
164 flag)
165 if the counter has a value greater than 0.
166 .IP *
167 The file descriptor is writable
168 (the
169 .BR select (2)
170 .I writefds
171 argument; the
172 .BR poll (2)
173 .B POLLOUT
174 flag)
175 if it is possible to write a value of at least "1" without blocking.
176 .IP *
177 If an overflow of the counter value was detected,
178 then
179 .BR select (2)
180 indicates the file descriptor as being both readable and writable, and
181 .BR poll (2)
182 returns a
183 .B POLLERR
184 event.
185 As noted above,
186 .BR write (2)
187 can never overflow the counter.
188 However an overflow can occur if 2^64
189 eventfd "signal posts" were performed by the KAIO
190 subsystem (theoretically possible, but practically unlikely).
191 If an overflow has occurred, then
192 .BR read (2)
193 will return that maximum
194 .I uint64_t
195 value (i.e., 0xffffffffffffffff).
196 .RE
197 .IP
198 The eventfd file descriptor also supports the other file-descriptor
199 multiplexing APIs:
200 .BR pselect (2)
201 and
202 .BR ppoll (2).
203 .TP
204 .BR close (2)
205 When the file descriptor is no longer required it should be closed.
206 When all file descriptors associated with the same eventfd object
207 have been closed, the resources for object are freed by the kernel.
208 .PP
209 A copy of the file descriptor created by
210 .BR eventfd ()
211 is inherited by the child produced by
212 .BR fork (2).
213 The duplicate file descriptor is associated with the same
214 eventfd object.
215 File descriptors created by
216 .BR eventfd ()
217 are preserved across
218 .BR execve (2),
219 unless the close-on-exec flag has been set.
220 .SH RETURN VALUE
221 On success,
222 .BR eventfd ()
223 returns a new eventfd file descriptor.
224 On error, \-1 is returned and
225 .I errno
226 is set to indicate the error.
227 .SH ERRORS
228 .TP
229 .B EINVAL
230 An unsupported value was specified in
231 .IR flags .
232 .TP
233 .B EMFILE
234 The per-process limit on open file descriptors has been reached.
235 .TP
236 .B ENFILE
237 The system-wide limit on the total number of open files has been
238 reached.
239 .TP
240 .B ENODEV
241 .\" Note from Davide:
242 .\" The ENODEV error is basically never going to happen if
243 .\" the kernel boots correctly. That error happen only if during
244 .\" the kernel initialization, some error occur in the anonymous
245 .\" inode source initialization.
246 Could not mount (internal) anonymous inode device.
247 .TP
248 .B ENOMEM
249 There was insufficient memory to create a new
250 eventfd file descriptor.
251 .SH VERSIONS
252 .BR eventfd ()
253 is available on Linux since kernel 2.6.22.
254 Working support is provided in glibc since version 2.8.
255 .\" eventfd() is in glibc 2.7, but reportedly does not build
256 The
257 .BR eventfd2 ()
258 system call (see NOTES) is available on Linux since kernel 2.6.27.
259 Since version 2.9, the glibc
260 .BR eventfd ()
261 wrapper will employ the
262 .BR eventfd2 ()
263 system call, if it is supported by the kernel.
264 .SH CONFORMING TO
265 .BR eventfd ()
266 and
267 .BR eventfd2 ()
268 are Linux-specific.
269 .SH NOTES
270 Applications can use an eventfd file descriptor instead of a pipe (see
271 .BR pipe (2))
272 in all cases where a pipe is used simply to signal events.
273 The kernel overhead of an eventfd file descriptor
274 is much lower than that of a pipe,
275 and only one file descriptor is
276 required (versus the two required for a pipe).
277
278 When used in the kernel, an eventfd
279 file descriptor can provide a bridge from kernel to user space, allowing,
280 for example, functionalities like KAIO (kernel AIO)
281 .\" or eventually syslets/threadlets
282 to signal to a file descriptor that some operation is complete.
283
284 A key point about an eventfd file descriptor is that it can be
285 monitored just like any other file descriptor using
286 .BR select (2),
287 .BR poll (2),
288 or
289 .BR epoll (7).
290 This means that an application can simultaneously monitor the
291 readiness of "traditional" files and the readiness of other
292 kernel mechanisms that support the eventfd interface.
293 (Without the
294 .BR eventfd ()
295 interface, these mechanisms could not be multiplexed via
296 .BR select (2),
297 .BR poll (2),
298 or
299 .BR epoll (7).)
300 .SS Underlying Linux system calls
301 There are two underlying Linux system calls:
302 .BR eventfd ()
303 and the more recent
304 .BR eventfd2 ().
305 The former system call does not implement a
306 .I flags
307 argument.
308 The latter system call implements the
309 .I flags
310 values described above.
311 The glibc wrapper function will use
312 .BR eventfd2 ()
313 where it is available.
314 .SS Additional glibc features
315 The GNU C library defines an additional type,
316 and two functions that attempt to abstract some of the details of
317 reading and writing on an eventfd file descriptor:
318 .in +4n
319 .nf
320
321 typedef uint64_t eventfd_t;
322
323 int eventfd_read(int fd, eventfd_t *value);
324 int eventfd_write(int fd, eventfd_t value);
325 .fi
326 .in
327
328 The functions perform the read and write operations on an
329 eventfd file descriptor,
330 returning 0 if the correct number of bytes was transferred,
331 or \-1 otherwise.
332 .SH EXAMPLE
333 .PP
334 The following program creates an eventfd file descriptor
335 and then forks to create a child process.
336 While the parent briefly sleeps,
337 the child writes each of the integers supplied in the program's
338 command-line arguments to the eventfd file descriptor.
339 When the parent has finished sleeping,
340 it reads from the eventfd file descriptor.
341
342 The following shell session shows a sample run of the program:
343 .in +4n
344 .nf
345
346 .RB "$" " ./a.out 1 2 4 7 14"
347 Child writing 1 to efd
348 Child writing 2 to efd
349 Child writing 4 to efd
350 Child writing 7 to efd
351 Child writing 14 to efd
352 Child completed write loop
353 Parent about to read
354 Parent read 28 (0x1c) from efd
355 .fi
356 .in
357 .SS Program source
358 \&
359 .nf
360 #include <sys/eventfd.h>
361 #include <unistd.h>
362 #include <stdlib.h>
363 #include <stdio.h>
364 #include <stdint.h> /* Definition of uint64_t */
365
366 #define handle_error(msg) \\
367 do { perror(msg); exit(EXIT_FAILURE); } while (0)
368
369 int
370 main(int argc, char *argv[])
371 {
372 int efd, j;
373 uint64_t u;
374 ssize_t s;
375
376 if (argc < 2) {
377 fprintf(stderr, "Usage: %s <num>...\\n", argv[0]);
378 exit(EXIT_FAILURE);
379 }
380
381 efd = eventfd(0, 0);
382 if (efd == \-1)
383 handle_error("eventfd");
384
385 switch (fork()) {
386 case 0:
387 for (j = 1; j < argc; j++) {
388 printf("Child writing %s to efd\\n", argv[j]);
389 u = strtoull(argv[j], NULL, 0);
390 /* strtoull() allows various bases */
391 s = write(efd, &u, sizeof(uint64_t));
392 if (s != sizeof(uint64_t))
393 handle_error("write");
394 }
395 printf("Child completed write loop\\n");
396
397 exit(EXIT_SUCCESS);
398
399 default:
400 sleep(2);
401
402 printf("Parent about to read\\n");
403 s = read(efd, &u, sizeof(uint64_t));
404 if (s != sizeof(uint64_t))
405 handle_error("read");
406 printf("Parent read %llu (0x%llx) from efd\\n",
407 (unsigned long long) u, (unsigned long long) u);
408 exit(EXIT_SUCCESS);
409
410 case \-1:
411 handle_error("fork");
412 }
413 }
414 .fi
415 .SH SEE ALSO
416 .BR futex (2),
417 .BR pipe (2),
418 .BR poll (2),
419 .BR read (2),
420 .BR select (2),
421 .BR signalfd (2),
422 .BR timerfd_create (2),
423 .BR write (2),
424 .BR epoll (7),
425 .BR sem_overview (7)