2 // MacOS X does not implement poll(). Therefore, this replacement
3 // is required. It uses select().
12 #define FD_SETSIZE OPEN_MAX
13 #include <sys/types.h>
18 typedef struct pollfd
{
19 int fd
; /* file desc to poll */
20 short events
; /* events of interest on fd */
21 short revents
; /* events that occurred on fd */
27 #define POLLOUT 0x0004
28 #define POLLERR 0x0008
31 #define POLLNORM POLLIN
32 #define POLLPRI POLLIN
33 #define POLLRDNORM POLLIN
34 #define POLLRDBAND POLLIN
35 #define POLLWRNORM POLLOUT
36 #define POLLWRBAND POLLOUT
39 #define POLLHUP 0x0010
40 #define POLLNVAL 0x0020
42 inline int poll(struct pollfd
*pollSet
, int pollCount
, int pollTimeout
)
46 fd_set readFDs
, writeFDs
, exceptFDs
;
47 fd_set
*readp
, *writep
, *exceptp
;
48 struct pollfd
*pollEnd
, *p
;
63 pollEnd
= pollSet
+ pollCount
;
72 // Find the biggest fd in the poll set
74 for (p
= pollSet
; p
< pollEnd
; p
++)
80 if (maxFD
>= FD_SETSIZE
)
82 // At least one fd is too big
87 // Transcribe flags from the poll set to the fd sets
88 for (p
= pollSet
; p
< pollEnd
; p
++)
92 // Negative fd checks nothing and always reports zero
96 if (p
->events
& POLLIN
)
98 if (p
->events
& POLLOUT
)
99 FD_SET(p
->fd
, writep
);
101 FD_SET(p
->fd
, exceptp
);
102 // POLLERR is never set coming in; poll() always reports errors
103 // But don't report if we're not listening to anything at all.
108 // poll timeout is in milliseconds. Convert to struct timeval.
109 // poll timeout == -1 : wait forever : select timeout of NULL
110 // poll timeout == 0 : return immediately : select timeout of zero
111 if (pollTimeout
>= 0)
113 tv
.tv_sec
= pollTimeout
/ 1000;
114 tv
.tv_usec
= (pollTimeout
% 1000) * 1000;
122 selected
= select(maxFD
+1, readp
, writep
, exceptp
, tvp
);
126 // Error during select
129 else if (selected
> 0)
131 // Select found something
132 // Transcribe result from fd sets to poll set.
133 // Also count the number of selected fds. poll returns the
134 // number of ready fds; select returns the number of bits set.
136 for (p
= pollSet
; p
< pollEnd
; p
++)
140 // Negative fd always reports zero
144 if ( (p
->events
& POLLIN
) && FD_ISSET(p
->fd
, readp
) )
145 p
->revents
|= POLLIN
;
146 if ( (p
->events
& POLLOUT
) && FD_ISSET(p
->fd
, writep
) )
147 p
->revents
|= POLLOUT
;
148 if ( (p
->events
!= 0) && FD_ISSET(p
->fd
, exceptp
) )
149 p
->revents
|= POLLERR
;
159 // selected == 0, select timed out before anything happened
160 // Clear all result bits and return zero.
161 for (p
= pollSet
; p
< pollEnd
; p
++)