15adca30aSAxel Dörfler /* 2*2222d055SIngo Weinhold * Copyright 2002-2009, 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 9*2222d055SIngo Weinhold #include <config/types.h> 10*2222d055SIngo Weinhold 11493130b3SAxel Dörfler #include <sys/time.h> 12d068515fSAxel Dörfler #include <signal.h> 13493130b3SAxel Dörfler 14493130b3SAxel Dörfler 15493130b3SAxel Dörfler /* If FD_SET is already defined, only the select() prototype is 16493130b3SAxel Dörfler * exported in this header. 17493130b3SAxel Dörfler */ 18493130b3SAxel Dörfler #ifndef FD_SET 19493130b3SAxel Dörfler 20493130b3SAxel Dörfler /* You can define your own FDSETSIZE if you need more bits - but 21493130b3SAxel Dörfler * it should be enough for most uses. 22493130b3SAxel Dörfler */ 23d3dadb6bSPhilippe Houdoin #ifndef FD_SETSIZE 24d3dadb6bSPhilippe Houdoin # define FD_SETSIZE 1024 25493130b3SAxel Dörfler #endif 26d3dadb6bSPhilippe Houdoin 27*2222d055SIngo Weinhold typedef __haiku_uint32 fd_mask; 28d3dadb6bSPhilippe Houdoin 29d068515fSAxel Dörfler #ifndef _howmany 30d068515fSAxel Dörfler # define _howmany(x, y) (((x) + ((y) - 1)) / (y)) 31d3dadb6bSPhilippe Houdoin #endif 32d3dadb6bSPhilippe Houdoin 330fddf2f0SIngo Weinhold #ifndef howmany 340fddf2f0SIngo Weinhold # define howmany(x, y) (((x) + ((y) - 1)) / (y)) 350fddf2f0SIngo Weinhold #endif 360fddf2f0SIngo Weinhold 37d068515fSAxel Dörfler #define NFDBITS (sizeof(fd_mask) * 8) /* bits per mask */ 38d3dadb6bSPhilippe Houdoin 39d3dadb6bSPhilippe Houdoin typedef struct fd_set { 40d068515fSAxel Dörfler fd_mask bits[_howmany(FD_SETSIZE, NFDBITS)]; 41d3dadb6bSPhilippe Houdoin } fd_set; 42d3dadb6bSPhilippe Houdoin 43493130b3SAxel Dörfler #define _FD_BITSINDEX(fd) ((fd) / NFDBITS) 44493130b3SAxel Dörfler #define _FD_BIT(fd) (1L << ((fd) % NFDBITS)) 45d3dadb6bSPhilippe Houdoin 46493130b3SAxel Dörfler #define FD_ZERO(set) memset((set), 0, sizeof(fd_set)) 47493130b3SAxel Dörfler #define FD_SET(fd, set) ((set)->bits[_FD_BITSINDEX(fd)] |= _FD_BIT(fd)) 48493130b3SAxel Dörfler #define FD_CLR(fd, set) ((set)->bits[_FD_BITSINDEX(fd)] &= ~_FD_BIT(fd)) 49493130b3SAxel Dörfler #define FD_ISSET(fd, set) ((set)->bits[_FD_BITSINDEX(fd)] & _FD_BIT(fd)) 50b4fd412bSAxel Dörfler #define FD_COPY(source, target) (*(target) = *(source)) 51d3dadb6bSPhilippe Houdoin 52493130b3SAxel Dörfler #endif /* FD_SET */ 53493130b3SAxel Dörfler 54493130b3SAxel Dörfler #ifdef __cplusplus 55d068515fSAxel Dörfler extern "C" { 56493130b3SAxel Dörfler #endif 57d068515fSAxel Dörfler 582dc3fee5SAxel Dörfler extern int pselect(int numBits, struct fd_set *readBits, struct fd_set *writeBits, 592dc3fee5SAxel Dörfler struct fd_set *errorBits, const struct timespec *timeout, const sigset_t *sigMask); 602dc3fee5SAxel Dörfler extern int select(int numBits, struct fd_set *readBits, struct fd_set *writeBits, 612dc3fee5SAxel Dörfler struct fd_set *errorBits, struct timeval *timeout); 62d3dadb6bSPhilippe Houdoin 63d068515fSAxel Dörfler #ifdef __cplusplus 64d068515fSAxel Dörfler } 65d068515fSAxel Dörfler #endif 66d068515fSAxel Dörfler 67d3dadb6bSPhilippe Houdoin #endif /* _SYS_SELECT_H */ 68