11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1992, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2950477Speter * $FreeBSD$
301541Srgrimes */
311541Srgrimes
321541Srgrimes#ifndef _SYS_SELECT_H_
331541Srgrimes#define	_SYS_SELECT_H_
341541Srgrimes
3598272Swollman#include <sys/cdefs.h>
3698272Swollman#include <sys/_types.h>
3798272Swollman
3898302Swollman#include <sys/_sigset.h>
39108478Smike#include <sys/_timeval.h>
4098272Swollman#include <sys/timespec.h>
4198272Swollman
42103867Smiketypedef	unsigned long	__fd_mask;
43103867Smike#if __BSD_VISIBLE
44103867Smiketypedef	__fd_mask	fd_mask;
45103867Smike#endif
46103867Smike
47104504Smike#ifndef _SIGSET_T_DECLARED
48104504Smike#define	_SIGSET_T_DECLARED
49104504Smiketypedef	__sigset_t	sigset_t;
50104504Smike#endif
51104504Smike
52103867Smike/*
53103867Smike * Select uses bit masks of file descriptors in longs.  These macros
54103867Smike * manipulate such bit fields (the filesystem macros use chars).
55103867Smike * FD_SETSIZE may be defined by the user, but the default here should
56103867Smike * be enough for most uses.
57103867Smike */
58103867Smike#ifndef	FD_SETSIZE
59103867Smike#define	FD_SETSIZE	1024U
60103867Smike#endif
61103867Smike
62103867Smike#define	_NFDBITS	(sizeof(__fd_mask) * 8)	/* bits per mask */
63103867Smike#if __BSD_VISIBLE
64103867Smike#define	NFDBITS		_NFDBITS
65103867Smike#endif
66103867Smike
67103867Smike#ifndef _howmany
68103867Smike#define	_howmany(x, y)	(((x) + ((y) - 1)) / (y))
69103867Smike#endif
70103867Smike
71103867Smiketypedef	struct fd_set {
72107019Smike	__fd_mask	__fds_bits[_howmany(FD_SETSIZE, _NFDBITS)];
73103867Smike} fd_set;
74107019Smike#if __BSD_VISIBLE
75107019Smike#define	fds_bits	__fds_bits
76107019Smike#endif
77103867Smike
78107019Smike#define	__fdset_mask(n)	((__fd_mask)1 << ((n) % _NFDBITS))
79107377Smike#define	FD_CLR(n, p)	((p)->__fds_bits[(n)/_NFDBITS] &= ~__fdset_mask(n))
80103867Smike#if __BSD_VISIBLE
81107019Smike#define	FD_COPY(f, t)	(void)(*(t) = *(f))
82103867Smike#endif
83154090Smarcel#define	FD_ISSET(n, p)	(((p)->__fds_bits[(n)/_NFDBITS] & __fdset_mask(n)) != 0)
84107377Smike#define	FD_SET(n, p)	((p)->__fds_bits[(n)/_NFDBITS] |= __fdset_mask(n))
85107019Smike#define	FD_ZERO(p) do {					\
86107019Smike	fd_set *_p;					\
87107019Smike	__size_t _n;					\
88107019Smike							\
89107019Smike	_p = (p);					\
90107019Smike	_n = _howmany(FD_SETSIZE, _NFDBITS);		\
91107019Smike	while (_n > 0)					\
92107019Smike		_p->__fds_bits[--_n] = 0;		\
93107019Smike} while (0)
94103867Smike
9598272Swollman#ifndef _KERNEL
96103867Smike
9798272Swollman__BEGIN_DECLS
9898272Swollmanint pselect(int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict,
9998302Swollman	const struct timespec *__restrict, const sigset_t *__restrict);
100103867Smike#ifndef _SELECT_DECLARED
101103867Smike#define	_SELECT_DECLARED
102103867Smike/* XXX missing restrict type-qualifier */
103103867Smikeint	select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
104103867Smike#endif
10598272Swollman__END_DECLS
10698272Swollman#endif /* !_KERNEL */
10798272Swollman
10870650Swollman#endif /* _SYS_SELECT_H_ */
109