1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 * \brief Select, poll wrapper
15 #include <yaz/xmalloc.h>
19 #include <sys/types.h>
28 #include <sys/select.h>
32 #define FD_SETSIZE 512
38 Note that yaz_poll_select is limited as to how many file
39 descriptors it can multiplex due to its use of select() which in
40 turn uses the statically defined fd_set type to be a bitmap of the
41 file descriptors to check. On Ubuntu 6.06 (and almost certainly on
42 Debian, and probably on all Linuxes, and maybe all Unixes) this is
43 by default set to 1024 (though it may be possible to override this
44 using a #define before including <sys/select.h> -- I've not tried
45 this). 1024 file descriptors is a lot, but not enough in all
46 cases, e.g. when running IRSpy on a large target database. So you
47 should ensure that YAZ uses ZOOM_yaz_poll_poll() when possible.
49 int yaz_poll_select(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
52 fd_set input, output, except;
61 for (i = 0; i < num_fds; i++)
63 enum yaz_poll_mask mask = fds[i].input_mask;
66 if (mask & yaz_poll_read)
68 if (mask & yaz_poll_write)
70 if (mask & yaz_poll_except)
76 tv.tv_usec = nsec / 1000;
78 r = select(max_fd+1, &input, &output, &except, (sec == -1 ? 0 : &tv));
81 for (i = 0; i < num_fds; i++)
83 enum yaz_poll_mask mask = yaz_poll_none;
86 yaz_poll_add(mask, yaz_poll_timeout);
89 if (FD_ISSET(fd, &input))
90 yaz_poll_add(mask, yaz_poll_read);
91 if (FD_ISSET(fd, &output))
92 yaz_poll_add(mask, yaz_poll_write);
93 if (FD_ISSET(fd, &except))
94 yaz_poll_add(mask, yaz_poll_except);
96 fds[i].output_mask = mask;
103 int yaz_poll_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
106 struct pollfd *pollfds = (struct pollfd *)
107 xmalloc(num_fds * sizeof *pollfds);
111 for (i = 0; i < num_fds; i++)
113 enum yaz_poll_mask mask = fds[i].input_mask;
115 short poll_events = 0;
117 if (mask & yaz_poll_read)
118 poll_events += POLLIN;
119 if (mask & yaz_poll_write)
120 poll_events += POLLOUT;
121 if (mask & yaz_poll_except)
122 poll_events += POLLERR;
124 pollfds[i].events = poll_events;
125 pollfds[i].revents = 0;
127 r = poll(pollfds, num_fds, sec == -1 ? -1 : sec*1000 + nsec/1000000);
130 for (i = 0; i < num_fds; i++)
132 enum yaz_poll_mask mask = yaz_poll_none;
134 yaz_poll_add(mask, yaz_poll_timeout);
137 if (pollfds[i].revents & POLLIN)
138 yaz_poll_add(mask, yaz_poll_read);
139 if (pollfds[i].revents & POLLOUT)
140 yaz_poll_add(mask, yaz_poll_write);
141 if (pollfds[i].revents & ~(POLLIN | POLLOUT))
143 yaz_poll_add(mask, yaz_poll_except);
146 fds[i].output_mask = mask;
154 int yaz_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
156 #if YAZ_HAVE_SYS_POLL_H
157 return yaz_poll_poll(fds, num_fds, sec, nsec);
159 return yaz_poll_select(fds, num_fds, sec, nsec);
166 * indent-tabs-mode: nil
168 * vim: shiftwidth=4 tabstop=8 expandtab