The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 28 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 29 | #ifndef _SYS_SELECT_H_ |
| 30 | #define _SYS_SELECT_H_ |
| 31 | |
Yabin Cui | db49903 | 2014-12-09 20:15:48 -0800 | [diff] [blame] | 32 | #include <sys/cdefs.h> |
| 33 | #include <sys/types.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 35 | #include <linux/time.h> |
| 36 | #include <signal.h> |
| 37 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | __BEGIN_DECLS |
| 39 | |
Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 40 | typedef unsigned long fd_mask; |
| 41 | |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 42 | #define FD_SETSIZE 1024 |
Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 43 | #define NFDBITS (8 * sizeof(fd_mask)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 44 | |
Elliott Hughes | d3e64a3 | 2013-09-30 17:41:08 -0700 | [diff] [blame] | 45 | typedef struct { |
Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 46 | fd_mask fds_bits[FD_SETSIZE/NFDBITS]; |
Elliott Hughes | d3e64a3 | 2013-09-30 17:41:08 -0700 | [diff] [blame] | 47 | } fd_set; |
| 48 | |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 49 | #define __FDELT(fd) ((fd) / NFDBITS) |
| 50 | #define __FDMASK(fd) (1UL << ((fd) % NFDBITS)) |
Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 51 | #define __FDS_BITS(type,set) (__BIONIC_CAST(static_cast, type, set)->fds_bits) |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 52 | |
Elliott Hughes | 5038b19 | 2015-01-28 21:02:34 -0800 | [diff] [blame] | 53 | /* Inline loop so we don't have to declare memset. */ |
| 54 | #define FD_ZERO(set) \ |
| 55 | do { \ |
| 56 | size_t __i; \ |
Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 57 | for (__i = 0; __i < sizeof(fd_set)/sizeof(fd_mask); ++__i) { \ |
Elliott Hughes | 5038b19 | 2015-01-28 21:02:34 -0800 | [diff] [blame] | 58 | (set)->fds_bits[__i] = 0; \ |
| 59 | } \ |
| 60 | } while (0) |
Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 62 | void __FD_CLR_chk(int, fd_set*, size_t) __INTRODUCED_IN(21); |
| 63 | void __FD_SET_chk(int, fd_set*, size_t) __INTRODUCED_IN(21); |
Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 64 | int __FD_ISSET_chk(int, const fd_set*, size_t) __INTRODUCED_IN(21); |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 65 | |
Andreas Gampe | 00a6d5f | 2018-04-13 13:52:10 -0700 | [diff] [blame] | 66 | #define __FD_CLR(fd, set) (__FDS_BITS(fd_set*,set)[__FDELT(fd)] &= ~__FDMASK(fd)) |
| 67 | #define __FD_SET(fd, set) (__FDS_BITS(fd_set*,set)[__FDELT(fd)] |= __FDMASK(fd)) |
| 68 | #define __FD_ISSET(fd, set) ((__FDS_BITS(const fd_set*,set)[__FDELT(fd)] & __FDMASK(fd)) != 0) |
| 69 | |
| 70 | |
| 71 | #if __ANDROID_API__ >= __ANDROID_API_L__ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 72 | #define FD_CLR(fd, set) __FD_CLR_chk(fd, set, __bos(set)) |
| 73 | #define FD_SET(fd, set) __FD_SET_chk(fd, set, __bos(set)) |
| 74 | #define FD_ISSET(fd, set) __FD_ISSET_chk(fd, set, __bos(set)) |
Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 75 | #else |
Andreas Gampe | 00a6d5f | 2018-04-13 13:52:10 -0700 | [diff] [blame] | 76 | #define FD_CLR(fd, set) __FD_CLR(fd, set) |
| 77 | #define FD_SET(fd, set) __FD_SET(fd, set) |
| 78 | #define FD_ISSET(fd, set) __FD_ISSET(fd, set) |
| 79 | #endif /* __ANDROID_API >= 21 */ |
Nick Kralevich | 90201d5 | 2013-10-02 16:11:30 -0700 | [diff] [blame] | 80 | |
Elliott Hughes | ff26a16 | 2017-08-17 22:34:21 +0000 | [diff] [blame] | 81 | int select(int __fd_count, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, struct timeval* __timeout); |
| 82 | int pselect(int __fd_count, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, const struct timespec* __timeout, const sigset_t* __mask); |
Logan Chien | 25bcf59 | 2018-10-23 21:32:37 +0800 | [diff] [blame] | 83 | int pselect64(int __fd_count, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, const struct timespec* __timeout, const sigset64_t* __mask) __INTRODUCED_IN(28); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 | |
| 85 | __END_DECLS |
| 86 | |
Elliott Hughes | ff26a16 | 2017-08-17 22:34:21 +0000 | [diff] [blame] | 87 | #endif |