1/*
2 * Copyright 2002-2012 Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _SYS_SELECT_H
6#define _SYS_SELECT_H
7
8
9#include <config/types.h>
10
11#include <sys/time.h>
12#include <signal.h>
13
14
15/* If FD_SET is already defined, only the select() prototype is
16 * exported in this header.
17 */
18#ifndef FD_SET
19
20/* You can define your own FDSETSIZE if you need more bits - but
21 * it should be enough for most uses.
22 */
23#ifndef FD_SETSIZE
24#	define FD_SETSIZE 1024
25#endif
26
27typedef __haiku_uint32 fd_mask;
28
29#ifndef _howmany
30#	define _howmany(x, y) (((x) + ((y) - 1)) / (y))
31#endif
32
33#ifndef howmany
34#	define howmany(x, y) (((x) + ((y) - 1)) / (y))
35#endif
36
37#define NFDBITS (sizeof(fd_mask) * 8)	/* bits per mask */
38
39typedef struct fd_set {
40	fd_mask bits[_howmany(FD_SETSIZE, NFDBITS)];
41} fd_set;
42
43#define _FD_BITSINDEX(fd) ((fd) / NFDBITS)
44#define _FD_BIT(fd) (1L << ((fd) % NFDBITS))
45
46/* FD_ZERO uses memset */
47#include <string.h>
48
49#define FD_ZERO(set) memset((set), 0, sizeof(fd_set))
50#define FD_SET(fd, set) ((set)->bits[_FD_BITSINDEX(fd)] |= _FD_BIT(fd))
51#define FD_CLR(fd, set) ((set)->bits[_FD_BITSINDEX(fd)] &= ~_FD_BIT(fd))
52#define FD_ISSET(fd, set) ((set)->bits[_FD_BITSINDEX(fd)] & _FD_BIT(fd))
53#define FD_COPY(source, target) (*(target) = *(source))
54
55#endif	/* FD_SET */
56
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61extern int pselect(int numBits, struct fd_set *readBits, struct fd_set *writeBits,
62			struct fd_set *errorBits, const struct timespec *timeout, const sigset_t *sigMask);
63extern int select(int numBits, struct fd_set *readBits, struct fd_set *writeBits,
64			struct fd_set *errorBits, struct timeval *timeout);
65
66#ifdef __cplusplus
67}
68#endif
69
70#endif	/* _SYS_SELECT_H */
71