2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: poll.c,v 1.2 2007-11-09 22:08:14 adam Exp $
9 * \brief Select, poll wrapper
17 #include <yaz/xmalloc.h>
21 #include <sys/types.h>
30 #include <sys/select.h>
34 #define FD_SETSIZE 512
40 Note that yaz_poll_select is limited as to how many file
41 descriptors it can multiplex due to its use of select() which in
42 turn uses the statically defined fd_set type to be a bitmap of the
43 file descriptors to check. On Ubuntu 6.06 (and almost certainly on
44 Debian, and probably on all Linuxes, and maybe all Unixes) this is
45 by default set to 1024 (though it may be possible to override this
46 using a #define before including <sys/select.h> -- I've not tried
47 this). 1024 file descriptors is a lot, but not enough in all
48 cases, e.g. when running IRSpy on a large target database. So you
49 should ensure that YAZ uses ZOOM_yaz_poll_poll() when possible.
51 static int yaz_poll_select(struct yaz_poll_fd *fds, int num_fds,
55 fd_set input, output, except;
64 for (i = 0; i < num_fds; i++)
66 enum yaz_poll_mask mask = fds[i].input_mask;
69 if (mask & yaz_poll_read)
71 if (mask & yaz_poll_write)
73 if (mask & yaz_poll_except)
79 tv.tv_usec = nsec / 1000;
81 while ((r = select(max_fd+1, &input, &output, &except,
82 (sec == -1 ? 0 : &tv))) < 0 && errno == EINTR)
86 for (i = 0; i < num_fds; i++)
88 enum yaz_poll_mask mask = 0;
91 mask += yaz_poll_timeout;
94 if (FD_ISSET(fd, &input))
95 mask += yaz_poll_read;
96 if (FD_ISSET(fd, &output))
97 mask += yaz_poll_write;
98 if (FD_ISSET(fd, &except))
99 mask += yaz_poll_except;
101 fds[i].output_mask = mask;
107 static int yaz_poll_poll(struct yaz_poll_fd *fds, int num_fds,
111 struct pollfd *pollfds = (struct pollfd *)
112 xmalloc(num_fds * sizeof *pollfds);
116 for (i = 0; i < num_fds; i++)
118 enum yaz_poll_mask mask = fds[i].input_mask;
120 short poll_events = 0;
122 if (mask & yaz_poll_read)
123 poll_events += POLLIN;
124 if (mask & yaz_poll_write)
125 poll_events += POLLOUT;
126 if (mask & yaz_poll_except)
127 poll_events += POLLERR;
129 pollfds[i].events = poll_events;
130 pollfds[i].revents = 0;
132 while ((r = poll(pollfds, num_fds,
133 (sec == -1 ? -1 : sec*1000 + nsec/1000000))) < 0
140 for (i = 0; i < num_fds; i++)
142 enum yaz_poll_mask mask = 0;
144 mask += yaz_poll_timeout;
147 if (pollfds[i].revents & POLLIN)
148 mask += yaz_poll_read;
149 if (pollfds[i].revents & POLLOUT)
150 mask += yaz_poll_write;
151 if (pollfds[i].revents & POLLERR)
152 mask += yaz_poll_except;
154 fds[i].output_mask = mask;
162 int yaz_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
164 #if YAZ_HAVE_SYS_POLL_H
165 return yaz_poll_poll(fds, num_fds, sec, nsec);
167 return yaz_poll_select(fds, num_fds, sec, nsec);
174 * indent-tabs-mode: nil
176 * vim: shiftwidth=4 tabstop=8 expandtab