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_register_fd (gint fd
, gint events
, gboolean is_new
)
41 g_assert (poll_fds_size
<= poll_fds_capacity
);
43 g_assert ((events
& ~(EVENT_IN
| EVENT_OUT
)) == 0);
46 if (events
& EVENT_IN
)
47 poll_event
|= MONO_POLLIN
;
48 if (events
& EVENT_OUT
)
49 poll_event
|= MONO_POLLOUT
;
51 for (i
= 0; i
< poll_fds_size
; ++i
) {
52 if (poll_fds
[i
].fd
== fd
) {
54 POLL_INIT_FD (&poll_fds
[i
], fd
, poll_event
);
61 for (i
= 0; i
< poll_fds_size
; ++i
) {
62 if (poll_fds
[i
].fd
== -1) {
63 POLL_INIT_FD (&poll_fds
[i
], fd
, poll_event
);
70 if (poll_fds_size
> poll_fds_capacity
) {
71 poll_fds_capacity
*= 2;
72 g_assert (poll_fds_size
<= poll_fds_capacity
);
74 poll_fds
= (mono_pollfd
*)g_renew (mono_pollfd
, poll_fds
, poll_fds_capacity
);
77 POLL_INIT_FD (&poll_fds
[poll_fds_size
- 1], fd
, poll_event
);
81 poll_remove_fd (gint fd
)
87 for (i
= 0; i
< poll_fds_size
; ++i
) {
88 if (poll_fds
[i
].fd
== fd
) {
89 POLL_INIT_FD (&poll_fds
[i
], -1, 0);
94 /* if we don't find the fd in poll_fds,
95 * it means we try to delete it twice */
96 g_assert (i
< poll_fds_size
);
98 /* if we find it again, it means we added
100 for (; i
< poll_fds_size
; ++i
)
101 g_assert (poll_fds
[i
].fd
!= fd
);
103 /* reduce the value of poll_fds_size so we
104 * do not keep it too big */
105 while (poll_fds_size
> 1 && poll_fds
[poll_fds_size
- 1].fd
== -1)
110 poll_mark_bad_fds (mono_pollfd
*fds
, gint size
)
114 for (i
= 0; i
< size
; i
++) {
115 if (fds
[i
].fd
== -1)
118 switch (mono_poll (&fds
[i
], 1, 0)) {
125 fds
[i
].revents
|= MONO_POLLNVAL
;
136 poll_event_wait (void (*callback
) (gint fd
, gint events
, gpointer user_data
), gpointer user_data
)
140 for (i
= 0; i
< poll_fds_size
; ++i
)
141 poll_fds
[i
].revents
= 0;
143 mono_thread_info_set_flags (MONO_THREAD_INFO_FLAGS_NO_GC
);
146 ready
= mono_poll (poll_fds
, poll_fds_size
, -1);
149 mono_thread_info_set_flags (MONO_THREAD_INFO_FLAGS_NONE
);
153 * Apart from EINTR, we only check EBADF, for the rest:
154 * EINVAL: mono_poll() 'protects' us from descriptor
155 * numbers above the limit if using select() by marking
156 * then as POLLERR. If a system poll() is being
157 * used, the number of descriptor we're passing will not
158 * be over sysconf(_SC_OPEN_MAX), as the error would have
159 * happened when opening.
161 * EFAULT: we own the memory pointed by pfds.
162 * ENOMEM: we're doomed anyway
174 ready
= poll_mark_bad_fds (poll_fds
, poll_fds_size
);
178 g_error ("poll_event_wait: mono_poll () failed, error (%d) %s", errno
, g_strerror (errno
));
188 g_assert (ready
> 0);
190 for (i
= 0; i
< poll_fds_size
; ++i
) {
193 if (poll_fds
[i
].fd
== -1)
195 if (poll_fds
[i
].revents
== 0)
198 fd
= poll_fds
[i
].fd
;
199 if (poll_fds
[i
].revents
& (MONO_POLLIN
| MONO_POLLERR
| MONO_POLLHUP
| MONO_POLLNVAL
))
201 if (poll_fds
[i
].revents
& (MONO_POLLOUT
| MONO_POLLERR
| MONO_POLLHUP
| MONO_POLLNVAL
))
203 if (poll_fds
[i
].revents
& (MONO_POLLERR
| MONO_POLLHUP
| MONO_POLLNVAL
))
206 callback (fd
, events
, user_data
);
215 static ThreadPoolIOBackend backend_poll
= {