1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
7 * \brief Select, poll wrapper
18 #include <yaz/xmalloc.h>
22 #include <sys/types.h>
31 #include <sys/select.h>
35 #define FD_SETSIZE 512
41 Note that yaz_poll_select is limited as to how many file
42 descriptors it can multiplex due to its use of select() which in
43 turn uses the statically defined fd_set type to be a bitmap of the
44 file descriptors to check. On Ubuntu 6.06 (and almost certainly on
45 Debian, and probably on all Linuxes, and maybe all Unixes) this is
46 by default set to 1024 (though it may be possible to override this
47 using a #define before including <sys/select.h> -- I've not tried
48 this). 1024 file descriptors is a lot, but not enough in all
49 cases, e.g. when running IRSpy on a large target database. So you
50 should ensure that YAZ uses ZOOM_yaz_poll_poll() when possible.
52 int yaz_poll_select(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
55 fd_set input, output, except;
64 for (i = 0; i < num_fds; i++)
66 enum yaz_poll_mask mask = fds[i].input_mask;
73 if (mask & yaz_poll_read)
75 if (mask & yaz_poll_write)
77 if (mask & yaz_poll_except)
83 tv.tv_usec = nsec / 1000;
85 r = select(max_fd+1, &input, &output, &except, (sec == -1 ? 0 : &tv));
88 for (i = 0; i < num_fds; i++)
90 enum yaz_poll_mask mask = yaz_poll_none;
93 yaz_poll_add(mask, yaz_poll_timeout);
95 if (FD_ISSET(fd, &input))
96 yaz_poll_add(mask, yaz_poll_read);
97 if (FD_ISSET(fd, &output))
98 yaz_poll_add(mask, yaz_poll_write);
99 if (FD_ISSET(fd, &except))
100 yaz_poll_add(mask, yaz_poll_except);
102 fds[i].output_mask = mask;
109 int yaz_poll_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
112 struct pollfd *pollfds = (struct pollfd *)
113 xmalloc(num_fds * sizeof *pollfds);
117 for (i = 0; i < num_fds; i++)
119 enum yaz_poll_mask mask = fds[i].input_mask;
121 short poll_events = 0;
123 if (mask & yaz_poll_read)
124 poll_events += POLLIN;
125 if (mask & yaz_poll_write)
126 poll_events += POLLOUT;
127 if (mask & yaz_poll_except)
128 poll_events += POLLERR;
130 pollfds[i].events = poll_events;
131 pollfds[i].revents = 0;
133 r = poll(pollfds, num_fds, sec == -1 ? -1 : sec*1000 + nsec/1000000);
136 for (i = 0; i < num_fds; i++)
138 enum yaz_poll_mask mask = yaz_poll_none;
140 yaz_poll_add(mask, yaz_poll_timeout);
143 if (pollfds[i].revents & POLLIN)
144 yaz_poll_add(mask, yaz_poll_read);
145 if (pollfds[i].revents & POLLOUT)
146 yaz_poll_add(mask, yaz_poll_write);
147 if (pollfds[i].revents & ~(POLLIN | POLLOUT))
149 yaz_poll_add(mask, yaz_poll_except);
152 fds[i].output_mask = mask;
160 int yaz_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
163 return yaz_poll_poll(fds, num_fds, sec, nsec);
165 return yaz_poll_select(fds, num_fds, sec, nsec);
172 * c-file-style: "Stroustrup"
173 * indent-tabs-mode: nil
175 * vim: shiftwidth=4 tabstop=8 expandtab