Skip to content

Commit 49ac191

Browse files
cfriedtnashif
authored andcommitted
net: sockets: move select() implementation to zvfs
Move the implementation of zsock_select() to zvfs_select(). This allows other types of file descriptors to also make use of select() functionality even when the network subsystem is not enabled. Additionally, it partially removes a dependency cycle between posix and networking by moving functionality into a mutual dependency. Signed-off-by: Chris Friedt <[email protected]>
1 parent 93973e2 commit 49ac191

File tree

12 files changed

+146
-82
lines changed

12 files changed

+146
-82
lines changed

include/zephyr/net/socket_select.h

+28-14
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919
* @{
2020
*/
2121

22+
#include <time.h>
23+
2224
#include <zephyr/toolchain.h>
2325
#include <zephyr/net/socket_types.h>
26+
#include <zephyr/sys/fdtable.h>
2427

2528
#ifdef __cplusplus
2629
extern "C" {
2730
#endif
2831

2932
/** Socket file descriptor set. */
30-
typedef struct zsock_fd_set {
31-
uint32_t bitset[(CONFIG_ZVFS_OPEN_MAX + 31) / 32];
32-
} zsock_fd_set;
33+
typedef struct zvfs_fd_set zsock_fd_set;
3334

3435
/**
3536
* @brief Legacy function to poll multiple sockets for events
@@ -47,13 +48,16 @@ typedef struct zsock_fd_set {
4748
* it may conflict with generic POSIX ``select()`` function).
4849
* @endrst
4950
*/
50-
__syscall int zsock_select(int nfds, zsock_fd_set *readfds,
51-
zsock_fd_set *writefds,
52-
zsock_fd_set *exceptfds,
53-
struct zsock_timeval *timeout);
51+
static inline int zsock_select(int nfds, zsock_fd_set *readfds, zsock_fd_set *writefds,
52+
zsock_fd_set *exceptfds, struct zsock_timeval *timeout)
53+
{
54+
struct timeval;
55+
56+
return zvfs_select(nfds, readfds, writefds, exceptfds, (struct timeval *)timeout);
57+
}
5458

5559
/** Number of file descriptors which can be added to zsock_fd_set */
56-
#define ZSOCK_FD_SETSIZE (sizeof(((zsock_fd_set *)0)->bitset) * 8)
60+
#define ZSOCK_FD_SETSIZE ZVFS_FD_SETSIZE
5761

5862
/**
5963
* @brief Initialize (clear) fd_set
@@ -67,7 +71,10 @@ __syscall int zsock_select(int nfds, zsock_fd_set *readfds,
6771
* if :kconfig:option:`CONFIG_POSIX_API` is defined.
6872
* @endrst
6973
*/
70-
void ZSOCK_FD_ZERO(zsock_fd_set *set);
74+
static inline void ZSOCK_FD_ZERO(zsock_fd_set *set)
75+
{
76+
ZVFS_FD_ZERO(set);
77+
}
7178

7279
/**
7380
* @brief Check whether socket is a member of fd_set
@@ -81,7 +88,10 @@ void ZSOCK_FD_ZERO(zsock_fd_set *set);
8188
* if :kconfig:option:`CONFIG_POSIX_API` is defined.
8289
* @endrst
8390
*/
84-
int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set);
91+
static inline int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set)
92+
{
93+
return ZVFS_FD_ISSET(fd, set);
94+
}
8595

8696
/**
8797
* @brief Remove socket from fd_set
@@ -95,7 +105,10 @@ int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set);
95105
* if :kconfig:option:`CONFIG_POSIX_API` is defined.
96106
* @endrst
97107
*/
98-
void ZSOCK_FD_CLR(int fd, zsock_fd_set *set);
108+
static inline void ZSOCK_FD_CLR(int fd, zsock_fd_set *set)
109+
{
110+
ZVFS_FD_CLR(fd, set);
111+
}
99112

