5 #include "utils/mono-poll.h"
7 static mono_pollfd
*poll_fds
;
8 static guint poll_fds_capacity
;
9 static guint poll_fds_size
;
12 POLL_INIT_FD (mono_pollfd
*poll_fd
, gint fd
, gint events
)
15 poll_fd
->events
= events
;
20 poll_init (gint wakeup_pipe_fd
)
22 g_assert (wakeup_pipe_fd
>= 0);
25 poll_fds_capacity
= 64;
27 poll_fds
= g_new0 (mono_pollfd
, poll_fds_capacity
);
29 POLL_INIT_FD (&poll_fds
[0], wakeup_pipe_fd
, MONO_POLLIN
);
35 poll_can_register_fd (int fd
)
37 return mono_poll_can_add (poll_fds
, poll_fds_size
, fd
);
41 poll_register_fd (gint fd
, gint events
, gboolean is_new
)
47 g_assert (poll_fds_size
<= poll_fds_capacity
);
49 g_assert ((events
& ~(EVENT_IN
| EVENT_OUT
)) == 0);
52 if (events
& EVENT_IN
)
53 poll_event
|= MONO_POLLIN
;
54 if (events
& EVENT_OUT
)
55 poll_event
|= MONO_POLLOUT
;
57 for (i
= 0; i
< poll_fds_size
; ++i
) {
58 if (poll_fds
[i
].fd
== fd
) {
60 POLL_INIT_FD (&poll_fds
[i
], fd
, poll_event
);
67 for (i
= 0; i
< poll_fds_size
; ++i
) {
68 if (poll_fds
[i
].fd
== -1) {
69 POLL_INIT_FD (&poll_fds
[i
], fd
, poll_event
);
76 if (poll_fds_size
> poll_fds_capacity
) {
77 poll_fds_capacity
*= 2;
78 g_assert (poll_fds_size
<= poll_fds_capacity
);
80 poll_fds
= (mono_pollfd
*)g_renew (mono_pollfd
, poll_fds
, poll_fds_capacity
);
83 POLL_INIT_FD (&poll_fds
[poll_fds_size
- 1], fd
, poll_event
);
88 poll_remove_fd (gint fd
)
94 for (i
= 0; i
< poll_fds_size
; ++i
) {
95 if (poll_fds
[i
].fd
== fd
) {
96 POLL_INIT_FD (&poll_fds
[i
], -1, 0);
101 /* if we don't find the fd in poll_fds,
102 * it means we try to delete it twice */
103 g_assert (i
< poll_fds_size
);
105 /* if we find it again, it means we added
107 for (; i
< poll_fds_size
; ++i
)
108 g_assert (poll_fds
[i
].fd
!= fd
);
110 /* reduce the value of poll_fds_size so we
111 * do not keep it too big */
112 while (poll_fds_size
> 1 && poll_fds
[poll_fds_size
- 1].fd
== -1)
117 poll_mark_bad_fds (mono_pollfd
*fds
, gint size
)
121 for (i
= 0; i
< size
; i
++) {
122 if (fds
[i
].fd
== -1)
125 switch (mono_poll (&fds
[i
], 1, 0)) {
132 fds
[i
].revents
|= MONO_POLLNVAL
;
143 poll_event_wait (void (*callback
) (gint fd
, gint events
, gpointer user_data
), gpointer user_data
)
147 for (i
= 0; i
< poll_fds_size
; ++i
)
148 poll_fds
[i
].revents
= 0;
150 mono_thread_info_set_flags (MONO_THREAD_INFO_FLAGS_NO_GC
);
153 ready
= mono_poll (poll_fds
, poll_fds_size
, -1);
156 mono_thread_info_set_flags (MONO_THREAD_INFO_FLAGS_NONE
);
160 * Apart from EINTR, we only check EBADF, for the rest:
161 * EINVAL: mono_poll() 'protects' us from descriptor
162 * numbers above the limit if using select() by marking
163 * then as POLLERR. If a system poll() is being
164 * used, the number of descriptor we're passing will not
165 * be over sysconf(_SC_OPEN_MAX), as the error would have
166 * happened when opening.
168 * EFAULT: we own the memory pointed by pfds.
169 * ENOMEM: we're doomed anyway
181 ready
= poll_mark_bad_fds (poll_fds
, poll_fds_size
);
185 g_error ("poll_event_wait: mono_poll () failed, error (%d) %s", errno
, g_strerror (errno
));
195 g_assert (ready
> 0);
197 for (i
= 0; i
< poll_fds_size
; ++i
) {
200 if (poll_fds
[i
].fd
== -1)
202 if (poll_fds
[i
].revents
== 0)
205 fd
= poll_fds
[i
].fd
;
206 if (poll_fds
[i
].revents
& (MONO_POLLIN
| MONO_POLLERR
| MONO_POLLHUP
| MONO_POLLNVAL
))
208 if (poll_fds
[i
].revents
& (MONO_POLLOUT
| MONO_POLLERR
| MONO_POLLHUP
| MONO_POLLNVAL
))
210 if (poll_fds
[i
].revents
& (MONO_POLLERR
| MONO_POLLHUP
| MONO_POLLNVAL
))
213 callback (fd
, events
, user_data
);
222 static ThreadPoolIOBackend backend_poll
= {
224 poll_can_register_fd
,