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 
9 #pragma once
10 
31 #ifdef CPU_NATIVE
32 /* On native, system headers may depend on system's <sys/select.h>. Hence,
33  * include the real sys/select.h here. */
34 __extension__
35 #include_next <sys/select.h>
36 #endif
37 
38 #include <string.h>
39 /* prevent cyclic dependency with newlib/picolibc's `sys/types.h` */
40 #if (defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC)) && !defined(CPU_ESP8266)
41 #include <sys/_timeval.h>
42 #else
43 #include <sys/time.h>
44 #endif
45 
46 #include "bitfield.h"
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
55 #define POSIX_SELECT_THREAD_FLAG (1U << 3)
56 
57 #ifndef CPU_NATIVE
58 
70 #ifndef CONFIG_POSIX_FD_SET_SIZE
71 #define CONFIG_POSIX_FD_SET_SIZE (16)
72 #endif
75 /* ESP's newlib has this already defined in `sys/types.h` */
76 #if !defined(CPU_ESP8266)
82 #define FD_SETSIZE (CONFIG_POSIX_FD_SET_SIZE)
83 
87 typedef struct {
90 } fd_set;
91 
98 static inline void FD_CLR(int fd, fd_set *fdsetp)
99 {
100  bf_unset(fdsetp->fds, fd);
101 }
102 
112 static inline int FD_ISSET(int fd, fd_set *fdsetp)
113 {
114  return (int)bf_isset(fdsetp->fds, fd);
115 }
116 
124 static inline void FD_SET(int fd, fd_set *fdsetp)
125 {
126  bf_set(fdsetp->fds, fd);
127 }
128 
134 static inline void FD_ZERO(fd_set *fdsetp)
135 {
136  memset(fdsetp->fds, 0, sizeof(fdsetp->fds));
137 }
138 #endif /* !defined(CPU_ESP32) && !defined(CPU_ESP8266) */
139 
172 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
173  struct timeval *timeout);
174 
175 #endif /* CPU_NATIVE */
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
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:55
static void bf_unset(uint8_t field[], size_t idx)
Clear the bit.
Definition: bitfield.h:79
static bool bf_isset(const uint8_t field[], size_t idx)
Check if the bet is set.
Definition: bitfield.h:127
static int FD_ISSET(int fd, fd_set *fdsetp)
Checks if a file descriptor is a member of an fd_set
Definition: select.h:112
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:124
static void FD_ZERO(fd_set *fdsetp)
Initializes the descriptor set as an empty set.
Definition: select.h:134
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:82
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:98
The fd_set structure.
Definition: select.h:87
BITFIELD(fds, FD_SETSIZE)
Bit-field to represent the set of file descriptors.
Definition of struct timeval for the atmega.
Definition: time.h:22