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 #include <string.h>
33 /* prevent cyclic dependency with newlib/picolibc's `sys/types.h` */
34 #if (defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC)) && \
35  !defined(CPU_ESP32) && !defined(CPU_ESP8266)
36 #include <sys/_timeval.h>
37 #else
38 #include <sys/time.h>
39 #endif
40 
41 #include "bitfield.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
58 #ifndef CONFIG_POSIX_FD_SET_SIZE
59 #define CONFIG_POSIX_FD_SET_SIZE (16)
60 #endif
61 
66 #define POSIX_SELECT_THREAD_FLAG (1U << 3)
67 
68 /* ESP's newlib has this already defined in `sys/types.h` */
69 #if !defined(CPU_ESP32) && !defined(CPU_ESP8266)
70 
75 #define FD_SETSIZE (CONFIG_POSIX_FD_SET_SIZE)
76 
80 typedef struct {
81  BITFIELD(fds, FD_SETSIZE);
83 } fd_set;
84 
91 static inline void FD_CLR(int fd, fd_set *fdsetp)
92 {
93  bf_unset(fdsetp->fds, fd);
94 }
95 
105 static inline int FD_ISSET(int fd, fd_set *fdsetp)
106 {
107  return (int)bf_isset(fdsetp->fds, fd);
108 }
109 
117 static inline void FD_SET(int fd, fd_set *fdsetp)
118 {
119  bf_set(fdsetp->fds, fd);
120 }
121 
127 static inline void FD_ZERO(fd_set *fdsetp)
128 {
129  memset(fdsetp->fds, 0, sizeof(fdsetp->fds));
130 }
131 #endif /* !defined(CPU_ESP32) && !defined(CPU_ESP8266) */
132 
165 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
166  struct timeval *timeout);
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* SYS_SELECT_H */
173 
bitfield.h
bitfields operations on bitfields of arbitrary length
bf_isset
static bool bf_isset(uint8_t field[], size_t idx)
Check if the bet is set.
Definition: bitfield.h:87
fd_set
The fd_set structure.
Definition: select.h:80
FD_SETSIZE
#define FD_SETSIZE
Maximum number of file descriptors in an fd_set structure.
Definition: select.h:75
select
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.
FD_ISSET
static int FD_ISSET(int fd, fd_set *fdsetp)
Checks if a file descriptor is a member of an fd_set
Definition: select.h:105
FD_CLR
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:91
bf_unset
static void bf_unset(uint8_t field[], size_t idx)
Clear the bit.
Definition: bitfield.h:65
bf_set
static void bf_set(uint8_t field[], size_t idx)
Set the bit to 1.
Definition: bitfield.h:54
timeval
Definition of struct timeval for the atmega.
Definition: time.h:23
BITFIELD
#define BITFIELD(NAME, SIZE)
Declare a bitfield of a given size.
Definition: bitfield.h:46
FD_SET
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:117
FD_ZERO
static void FD_ZERO(fd_set *fdsetp)
Initializes the descriptor set as an empty set.
Definition: select.h:127