xref: /haiku/headers/posix/sys/select.h (revision 7ee61327ce4b9487aeca09dd6a2df67616dc1616)
15adca30aSAxel Dörfler /*
2*7ee61327SAugustin Cavalier  * Copyright 2002-2024, Haiku, Inc. All rights reserved.
35adca30aSAxel Dörfler  * Distributed under the terms of the MIT License.
45adca30aSAxel Dörfler  */
5d3dadb6bSPhilippe Houdoin #ifndef _SYS_SELECT_H
6d3dadb6bSPhilippe Houdoin #define _SYS_SELECT_H
7d3dadb6bSPhilippe Houdoin 
8d068515fSAxel Dörfler 
92222d055SIngo Weinhold #include <config/types.h>
102222d055SIngo Weinhold 
11493130b3SAxel Dörfler #include <sys/time.h>
12d068515fSAxel Dörfler #include <signal.h>
13*7ee61327SAugustin Cavalier #include <string.h>
14493130b3SAxel Dörfler 
15493130b3SAxel Dörfler 
16*7ee61327SAugustin Cavalier /* Custom FD_SETSIZEs can be defined if more bits are needed, but the default
17*7ee61327SAugustin Cavalier  * should suffice for most uses. */
18d3dadb6bSPhilippe Houdoin #ifndef FD_SETSIZE
19d3dadb6bSPhilippe Houdoin #	define FD_SETSIZE	1024
20493130b3SAxel Dörfler #endif
21d3dadb6bSPhilippe Houdoin 
222222d055SIngo Weinhold typedef __haiku_uint32 fd_mask;
23d3dadb6bSPhilippe Houdoin 
24d068515fSAxel Dörfler #define NFDBITS		(sizeof(fd_mask) * 8)	/* bits per mask */
25d3dadb6bSPhilippe Houdoin 
26d3dadb6bSPhilippe Houdoin typedef struct fd_set {
27e5f515c3SAugustin Cavalier 	fd_mask bits[((FD_SETSIZE) + ((NFDBITS) - 1)) / (NFDBITS)];
28d3dadb6bSPhilippe Houdoin } fd_set;
29d3dadb6bSPhilippe Houdoin 
30493130b3SAxel Dörfler #define _FD_BITSINDEX(fd) ((fd) / NFDBITS)
31493130b3SAxel Dörfler #define _FD_BIT(fd) (1L << ((fd) % NFDBITS))
32d3dadb6bSPhilippe Houdoin 
33493130b3SAxel Dörfler #define FD_ZERO(set) memset((set), 0, sizeof(fd_set))
34493130b3SAxel Dörfler #define FD_SET(fd, set) ((set)->bits[_FD_BITSINDEX(fd)] |= _FD_BIT(fd))
35493130b3SAxel Dörfler #define FD_CLR(fd, set) ((set)->bits[_FD_BITSINDEX(fd)] &= ~_FD_BIT(fd))
36493130b3SAxel Dörfler #define FD_ISSET(fd, set) ((set)->bits[_FD_BITSINDEX(fd)] & _FD_BIT(fd))
37b4fd412bSAxel Dörfler #define FD_COPY(source, target) (*(target) = *(source))
38d3dadb6bSPhilippe Houdoin 
39e5f515c3SAugustin Cavalier 
40493130b3SAxel Dörfler #ifdef __cplusplus
41d068515fSAxel Dörfler extern "C" {
42493130b3SAxel Dörfler #endif
43d068515fSAxel Dörfler 
442dc3fee5SAxel Dörfler extern int pselect(int numBits, struct fd_set *readBits, struct fd_set *writeBits,
452dc3fee5SAxel Dörfler 			struct fd_set *errorBits, const struct timespec *timeout, const sigset_t *sigMask);
462dc3fee5SAxel Dörfler extern int select(int numBits, struct fd_set *readBits, struct fd_set *writeBits,
472dc3fee5SAxel Dörfler 			struct fd_set *errorBits, struct timeval *timeout);
48d3dadb6bSPhilippe Houdoin 
49d068515fSAxel Dörfler #ifdef __cplusplus
50d068515fSAxel Dörfler }
51d068515fSAxel Dörfler #endif
52d068515fSAxel Dörfler 
53*7ee61327SAugustin Cavalier 
54d3dadb6bSPhilippe Houdoin #endif	/* _SYS_SELECT_H */
55