Discussion:
Win32 poll() Equivalent
(too old to reply)
John
2003-09-29 20:46:48 UTC
Permalink
Hi,

Is there a Win32 equivalent to the UNIX poll() function? I have an
integer file descriptor that is handed to me by an API, and I need to
block on it until data is written, but I can't read the fd myself
(i.e. I can't move the file pointer myself).

Any guidance here would be very much appreciated.

Thanks,
John
Gisle Vanem
2003-09-29 22:40:46 UTC
Permalink
Post by John
Is there a Win32 equivalent to the UNIX poll() function?
It can be emulated using select(). Here's one I once
wrote:

poll.h:
#ifndef __POLL_H
#define __POLL_H

#define POLLIN 0x0001
#define POLLPRI 0x0002 /* not used */
#define POLLOUT 0x0004
#define POLLERR 0x0008
#define POLLHUP 0x0010 /* not used */
#define POLLNVAL 0x0020 /* not used */

struct pollfd {
int fd;
int events; /* in param: what to poll for */
int revents; /* out param: what events occured */
};

extern int poll (struct pollfd *p, int num, int timeout);

#endif /* __POLL_H */

poll.c:
#include "OS/windows/ec_mingw_poll.h"

int poll (struct pollfd *p, int num, int timeout)
{
struct timeval tv;
fd_set read, write, except;
int i, n, ret;

FD_ZERO (&read);
FD_ZERO (&write);
FD_ZERO (&except);

n = -1;
for (i = 0; i < num; i++)
{
if (p[i].fd < 0)
continue;
if (p[i].events & POLLIN)
FD_SET (p[i].fd, &read);
if (p[i].events & POLLOUT)
FD_SET (p[i].fd, &write);
if (p[i].events & POLLERR)
FD_SET (p[i].fd, &except);
if (p[i].fd > n)
n = p[i].fd;
}

if (n == -1)
return (0);

if (timeout < 0)
ret = select (n+1, &read, &write, &except, NULL);
else
{
tv.tv_sec = timeout / 1000;
tv.tv_usec = 1000 * (timeout % 1000);
ret = select (n+1, &read, &write, &except, &tv);
}

for (i = 0; ret >= 0 && i < num; i++)
{
p[i].revents = 0;
if (FD_ISSET (p[i].fd, &read))
p[i].revents |= POLLIN;
if (FD_ISSET (p[i].fd, &write))
p[i].revents |= POLLOUT;
if (FD_ISSET (p[i].fd, &except))
p[i].revents |= POLLERR;
}
return (ret);
}

Loading...