100113
/**
101114
* @brief Add socket to fd_set
@@ -109,7 +122,10 @@ void ZSOCK_FD_CLR(int fd, zsock_fd_set *set);
109122
* if :kconfig:option:`CONFIG_POSIX_API` is defined.
110123
* @endrst
111124
*/
112-
void ZSOCK_FD_SET(int fd, zsock_fd_set *set);
125+
static inline void ZSOCK_FD_SET(int fd, zsock_fd_set *set)
126+
{
127+
ZVFS_FD_SET(fd, set);
128+
}
113129

114130
/** @cond INTERNAL_HIDDEN */
115131

@@ -153,8 +169,6 @@ static inline void FD_SET(int fd, zsock_fd_set *set)
153169
}
154170
#endif
155171

156-
#include <zephyr/syscalls/socket_select.h>
157-
158172
/**
159173
* @}
160174
*/

include/zephyr/posix/sys/select.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
extern "C" {
1414
#endif
1515

16+
#undef fd_set
1617
#define fd_set zsock_fd_set
17-
#define FD_SETSIZE ZSOCK_FD_SETSIZE
18-
#define FD_ZERO ZSOCK_FD_ZERO
19-
#define FD_SET ZSOCK_FD_SET
20-
#define FD_CLR ZSOCK_FD_CLR
21-
#define FD_ISSET ZSOCK_FD_ISSET
18+
19+
#define FD_SETSIZE ZVFS_FD_SETSIZE
2220

2321
struct timeval;
2422

25-
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
23+
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
24+
void FD_CLR(int fd, fd_set *fdset);
25+
int FD_ISSET(int fd, fd_set *fdset);
26+
void FD_SET(int fd, fd_set *fdset);
27+
void FD_ZERO(fd_set *fdset);
2628

2729
#ifdef __cplusplus
2830
}

include/zephyr/sys/fdtable.h

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <stdarg.h>
1010
#include <sys/types.h>
11+
1112
/* FIXME: For native_posix ssize_t, off_t. */
1213
#include <zephyr/fs/fs.h>
1314
#include <zephyr/sys/mutex.h>
@@ -207,9 +208,23 @@ struct zvfs_pollfd {
207208

208209
__syscall int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout);
209210

210-
struct zsock_fd_set {
211+
struct zvfs_fd_set {
211212
uint32_t bitset[(CONFIG_ZVFS_OPEN_MAX + 31) / 32];
212213
};
214+
215+
#define ZVFS_FD_SETSIZE (sizeof(((struct zvfs_fd_set *)0)->bitset) * 8)
216+
217+
void ZVFS_FD_CLR(int fd, struct zvfs_fd_set *fdset);
218+
int ZVFS_FD_ISSET(int fd, struct zvfs_fd_set *fdset);
219+
void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset);
220+
void ZVFS_FD_ZERO(struct zvfs_fd_set *fdset);
221+
222+
struct timespec;
223+
__syscall int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
224+
struct zvfs_fd_set *ZRESTRICT writefds,
225+
struct zvfs_fd_set *ZRESTRICT errorfds,
226+
const struct timeval *ZRESTRICT timeout);
227+
213228
/**
214229
* Request codes for fd_op_vtable.ioctl().
215230
*
@@ -239,4 +254,6 @@ enum {
239254
}
240255
#endif
241256

257+
#include <zephyr/syscalls/fdtable.h>
258+
242259
#endif /* ZEPHYR_INCLUDE_SYS_FDTABLE_H_ */

lib/os/zvfs/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
zephyr_library()
44
zephyr_library_sources_ifdef(CONFIG_ZVFS_EVENTFD zvfs_eventfd.c)
55
zephyr_library_sources_ifdef(CONFIG_ZVFS_POLL zvfs_poll.c)
6+
zephyr_library_sources_ifdef(CONFIG_ZVFS_SELECT zvfs_select.c)

lib/os/zvfs/Kconfig

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ config ZVFS_POLL_MAX
4949
help
5050
Maximum number of entries supported for poll() call.
5151

52+
config ZVFS_SELECT
53+
bool "ZVFS select"
54+
help
55+
Enable support for zvfs_select().
56+
5257
endif # ZVFS_POLL
5358

5459
endif # ZVFS

0 commit comments

Comments
 (0)