2 #define POLL_NEVENTS 1024
4 static mono_pollfd *poll_fds;
5 static guint poll_fds_capacity;
6 static guint poll_fds_size;
9 POLL_INIT_FD (mono_pollfd *poll_fd, gint fd, gint events)
12 poll_fd->events = events;
17 poll_init (gint wakeup_pipe_fd)
22 poll_fds_capacity = POLL_NEVENTS;
23 poll_fds = g_new0 (mono_pollfd, poll_fds_capacity);
25 POLL_INIT_FD (poll_fds, wakeup_pipe_fd, MONO_POLLIN);
26 for (i = 1; i < poll_fds_capacity; ++i)
27 POLL_INIT_FD (poll_fds + i, -1, 0);
39 poll_mark_bad_fds (mono_pollfd *poll_fds, gint poll_fds_size)
46 for (i = 0; i < poll_fds_size; i++) {
47 poll_fd = poll_fds + i;
48 if (poll_fd->fd == -1)
51 ret = mono_poll (poll_fd, 1, 0);
55 #if !defined(HOST_WIN32)
58 if (WSAGetLastError () == WSAEBADF)
61 poll_fd->revents |= MONO_POLLNVAL;
71 poll_register_fd (gint fd, gint events, gboolean is_new)
73 gboolean found = FALSE;
76 for (j = 1; j < poll_fds_size; ++j) {
77 mono_pollfd *poll_fd = poll_fds + j;
78 if (poll_fd->fd == fd) {
86 POLL_INIT_FD (poll_fds + j, -1, 0);
91 for (j = 1; j < poll_fds_capacity; ++j) {
92 mono_pollfd *poll_fd = poll_fds + j;
93 if (poll_fd->fd == -1)
98 if (j == poll_fds_capacity) {
99 poll_fds_capacity += POLL_NEVENTS;
100 poll_fds = g_renew (mono_pollfd, poll_fds, poll_fds_capacity);
101 for (k = j; k < poll_fds_capacity; ++k)
102 POLL_INIT_FD (poll_fds + k, -1, 0);
105 POLL_INIT_FD (poll_fds + j, fd, events);
107 if (j >= poll_fds_size)
108 poll_fds_size = j + 1;
112 poll_event_wait (void)
116 ready = mono_poll (poll_fds, poll_fds_size, -1);
119 * Apart from EINTR, we only check EBADF, for the rest:
120 * EINVAL: mono_poll() 'protects' us from descriptor
121 * numbers above the limit if using select() by marking
122 * then as MONO_POLLERR. If a system poll() is being
123 * used, the number of descriptor we're passing will not
124 * be over sysconf(_SC_OPEN_MAX), as the error would have
125 * happened when opening.
127 * EFAULT: we own the memory pointed by pfds.
128 * ENOMEM: we're doomed anyway
131 #if !defined(HOST_WIN32)
134 switch (WSAGetLastError ())
137 #if !defined(HOST_WIN32)
142 mono_thread_internal_check_for_interruption_critical (mono_thread_internal_current ());
145 #if !defined(HOST_WIN32)
150 ready = poll_mark_bad_fds (poll_fds, poll_fds_size);
153 #if !defined(HOST_WIN32)
154 g_warning ("poll_event_wait: mono_poll () failed, error (%d) %s", errno, g_strerror (errno));
156 g_warning ("poll_event_wait: mono_poll () failed, error (%d)\n", WSAGetLastError ());
166 poll_event_get_fd_at (gint i, gint *events)
170 *events = ((poll_fds [i].revents & (MONO_POLLIN | MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)) ? MONO_POLLIN : 0)
171 | ((poll_fds [i].revents & (MONO_POLLOUT | MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)) ? MONO_POLLOUT : 0);
173 /* if nothing happened on the fd, then just return
174 * an invalid fd number so it is discarded */
175 return poll_fds [i].revents == 0 ? -1 : poll_fds [i].fd;
179 poll_event_get_fd_max (void)
181 return poll_fds_size;
184 static ThreadPoolIOBackend backend_poll = {
186 .cleanup = poll_cleanup,
187 .register_fd = poll_register_fd,
188 .event_wait = poll_event_wait,
189 .event_get_fd_max = poll_event_get_fd_max,
190 .event_get_fd_at = poll_event_get_fd_at,