1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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 = 0;
115 pollfds = (struct pollfd *) xmalloc(num_fds * sizeof *pollfds);
117 assert(num_fds >= 0);
118 for (i = 0; i < num_fds; i++)
120 enum yaz_poll_mask mask = fds[i].input_mask;
122 short poll_events = 0;
124 if (mask & yaz_poll_read)
125 poll_events += POLLIN;
126 if (mask & yaz_poll_write)
127 poll_events += POLLOUT;
128 if (mask & yaz_poll_except)
129 poll_events += POLLERR;
131 pollfds[i].events = poll_events;
132 pollfds[i].revents = 0;
134 r = poll(pollfds, num_fds, sec == -1 ? -1 : sec*1000 + nsec/1000000);
137 for (i = 0; i < num_fds; i++)
139 enum yaz_poll_mask mask = yaz_poll_none;
141 yaz_poll_add(mask, yaz_poll_timeout);
144 if (pollfds[i].revents & POLLIN)
145 yaz_poll_add(mask, yaz_poll_read);
146 if (pollfds[i].revents & POLLOUT)
147 yaz_poll_add(mask, yaz_poll_write);
148 if (pollfds[i].revents & ~(POLLIN | POLLOUT))
150 yaz_poll_add(mask, yaz_poll_except);
153 fds[i].output_mask = mask;
161 int yaz_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
164 return yaz_poll_poll(fds, num_fds, sec, nsec);
166 return yaz_poll_select(fds, num_fds, sec, nsec);
173 * c-file-style: "Stroustrup"
174 * indent-tabs-mode: nil
176 * vim: shiftwidth=4 tabstop=8 expandtab