2 * poll2select - convert poll() calls to select() calls
3 * Copyright 2005 Ludovico Gardenghi
4 * Licensed under the GPLv2
7 #include <sys/select.h>
16 #include <vdecommon.h>
19 #define MAX(x,y) ((x)>(y)?(x):(y))
22 static int prepare_select(struct pollfd
*ufds
, nfds_t nfds
, int timeout
,
23 struct timeval
**pstimeout
, int *maxfdp1
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*efds
)
26 struct pollfd
*currfd
;
27 struct timeval
*stimeout
= *pstimeout
;
30 * Conversion of information about file descriptors
35 if ((nfds
> 0) && (ufds
== NULL
))
41 for (i
= 0; i
< nfds
; i
++)
51 if (currfd
->events
& POLLIN
)
52 FD_SET(currfd
->fd
, rfds
);
53 if (currfd
->events
& POLLOUT
)
54 FD_SET(currfd
->fd
, wfds
);
55 if (currfd
->events
& POLLPRI
)
56 FD_SET(currfd
->fd
, efds
);
58 *maxfdp1
= MAX(*maxfdp1
, currfd
->fd
);
64 * Conversion of information about timeout
75 stimeout
->tv_usec
= 0;
84 stimeout
->tv_sec
= timeout
/ 1000;
85 stimeout
->tv_usec
= (timeout
% 1000) * 1000;
87 else // if (timeout < 0)
93 static int convert_results(struct pollfd
*ufds
, int nfds
,
94 fd_set
*rfds
, fd_set
*wfds
, fd_set
*efds
)
97 struct pollfd
*currfd
;
100 for (i
= 0; i
< nfds
; i
++)
106 if (FD_ISSET(currfd
->fd
, rfds
))
107 currfd
->revents
|= POLLIN
;
108 if (FD_ISSET(currfd
->fd
, wfds
))
109 currfd
->revents
|= POLLOUT
;
110 if (FD_ISSET(currfd
->fd
, efds
))
111 currfd
->revents
|= POLLPRI
;
113 if (currfd
->revents
!= 0)
120 int vde_poll(struct pollfd
*ufds
, nfds_t nfds
, int timeout
)
122 fd_set rfds
, wfds
, efds
;
123 struct timeval stimeout
;
124 struct timeval
*pstimeout
= &stimeout
;
126 int pretval
, sretval
, tretval
;
132 tretval
= prepare_select(ufds
, nfds
, timeout
, &pstimeout
, &maxfdp1
, &rfds
, &wfds
, &efds
);
136 sretval
= select(maxfdp1
, &rfds
, &wfds
, &efds
, pstimeout
);
140 pretval
= convert_results(ufds
, nfds
, &rfds
, &wfds
, &efds
);