select.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
29 #ifndef SYS_SELECT_H
30 #define SYS_SELECT_H
31 
32 #ifdef CPU_NATIVE
33 /* On native, system headers may depend on system's <sys/select.h>. Hence,
34  * include the real sys/select.h here. */
35 __extension__
36 #include_next <sys/select.h>
37 #endif
38 
39 #include <string.h>
40 /* prevent cyclic dependency with newlib/picolibc's `sys/types.h` */
41 #if (defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC)) && !defined(CPU_ESP8266)
42 #include <sys/_timeval.h>
43 #else
44 #include <sys/time.h>
45 #endif
46 
47 #include "bitfield.h"
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
56 #define POSIX_SELECT_THREAD_FLAG (1U << 3)
57 
58 #ifndef CPU_NATIVE
59 
71 #ifndef CONFIG_POSIX_FD_SET_SIZE
72 #define CONFIG_POSIX_FD_SET_SIZE (16)
73 #endif
76 /* ESP's newlib has this already defined in `sys/types.h` */
77 #if !defined(CPU_ESP8266)
83 #define FD_SETSIZE (CONFIG_POSIX_FD_SET_SIZE)
84 
88 typedef struct {
91 } fd_set;
92 
99 static inline void FD_CLR(int fd, fd_set *fdsetp)
100 {
101  bf_unset(fdsetp->fds, fd);
102 }
103 
113 static inline int FD_ISSET(int fd, fd_set *fdsetp)
114 {
115  return (int)bf_isset(fdsetp->fds, fd);
116 }
117 
125 static inline void FD_SET(int fd, fd_set *fdsetp)
126 {
127  bf_set(fdsetp->fds, fd);
128 }
129 
135 static inline void FD_ZERO(fd_set *fdsetp)
136 {
137  memset(fdsetp->fds, 0, sizeof(fdsetp->fds));
138 }
139 #endif /* !defined(CPU_ESP32) && !defined(CPU_ESP8266) */
140 
173 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
174  struct timeval *timeout);
175 
176 #endif /* CPU_NATIVE */
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 #endif /* SYS_SELECT_H */
bitfields operations on bitfields of arbitrary length
static void bf_set(uint8_t field[], size_t idx)
Set the bit to 1.
Definition: bitfield.h:56
static void bf_unset(uint8_t field[], size_t idx)
Clear the bit.
Definition: bitfield.h:80
static bool bf_isset(const uint8_t field[], size_t idx)
Check if the bet is set.
Definition: bitfield.h:128
static int FD_ISSET(int fd, fd_set *fdsetp)
Checks if a file descriptor is a member of an fd_set
Definition: select.h:113
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:125
static void FD_ZERO(fd_set *fdsetp)
Initializes the descriptor set as an empty set.
Definition: select.h:135
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:83
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:99
The fd_set structure.
Definition: select.h:88
BITFIELD(fds, FD_SETSIZE)
Bit-field to represent the set of file descriptors.
Definition of struct timeval for the atmega.
Definition: time.h:23