1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 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;
70 if (mask & yaz_poll_read)
72 if (mask & yaz_poll_write)
74 if (mask & yaz_poll_except)
80 tv.tv_usec = nsec / 1000;
82 r = select(max_fd+1, &input, &output, &except, (sec == -1 ? 0 : &tv));
85 for (i = 0; i < num_fds; i++)
87 enum yaz_poll_mask mask = yaz_poll_none;
90 yaz_poll_add(mask, yaz_poll_timeout);
92 if (FD_ISSET(fd, &input))
93 yaz_poll_add(mask, yaz_poll_read);
94 if (FD_ISSET(fd, &output))
95 yaz_poll_add(mask, yaz_poll_write);
96 if (FD_ISSET(fd, &except))
97 yaz_poll_add(mask, yaz_poll_except);
99 fds[i].output_mask = mask;
106 int yaz_poll_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
109 struct pollfd *pollfds = (struct pollfd *)
110 xmalloc(num_fds * sizeof *pollfds);
114 for (i = 0; i < num_fds; i++)
116 enum yaz_poll_mask mask = fds[i].input_mask;
118 short poll_events = 0;
120 if (mask & yaz_poll_read)
121 poll_events += POLLIN;
122 if (mask & yaz_poll_write)
123 poll_events += POLLOUT;
124 if (mask & yaz_poll_except)
125 poll_events += POLLERR;
127 pollfds[i].events = poll_events;
128 pollfds[i].revents = 0;
130 r = poll(pollfds, num_fds, sec == -1 ? -1 : sec*1000 + nsec/1000000);
133 for (i = 0; i < num_fds; i++)
135 enum yaz_poll_mask mask = yaz_poll_none;
137 yaz_poll_add(mask, yaz_poll_timeout);
140 if (pollfds[i].revents & POLLIN)
141 yaz_poll_add(mask, yaz_poll_read);
142 if (pollfds[i].revents & POLLOUT)
143 yaz_poll_add(mask, yaz_poll_write);
144 if (pollfds[i].revents & ~(POLLIN | POLLOUT))
146 yaz_poll_add(mask, yaz_poll_except);
149 fds[i].output_mask = mask;
157 int yaz_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
159 #if YAZ_HAVE_SYS_POLL_H
160 return yaz_poll_poll(fds, num_fds, sec, nsec);
162 return yaz_poll_select(fds, num_fds, sec, nsec);
169 * c-file-style: "Stroustrup"
170 * indent-tabs-mode: nil
172 * vim: shiftwidth=4 tabstop=8 expandtab