Search code examples
clinuxglibc

When will eventfd_read() block?


I wonder under what situation eventfd_read() will block?

I read the manpage but it doesn't mention anything.

I created the file descriptor through eventfd(0,0).

Thanks in advance.


Solution

  • From eventfd(2) man page for read() call:

    If the eventfd counter is zero at the time of the call to read(2), then the call either blocks until the counter becomes nonzero (at which time, the read(2) proceeds as described above) or fails with the error EAGAIN if the file descriptor has been made nonblocking.

    And for eventfd_read() and eventfd_write() functions:

    The functions perform the read and write operations on an eventfd file descriptor, returning 0 if the correct number of bytes was transferred, or -1 otherwise.

    So eventfd_read() is just wrapper for read() and blocks when read() blocks, i.e. when eventfd counter is zero and O_NONBLOCK is not set for a descriptor (using fcntl(2) or EFD_NONBLOCK).

    You can verify this in glibc sources:

    int
    eventfd_read (int fd, eventfd_t *value)
    {
      return __read (fd, value, sizeof (eventfd_t)) != sizeof (eventfd_t) ? -1 : 0;
    }