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)) {
122 poll_fds [i].revents |= MONO_POLLNVAL;
133 poll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), gpointer user_data)
137 for (i = 0; i < poll_fds_size; ++i)
138 poll_fds [i].revents = 0;
140 mono_gc_set_skip_thread (TRUE);
143 ready = mono_poll (poll_fds, poll_fds_size, -1);
146 mono_gc_set_skip_thread (FALSE);
150 * Apart from EINTR, we only check EBADF, for the rest:
151 * EINVAL: mono_poll() 'protects' us from descriptor
152 * numbers above the limit if using select() by marking
153 * then as POLLERR. If a system poll() is being
154 * used, the number of descriptor we're passing will not
155 * be over sysconf(_SC_OPEN_MAX), as the error would have
156 * happened when opening.
158 * EFAULT: we own the memory pointed by pfds.
159 * ENOMEM: we're doomed anyway
166 mono_thread_internal_check_for_interruption_critical (mono_thread_internal_current ());
172 ready = poll_mark_bad_fds (poll_fds, poll_fds_size);
176 g_error ("poll_event_wait: mono_poll () failed, error (%d) %s", errno, g_strerror (errno));
186 g_assert (ready > 0);
188 for (i = 0; i < poll_fds_size; ++i) {
191 if (poll_fds [i].fd == -1)
193 if (poll_fds [i].revents == 0)
196 fd = poll_fds [i].fd;
197 if (poll_fds [i].revents & (MONO_POLLIN | MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL))
199 if (poll_fds [i].revents & (MONO_POLLOUT | MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL))
201 if (poll_fds [i].revents & (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL))
204 callback (fd, events, user_data);
213 static ThreadPoolIOBackend backend_poll = {
215 .register_fd = poll_register_fd,
216 .remove_fd = poll_remove_fd,
217 .event_wait = poll_event_wait,