The eventfd() function creates an “eventfd object” that can be used by the user-state program as an event waiting/notification mechanism and the kernel can notify the user-state program of the event.
The eventfd object contains a uint64_t counter, which is saved by the kernel. The value of this counter can be initialized by the initval parameter when initializing eventfd.
The eventfd() call returns a new fd that points to this newly created eventfd object.
The flags parameter can be obtained by bitwise and combination of the following options that determine the behavior of eventfd.
- EFD_CLOEXEC (linux 2.6.27 and later) Set this fd to close-on-exec (automatically closes the fd when exec is called)
- EFD_NONBLOCK (linux 2.6.27 and later) set this fd to non-blocking
- EFD_SEMAPHORE (linux 2.6.30 and later) reads semaphore-like data from eventfd, see the description of read below
Related IO methods
Read returns the length of the 8-byte shaped buffer (i.e., returns 8) upon success. If the supplied buffer is less than 8 bytes, -1 is returned and errno is set to EINVAL error.
The result of read varies depending on whether the eventfd’s counter is 0 and whether the flag parameter was set to EFD_SEMAPHORE when the eventfd object was created.
- If EFD_SEMAPHORE is not set and the value of counter is non-zero, read returns an 8-byte shaper with the value of counter and sets the value of counter to 0
- If EFD_SEMAPHORE is set and the value of counter is non-zero, read returns an 8-byte shape-shift with a value of 1 and sets the value of counter by one
- If the value of counter is 0, the process goes into a blocking state or returns errno for EAGAIN, depending on whether flag is set to nonblocking
The write method adds 8 bytes of shaped data from the buffer to the eventfd’s counter. the maximum value stored on the counter is unint64-1, i.e. 0xfffffffffffffffe. if the sum is exceeded, wirte causes blocking or returns an errno depending on whether flag is set to non-blocking. EAGAIN’s errno.
If the buffer provided to the write call is less than 8 bytes, or if an attempt is made to write 0xffffffffffffffffff, write will return an EINVAL error.
poll, select and other similar operations
eventfd supports poll, select, epoll, and other similar operations.
- eventfd is readable when the value of counter is greater than 0
- eventfd is writable when counter is less than 0xffffffffffffff, i.e., at least one 1 can be written without blocking
- When counter overflows, select considers eventfd both writeable and readable, and poll returns a POLLERR error. As mentioned above, write will never cause a counter overflow. However, if the KAIO subsystem performs 2^64 eventfd “signal posts”, an overflow may occur (theoretically possible, but unlikely in practice). If an overflow occurs, the read will return that maximum uint64_t value (i.e. 0xffffffffffffffffff).
The eventfd file descriptor also supports other file descriptor multiplexing APIs: pselect and ppoll.
File descriptors should be closed when they are no longer needed. When all file descriptors associated with the same eventfd object have been closed, the kernel releases the object’s resources.
A copy of the file descriptor created by eventfd() is inherited by the child process generated by fork. Duplicate file descriptors are associated with the same eventfd object. File descriptors created by eventfd() are retained in execve unless the close-on-exec flag is set.
On success, eventfd() returns a new eventfd file descriptor. On error, the return value is -1 and errno is set to the corresponding error code.
- EINVAL flags have unsupported values specified in them
- EMFILE has reached the limit of the number of open file descriptors per process
- ENFILE The limit on the number of file descriptors opened by the operating system has been reached.
- ENODEV cannot mount (internal) anonymous inode devices
- ENOMEM does not have enough memory to create a new eventfd
eventfd() is available from linux kernel 2.6.22 and later, with support provided by glibc from version 2.8 onwards. eventfd2() system call is supported from kernel 2.6.27 onwards. From version 2.9 onwards, glibc’s eventfd() is also implemented internally based on eventfd2().
In all cases where the pipe is used only to signal events, the application can use the eventfd file descriptor instead of the pipe (see pipe(2)). The eventfd file descriptor has a much lower kernel overhead than a pipe and requires only one file descriptor (while a pipe requires two).
When used in the kernel, the eventfd file descriptor can provide a bridge from the kernel to user space, for example, by allowing functions such as KAIO (kernel AIO) to signal to the file descriptor that certain operations have completed.
A key point about the eventfd file descriptor is that it can be monitored using select(2), poll(2) or epoll(7) just like any other file descriptor. This means that applications can monitor both the readiness of “legacy” files and the readiness of other kernel mechanisms that support the eventfd interface. (Without the eventfd() interface, these mechanisms cannot be multiplexed via select(2), poll(2), or epoll(7).
The current value of the eventfd counter can be viewed in the
/proc/[pid]/fdinfo directory of the process via an entry in the corresponding file descriptor. For more details, see proc(5).
There are two underlying Linux system calls: eventfd() and, more recently, eventfd2(). The former system call does not implement the flags argument. The latter system call implements the above flag values. The glibc wrapper function will use eventfd2() where available.