4 #if defined(HAVE_POLL_H)
6 #elif defined(HAVE_SYS_POLL_H)
10 typedef struct pollfd mono_pollfd;
12 #elif defined(HOST_WIN32)
16 typedef WSAPOLLFD mono_pollfd;
19 /* poll is not defined */
23 static mono_pollfd *poll_fds;
24 static guint poll_fds_capacity;
25 static guint poll_fds_size;
28 POLL_INIT_FD (mono_pollfd *poll_fd, gint fd, gint events)
31 poll_fd->events = events;
36 poll_init (gint wakeup_pipe_fd)
40 poll_fds_size = wakeup_pipe_fd + 1;
41 poll_fds_capacity = 64;
43 while (wakeup_pipe_fd >= poll_fds_capacity)
44 poll_fds_capacity *= 4;
46 poll_fds = g_new0 (mono_pollfd, poll_fds_capacity);
48 for (i = 0; i < wakeup_pipe_fd; ++i)
49 POLL_INIT_FD (&poll_fds [i], -1, 0);
51 POLL_INIT_FD (&poll_fds [wakeup_pipe_fd], wakeup_pipe_fd, POLLIN);
63 poll_register_fd (gint fd, gint events, gboolean is_new)
69 g_assert (poll_fds_size <= poll_fds_capacity);
71 if (fd >= poll_fds_capacity) {
73 poll_fds_capacity *= 4;
74 } while (fd >= poll_fds_capacity);
76 poll_fds = g_renew (mono_pollfd, poll_fds, poll_fds_capacity);
79 if (fd >= poll_fds_size) {
80 for (i = poll_fds_size; i <= fd; ++i)
81 POLL_INIT_FD (&poll_fds [i], -1, 0);
83 poll_fds_size = fd + 1;
86 poll_fd = &poll_fds [fd];
88 if (poll_fd->fd != -1) {
89 g_assert (poll_fd->fd == fd);
93 POLL_INIT_FD (poll_fd, fd, ((events & EVENT_IN) ? POLLIN : 0) | ((events & EVENT_OUT) ? POLLOUT : 0));
97 poll_remove_fd (gint fd)
103 g_assert (fd < poll_fds_size);
104 poll_fd = &poll_fds [fd];
106 g_assert (poll_fd->fd == fd);
107 POLL_INIT_FD (poll_fd, -1, 0);
111 poll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), gpointer user_data)
115 for (i = 0; i < poll_fds_size; ++i)
116 poll_fds [i].revents = 0;
118 mono_gc_set_skip_thread (TRUE);
120 #if !defined(HOST_WIN32)
121 ready = poll (poll_fds, poll_fds_size, -1);
123 ready = WSAPoll(poll_fds, poll_fds_size, -1);
124 if (ready == SOCKET_ERROR)
128 mono_gc_set_skip_thread (FALSE);
132 * Apart from EINTR, we only check EBADF, for the rest:
133 * EINVAL: mono_poll() 'protects' us from descriptor
134 * numbers above the limit if using select() by marking
135 * then as POLLERR. If a system poll() is being
136 * used, the number of descriptor we're passing will not
137 * be over sysconf(_SC_OPEN_MAX), as the error would have
138 * happened when opening.
140 * EFAULT: we own the memory pointed by pfds.
141 * ENOMEM: we're doomed anyway
144 #if !defined(HOST_WIN32)
147 switch (WSAGetLastError ())
150 #if !defined(HOST_WIN32)
156 mono_thread_internal_check_for_interruption_critical (mono_thread_internal_current ());
161 #if !defined(HOST_WIN32)
162 g_error ("poll_event_wait: mono_poll () failed, error (%d) %s", errno, g_strerror (errno));
164 g_error ("poll_event_wait: mono_poll () failed, error (%d)\n", WSAGetLastError ());
173 for (i = 0; i < poll_fds_size; ++i) {
176 if (poll_fds [i].fd == -1)
178 if (poll_fds [i].revents == 0)
181 fd = poll_fds [i].fd;
182 if (poll_fds [i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL))
184 if (poll_fds [i].revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL))
187 callback (fd, events, user_data);
196 static ThreadPoolIOBackend backend_poll = {
198 .cleanup = poll_cleanup,
199 .register_fd = poll_register_fd,
200 .remove_fd = poll_remove_fd,
201 .event_wait = poll_event_wait,