xref: /openbsd/sys/sys/select.h (revision 176944c6)
1*176944c6Sguenther /*	$OpenBSD: select.h,v 1.17 2016/09/12 19:41:20 guenther Exp $	*/
2df930be7Sderaadt 
3df930be7Sderaadt /*-
4df930be7Sderaadt  * Copyright (c) 1992, 1993
5df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
6df930be7Sderaadt  *
7df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
8df930be7Sderaadt  * modification, are permitted provided that the following conditions
9df930be7Sderaadt  * are met:
10df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
11df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
12df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
13df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
14df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
1529295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
16df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
17df930be7Sderaadt  *    without specific prior written permission.
18df930be7Sderaadt  *
19df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df930be7Sderaadt  * SUCH DAMAGE.
30df930be7Sderaadt  *
31df930be7Sderaadt  *	@(#)select.h	8.2 (Berkeley) 1/4/94
32df930be7Sderaadt  */
33df930be7Sderaadt 
34df930be7Sderaadt #ifndef _SYS_SELECT_H_
35df930be7Sderaadt #define	_SYS_SELECT_H_
36df930be7Sderaadt 
37*176944c6Sguenther #include <sys/types.h>
381a12e8a7Sprovos 
39*176944c6Sguenther #ifndef _TIMEVAL_DECLARED
40*176944c6Sguenther #define _TIMEVAL_DECLARED
41*176944c6Sguenther struct timeval {
42*176944c6Sguenther 	time_t		tv_sec;		/* seconds */
43*176944c6Sguenther 	suseconds_t	tv_usec;	/* and microseconds */
44*176944c6Sguenther };
45*176944c6Sguenther #endif
46*176944c6Sguenther 
47*176944c6Sguenther #ifndef _TIMESPEC_DECLARED
48*176944c6Sguenther #define _TIMESPEC_DECLARED
49*176944c6Sguenther struct timespec {
50*176944c6Sguenther 	time_t	tv_sec;		/* seconds */
51*176944c6Sguenther 	long	tv_nsec;	/* and nanoseconds */
52*176944c6Sguenther };
53*176944c6Sguenther #endif
5417132ff2Smatthew 
5517132ff2Smatthew /*
5639539e65Smillert  * Select uses bit masks of file descriptors in longs.  These macros
5739539e65Smillert  * manipulate such bit fields (the filesystem macros use chars).
5839539e65Smillert  * FD_SETSIZE may be defined by the user, but the default here should
5939539e65Smillert  * be enough for most uses.
60df930be7Sderaadt  */
6139539e65Smillert #ifndef	FD_SETSIZE
6239539e65Smillert #define	FD_SETSIZE	1024
63df930be7Sderaadt #endif
64df930be7Sderaadt 
6539539e65Smillert /*
6639539e65Smillert  * We don't want to pollute the namespace with select(2) internals.
6752a0e603Smillert  * Non-underscore versions are exposed later #if __BSD_VISIBLE
6839539e65Smillert  */
6939539e65Smillert #define	__NBBY	8				/* number of bits in a byte */
70a18aab4aSderaadt typedef uint32_t __fd_mask;
71ea9307b8Sotto #define __NFDBITS ((unsigned)(sizeof(__fd_mask) * __NBBY)) /* bits per mask */
7239539e65Smillert #define	__howmany(x, y)	(((x) + ((y) - 1)) / (y))
7339539e65Smillert 
7439539e65Smillert typedef	struct fd_set {
7539539e65Smillert 	__fd_mask fds_bits[__howmany(FD_SETSIZE, __NFDBITS)];
7639539e65Smillert } fd_set;
7739539e65Smillert 
78b8c90d45Sderaadt static __inline void
__fd_set(int fd,fd_set * p)79b8c90d45Sderaadt __fd_set(int fd, fd_set *p)
80b8c90d45Sderaadt {
81b8c90d45Sderaadt 	p->fds_bits[fd / __NFDBITS] |= (1U << (fd % __NFDBITS));
82b8c90d45Sderaadt }
83b8c90d45Sderaadt #define FD_SET(n, p)	__fd_set((n), (p))
84b8c90d45Sderaadt 
85b8c90d45Sderaadt static __inline void
__fd_clr(int fd,fd_set * p)86b8c90d45Sderaadt __fd_clr(int fd, fd_set *p)
87b8c90d45Sderaadt {
88b8c90d45Sderaadt 	p->fds_bits[fd / __NFDBITS] &= ~(1U << (fd % __NFDBITS));
89b8c90d45Sderaadt }
90b8c90d45Sderaadt #define FD_CLR(n, p)	__fd_clr((n), (p))
91b8c90d45Sderaadt 
92b8c90d45Sderaadt static __inline int
__fd_isset(int fd,const fd_set * p)9362df73d3Sjca __fd_isset(int fd, const fd_set *p)
94b8c90d45Sderaadt {
95b8c90d45Sderaadt 	return (p->fds_bits[fd / __NFDBITS] & (1U << (fd % __NFDBITS)));
96b8c90d45Sderaadt }
97b8c90d45Sderaadt #define FD_ISSET(n, p)	__fd_isset((n), (p))
98d98b0f83Snaddy 
99d98b0f83Snaddy #if __BSD_VISIBLE
100d98b0f83Snaddy #define	FD_COPY(f, t)	(void)(*(t) = *(f))
101d98b0f83Snaddy #endif
102d98b0f83Snaddy #define	FD_ZERO(p) do {					\
103d98b0f83Snaddy 	fd_set *_p = (p);				\
104d98b0f83Snaddy 	__size_t _n = __howmany(FD_SETSIZE, __NFDBITS);	\
105d98b0f83Snaddy 							\
106d98b0f83Snaddy 	while (_n > 0)					\
107d98b0f83Snaddy 		_p->fds_bits[--_n] = 0;			\
108d98b0f83Snaddy } while (0)
10939539e65Smillert 
11052a0e603Smillert #if __BSD_VISIBLE
11139539e65Smillert #define	NBBY	__NBBY
11239539e65Smillert #define fd_mask	__fd_mask
11339539e65Smillert #define NFDBITS	__NFDBITS
11439539e65Smillert #ifndef howmany
11539539e65Smillert #define howmany(x, y)	__howmany(x, y)
11639539e65Smillert #endif
11752a0e603Smillert #endif /* __BSD_VISIBLE */
11839539e65Smillert 
11939539e65Smillert #ifndef _KERNEL
12017132ff2Smatthew #ifndef _SIGSET_T_DEFINED_
12117132ff2Smatthew #define _SIGSET_T_DEFINED_
12217132ff2Smatthew typedef unsigned int sigset_t;
12317132ff2Smatthew #endif
12417132ff2Smatthew 
12539539e65Smillert #ifndef _SELECT_DEFINED_
12639539e65Smillert #define _SELECT_DEFINED_
12739539e65Smillert __BEGIN_DECLS
12817132ff2Smatthew int	select(int, fd_set * __restrict, fd_set * __restrict,
12917132ff2Smatthew 	    fd_set * __restrict, struct timeval * __restrict);
13017132ff2Smatthew int	pselect(int, fd_set * __restrict, fd_set * __restrict,
13117132ff2Smatthew 	    fd_set * __restrict, const struct timespec * __restrict,
13217132ff2Smatthew 	    const sigset_t * __restrict);
13339539e65Smillert __END_DECLS
13439539e65Smillert #endif
13539539e65Smillert #endif /* !_KERNEL */
13639539e65Smillert 
137df930be7Sderaadt #endif /* !_SYS_SELECT_H_ */
138