Loading...
Searching...
No Matches
select.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2019 Freie Universität Berlin
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
27
28#ifdef CPU_NATIVE
29/* On native, system headers may depend on system's <sys/select.h>. Hence,
30 * include the real sys/select.h here. */
31__extension__
32#include_next <sys/select.h>
33#endif
34
35#include <string.h>
36/* prevent cyclic dependency with newlib/picolibc's `sys/types.h` */
37#if (defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC)) && !defined(CPU_ESP8266)
38#include <sys/_timeval.h>
39#else
40#include <sys/time.h>
41#endif
42
43#include "bitfield.h"
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
52#define POSIX_SELECT_THREAD_FLAG (1U << 3)
53
54#ifndef CPU_NATIVE
55
67#ifndef CONFIG_POSIX_FD_SET_SIZE
68#define CONFIG_POSIX_FD_SET_SIZE (16)
69#endif
71
72/* ESP's newlib has this already defined in `sys/types.h` */
73#if !defined(CPU_ESP8266)
79#define FD_SETSIZE (CONFIG_POSIX_FD_SET_SIZE)
80
84typedef struct {
87} fd_set;
88
95static inline void FD_CLR(int fd, fd_set *fdsetp)
96{
97 bf_unset(fdsetp->fds, fd);
98}
99
109static inline int FD_ISSET(int fd, fd_set *fdsetp)
110{
111 return (int)bf_isset(fdsetp->fds, fd);
112}
113
121static inline void FD_SET(int fd, fd_set *fdsetp)
122{
123 bf_set(fdsetp->fds, fd);
124}
125
131static inline void FD_ZERO(fd_set *fdsetp)
132{
133 memset(fdsetp->fds, 0, sizeof(fdsetp->fds));
134}
135#endif /* !defined(CPU_ESP32) && !defined(CPU_ESP8266) */
136
169int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
170 struct timeval *timeout);
171
172#endif /* CPU_NATIVE */
173
174#ifdef __cplusplus
175}
176#endif
177
Bitfield operations on bitfields of arbitrary length.
static int FD_ISSET(int fd, fd_set *fdsetp)
Checks if a file descriptor is a member of an fd_set
Definition select.h:109
static void FD_SET(int fd, fd_set *fdsetp)
Adds a file descriptor from an fd_set if it is not already a member.
Definition select.h:121
static void FD_ZERO(fd_set *fdsetp)
Initializes the descriptor set as an empty set.
Definition select.h:131
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)
Examines the given file descriptor sets if they are ready for their respective operation.
#define FD_SETSIZE
Maximum number of file descriptors in an fd_set structure.
Definition select.h:79
static void FD_CLR(int fd, fd_set *fdsetp)
Removes a file descriptor from an fd_set if it is a member.
Definition select.h:95
static void bf_set(uint8_t field[], size_t idx)
Set the bit to 1.
Definition bitfield.h:51
static void bf_unset(uint8_t field[], size_t idx)
Clear the bit.
Definition bitfield.h:75
static bool bf_isset(const uint8_t field[], size_t idx)
Check if the bet is set.
Definition bitfield.h:123
The fd_set structure.
Definition select.h:84
BITFIELD(fds, FD_SETSIZE)
Bit-field to represent the set of file descriptors.
Definition of struct timeval for the atmega.
Definition time.h:19