Skip to content

Commit

Permalink
posix: eventfd: revise locking, signaling, and allocation
Browse files Browse the repository at this point in the history
TL;DR - a complete rewrite.

Previously, the prototypical `eventfd()` usage (one thread
performing a blocking `read()`, followed by another thread
performing a `write()`) would deadlock Zephyr. This shortcoming
has existed in Zephyr's `eventfd()` implementation from the
start and the suggested workaround was to use `poll()`.

However, that is not sufficient for integrating 3rd-party
libraries that may rely on proper `eventfd()` blocking
operations such as `eventfd_read()` and `eventfd_write()`.

The culprit was the per-fdtable-entry `struct k_mutex`.

Here we perform a minor revision of the locking strategy
and employ `k_condvar_broadcast()` and `k_condvar_wait()`
to signal and wait on the holder of a given `struct k_mutex`.

It is important to note, however, that the primary means of
synchronizing the eventfd state is actually the eventfd
spinlock. The fdtable mutex and condition variable are mainly
used for the purposes of blocking io (r,w,close) and are not
used in the code path of non-blocking reads.

The `wait_q` and `k_poll_signal` entries were removed from
`struct eventfd` as they were unnecessary.

Additionally, switch to using a bitarray because it is
possibly faster than linear search for allocating and
deallocating eventfd resources.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
  • Loading branch information
cfriedt authored and nashif committed Jun 2, 2023
1 parent 90343a1 commit e6eb0a7
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 171 deletions.
5 changes: 2 additions & 3 deletions include/zephyr/posix/sys/eventfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
#ifndef ZEPHYR_INCLUDE_POSIX_SYS_EVENTFD_H_
#define ZEPHYR_INCLUDE_POSIX_SYS_EVENTFD_H_

#include <zephyr/kernel.h>
#include <zephyr/sys/fdtable.h>
#include <sys/types.h>
#include <stdint.h>

#include <zephyr/kernel.h>
#include <zephyr/posix/fcntl.h>

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit e6eb0a7

Please sign in to comment.