xref: /openbsd/sys/sys/select.h (revision 176944c6)
1 /*	$OpenBSD: select.h,v 1.17 2016/09/12 19:41:20 guenther Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)select.h	8.2 (Berkeley) 1/4/94
32  */
33 
34 #ifndef _SYS_SELECT_H_
35 #define	_SYS_SELECT_H_
36 
37 #include <sys/types.h>
38 
39 #ifndef _TIMEVAL_DECLARED
40 #define _TIMEVAL_DECLARED
41 struct timeval {
42 	time_t		tv_sec;		/* seconds */
43 	suseconds_t	tv_usec;	/* and microseconds */
44 };
45 #endif
46 
47 #ifndef _TIMESPEC_DECLARED
48 #define _TIMESPEC_DECLARED
49 struct timespec {
50 	time_t	tv_sec;		/* seconds */
51 	long	tv_nsec;	/* and nanoseconds */
52 };
53 #endif
54 
55 /*
56  * Select uses bit masks of file descriptors in longs.  These macros
57  * manipulate such bit fields (the filesystem macros use chars).
58  * FD_SETSIZE may be defined by the user, but the default here should
59  * be enough for most uses.
60  */
61 #ifndef	FD_SETSIZE
62 #define	FD_SETSIZE	1024
63 #endif
64 
65 /*
66  * We don't want to pollute the namespace with select(2) internals.
67  * Non-underscore versions are exposed later #if __BSD_VISIBLE
68  */
69 #define	__NBBY	8				/* number of bits in a byte */
70 typedef uint32_t __fd_mask;
71 #define __NFDBITS ((unsigned)(sizeof(__fd_mask) * __NBBY)) /* bits per mask */
72 #define	__howmany(x, y)	(((x) + ((y) - 1)) / (y))
73 
74 typedef	struct fd_set {
75 	__fd_mask fds_bits[__howmany(FD_SETSIZE, __NFDBITS)];
76 } fd_set;
77 
78 static __inline void
__fd_set(int fd,fd_set * p)79 __fd_set(int fd, fd_set *p)
80 {
81 	p->fds_bits[fd / __NFDBITS] |= (1U << (fd % __NFDBITS));
82 }
83 #define FD_SET(n, p)	__fd_set((n), (p))
84 
85 static __inline void
__fd_clr(int fd,fd_set * p)86 __fd_clr(int fd, fd_set *p)
87 {
88 	p->fds_bits[fd / __NFDBITS] &= ~(1U << (fd % __NFDBITS));
89 }
90 #define FD_CLR(n, p)	__fd_clr((n), (p))
91 
92 static __inline int
__fd_isset(int fd,const fd_set * p)93 __fd_isset(int fd, const fd_set *p)
94 {
95 	return (p->fds_bits[fd / __NFDBITS] & (1U << (fd % __NFDBITS)));
96 }
97 #define FD_ISSET(n, p)	__fd_isset((n), (p))
98 
99 #if __BSD_VISIBLE
100 #define	FD_COPY(f, t)	(void)(*(t) = *(f))
101 #endif
102 #define	FD_ZERO(p) do {					\
103 	fd_set *_p = (p);				\
104 	__size_t _n = __howmany(FD_SETSIZE, __NFDBITS);	\
105 							\
106 	while (_n > 0)					\
107 		_p->fds_bits[--_n] = 0;			\
108 } while (0)
109 
110 #if __BSD_VISIBLE
111 #define	NBBY	__NBBY
112 #define fd_mask	__fd_mask
113 #define NFDBITS	__NFDBITS
114 #ifndef howmany
115 #define howmany(x, y)	__howmany(x, y)
116 #endif
117 #endif /* __BSD_VISIBLE */
118 
119 #ifndef _KERNEL
120 #ifndef _SIGSET_T_DEFINED_
121 #define _SIGSET_T_DEFINED_
122 typedef unsigned int sigset_t;
123 #endif
124 
125 #ifndef _SELECT_DEFINED_
126 #define _SELECT_DEFINED_
127 __BEGIN_DECLS
128 int	select(int, fd_set * __restrict, fd_set * __restrict,
129 	    fd_set * __restrict, struct timeval * __restrict);
130 int	pselect(int, fd_set * __restrict, fd_set * __restrict,
131 	    fd_set * __restrict, const struct timespec * __restrict,
132 	    const sigset_t * __restrict);
133 __END_DECLS
134 #endif
135 #endif /* !_KERNEL */
136 
137 #endif /* !_SYS_SELECT_H_ */
138