Skip to content

Commit 487a875

Browse files
cfriedtdleach02
authored andcommitted
posix: eventfd: fix dependency cycle between net and posix
Until recently, the posix api was purely a consumer of the network subsystem. However, a dependency cycle was added as a stop-gap solution for challenges with the native platform. Specifically, 1. eventfd symbols conflict with those of the host 2. eventfd was excluded from native libc builds via cmake If any part of the posix were then to select the network subsystem (which is a legitimate use case, given that networking is a part of the posix api), we would get a build error due to the Kconfig dependency cycle. As usual, with dependency cycles, the cycle can be broken via a third, mutual dependency. What is the third mutual dependency? Naturally, it is ZVFS which was planned some time ago. ZVFS will be where we collect file-descriptor and FILE-pointer APIs so that we can ensure consistency for Zephyr users. This change deprecates EVENTFD_MAX in favour of ZVFS_EVENTFD_MAX. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent b3e36ad commit 487a875

File tree

16 files changed

+628
-509
lines changed

16 files changed

+628
-509
lines changed

doc/services/portability/posix/kconfig/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ implementation of the POSIX API.
99
* :kconfig:option:`CONFIG_DYNAMIC_THREAD`
1010
* :kconfig:option:`CONFIG_DYNAMIC_THREAD_POOL_SIZE`
1111
* :kconfig:option:`CONFIG_EVENTFD`
12-
* :kconfig:option:`CONFIG_EVENTFD_MAX`
1312
* :kconfig:option:`CONFIG_FDTABLE`
1413
* :kconfig:option:`CONFIG_GETOPT_LONG`
1514
* :kconfig:option:`CONFIG_MAX_PTHREAD_SPINLOCK_COUNT`
@@ -38,3 +37,4 @@ implementation of the POSIX API.
3837
* :kconfig:option:`CONFIG_POSIX_SEM_VALUE_MAX`
3938
* :kconfig:option:`CONFIG_TIMER_CREATE_WAIT`
4039
* :kconfig:option:`CONFIG_THREAD_STACK_INFO`
40+
* :kconfig:option:`CONFIG_ZVFS_EVENTFD_MAX`

include/zephyr/posix/sys/eventfd.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
#ifndef ZEPHYR_INCLUDE_POSIX_SYS_EVENTFD_H_
88
#define ZEPHYR_INCLUDE_POSIX_SYS_EVENTFD_H_
99

10-
#include <stdint.h>
11-
12-
#include <zephyr/kernel.h>
13-
#include <zephyr/posix/fcntl.h>
10+
#include <zephyr/zvfs/eventfd.h>
1411

1512
#ifdef __cplusplus
1613
extern "C" {
1714
#endif
1815

19-
#define EFD_SEMAPHORE 0x2
20-
#define EFD_NONBLOCK O_NONBLOCK
16+
#define EFD_SEMAPHORE ZVFS_EFD_SEMAPHORE
17+
#define EFD_NONBLOCK ZVFS_EFD_NONBLOCK
2118

22-
typedef uint64_t eventfd_t;
19+
typedef zvfs_eventfd_t eventfd_t;
2320

2421
/**
2522
* @brief Create a file descriptor for event notification

include/zephyr/zvfs/eventfd.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2020 Tobias Svehagen
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_ZEPHYR_ZVFS_EVENTFD_H_
8+
#define ZEPHYR_INCLUDE_ZEPHYR_ZVFS_EVENTFD_H_
9+
10+
#include <stdint.h>
11+
12+
#include <zephyr/kernel.h>
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
#define ZVFS_EFD_SEMAPHORE 2
19+
#define ZVFS_EFD_NONBLOCK 0x4000
20+
21+
typedef uint64_t zvfs_eventfd_t;
22+
23+
/**
24+
* @brief Create a file descriptor for ZVFS event notification
25+
*
26+
* The returned file descriptor can be used with POSIX read/write calls or
27+
* with the @ref zvfs_eventfd_read or @ref zvfs_eventfd_write functions.
28+
*
29+
* It also supports polling and by including an eventfd in a call to poll,
30+
* it is possible to signal and wake the polling thread by simply writing to
31+
* the eventfd.
32+
*
33+
* When using read() and write() on a ZVFS eventfd, the size must always be at
34+
* least 8 bytes or the operation will fail with EINVAL.
35+
*
36+
* @return New ZVFS eventfd file descriptor on success, -1 on error
37+
*/
38+
int zvfs_eventfd(unsigned int initval, int flags);
39+
40+
/**
41+
* @brief Read from a ZVFS eventfd
42+
*
43+
* If call is successful, the value parameter will have the value 1
44+
*
45+
* @param fd File descriptor
46+
* @param value Pointer for storing the read value
47+
*
48+
* @return 0 on success, -1 on error
49+
*/
50+
int zvfs_eventfd_read(int fd, zvfs_eventfd_t *value);
51+
52+
/**
53+
* @brief Write to a ZVFS eventfd
54+
*
55+
* @param fd File descriptor
56+
* @param value Value to write
57+
*
58+
* @return 0 on success, -1 on error
59+
*/
60+
int zvfs_eventfd_write(int fd, zvfs_eventfd_t value);
61+
62+
#ifdef __cplusplus
63+
}
64+
#endif
65+
66+
#endif /* ZEPHYR_INCLUDE_ZEPHYR_ZVFS_EVENTFD_H_ */

lib/os/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ zephyr_library_include_directories(
4040
${ZEPHYR_BASE}/kernel/include
4141
${ZEPHYR_BASE}/arch/${ARCH}/include
4242
)
43+
44+
add_subdirectory_ifdef(CONFIG_ZVFS zvfs)

lib/os/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,6 @@ config POWEROFF
123123
Enable support for system power off.
124124

125125
rsource "Kconfig.cbprintf"
126+
rsource "zvfs/Kconfig"
126127

127128
endmenu

lib/os/zvfs/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library()
4+
zephyr_library_sources_ifdef(CONFIG_ZVFS_EVENTFD zvfs_eventfd.c)

lib/os/zvfs/Kconfig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) 2020 Tobias Svehagen
2+
# Copyright (c) 2023 Meta
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
menuconfig ZVFS
7+
bool "Zephyr virtual filesystem (ZVFS) support [EXPERIMENTAL]"
8+
select FDTABLE
9+
select EXPERIMENTAL
10+
help
11+
ZVFS is a central, Zephyr-native library that provides a common interoperable API for all
12+
types of file descriptors such as those from the non-virtual FS, sockets, eventfds, FILE *'s
13+
and more. It is designed to be used by all Zephyr subsystems that need to work with files.
14+
15+
if ZVFS
16+
17+
config ZVFS_EVENTFD
18+
bool "ZVFS event file descriptor support"
19+
select POLL
20+
help
21+
Enable support for ZVFS event file descriptors. An eventfd can
22+
be used as an event wait/notify mechanism together with POSIX calls
23+
like read, write and poll.
24+
25+
if ZVFS_EVENTFD
26+
27+
config ZVFS_EVENTFD_MAX
28+
int "Maximum number of ZVFS eventfd's"
29+
default 1
30+
range 1 4096
31+
help
32+
The maximum number of supported event file descriptors.
33+
34+
endif # ZVFS_EVENTFD
35+
36+
endif # ZVFS

0 commit comments

Comments
 (0)