2 #include "utils/mono-poll.h"
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)
19 g_assert (wakeup_pipe_fd >= 0);
22 poll_fds_capacity = 64;
24 poll_fds = g_new0 (mono_pollfd, poll_fds_capacity);
26 POLL_INIT_FD (&poll_fds [0], wakeup_pipe_fd, MONO_POLLIN);
32 poll_register_fd (gint fd, gint events, gboolean is_new)
38 g_assert (poll_fds_size <= poll_fds_capacity);
40 g_assert ((events & ~(EVENT_IN | EVENT_OUT)) == 0);
43 if (events & EVENT_IN)
44 poll_event |= MONO_POLLIN;
45 if (events & EVENT_OUT)
46 poll_event |= MONO_POLLOUT;
48 for (i = 0; i < poll_fds_size; ++i) {
49 if (poll_fds [i].fd == fd) {
51 POLL_INIT_FD (&poll_fds [i], fd, poll_event);
58 for (i = 0; i < poll_fds_size; ++i) {
59 if (poll_fds [i].fd == -1) {
60 POLL_INIT_FD (&poll_fds [i], fd, poll_event);
67 if (poll_fds_size > poll_fds_capacity) {
68 poll_fds_capacity *= 2;
69 g_assert (poll_fds_size <= poll_fds_capacity);
71 poll_fds = (mono_pollfd *)g_renew (mono_pollfd, poll_fds, poll_fds_capacity);
74 POLL_INIT_FD (&poll_fds [poll_fds_size - 1], fd, poll_event);
78 poll_remove_fd (gint fd)
84 for (i = 0; i < poll_fds_size; ++i) {
85 if (poll_fds [i].fd == fd) {
86 POLL_INIT_FD (&poll_fds [i], -1, 0);
91 /* if we don't find the fd in poll_fds,
92 * it means we try to delete it twice */
93 g_assert (i < poll_fds_size);
95 /* if we find it again, it means we added
97 for (; i < poll_fds_size; ++i)
98 g_assert (poll_fds [i].fd != fd);
100 /* reduce the value of poll_fds_size so we
101 * do not keep it too big */
102 while (poll_fds_size > 1 && poll_fds [poll_fds_size - 1].fd == -1)
107 poll_mark_bad_fds (mono_pollfd *poll_fds, gint poll_fds_size)
111 for (i = 0; i < poll_fds_size; i++) {
112 if (poll_fds [i].fd == -1)
115 switch (mono_poll (&poll_fds [i], 1, 0)) {
120 #if !defined(HOST_WIN32)
123 if (WSAGetLastError () == WSAEBADF)
126 poll_fds [i].revents |= MONO_POLLNVAL;
137 poll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), gpointer user_data)
141 for (i = 0; i < poll_fds_size; ++i)
142 poll_fds [i].revents = 0;
144 mono_gc_set_skip_thread (TRUE);
147 ready = mono_poll (poll_fds, poll_fds_size, -1);
150 mono_gc_set_skip_thread (FALSE);
154 * Apart from EINTR, we only check EBADF, for the rest:
155 * EINVAL: mono_poll() 'protects' us from descriptor
156 * numbers above the limit if using select() by marking
157 * then as POLLERR. If a system poll() is being
158 * used, the number of descriptor we're passing will not
159 * be over sysconf(_SC_OPEN_MAX), as the error would have
160 * happened when opening.
162 * EFAULT: we own the memory pointed by pfds.
163 * ENOMEM: we're doomed anyway
166 #if !defined(HOST_WIN32)
169 switch (WSAGetLastError ())
172 #if !defined(HOST_WIN32)
178 mono_thread_internal_check_for_interruption_critical (mono_thread_internal_current ());
182 #if !defined(HOST_WIN32)
188 ready = poll_mark_bad_fds (poll_fds, poll_fds_size);
192 #if !defined(HOST_WIN32)
193 g_error ("poll_event_wait: mono_poll () failed, error (%d) %s", errno, g_strerror (errno));
195 g_error ("poll_event_wait: mono_poll () failed, error (%d)\n", WSAGetLastError ());
206 g_assert (ready > 0);
208 for (i = 0; i < poll_fds_size; ++i) {
211 if (poll_fds [i].fd == -1)
213 if (poll_fds [i].revents == 0)
216 fd = poll_fds [i].fd;
217 if (poll_fds [i].revents & (MONO_POLLIN | MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL))
219 if (poll_fds [i].revents & (MONO_POLLOUT | MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL))
221 if (poll_fds [i].revents & (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL))
224 callback (fd, events, user_data);
233 static ThreadPoolIOBackend backend_poll = {
235 .register_fd = poll_register_fd,
236 .remove_fd = poll_remove_fd,
237 .event_wait = poll_event_wait,