2 * tpool-poll.c: poll related stuff
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2011 Novell, Inc (http://www.novell.com)
12 #define INIT_POLLFD(a, b, c) {(a)->fd = b; (a)->events = c; (a)->revents = 0;}
13 struct _tp_poll_data {
19 typedef struct _tp_poll_data tp_poll_data;
21 static void tp_poll_shutdown (gpointer event_data);
22 static void tp_poll_modify (gpointer p, int fd, int operation, int events, gboolean is_new);
23 static void tp_poll_wait (gpointer p);
26 tp_poll_init (SocketIOData *data)
30 struct sockaddr_in client;
31 struct sockaddr_in server;
36 result = g_new0 (tp_poll_data, 1);
38 if (pipe (result->pipe) != 0) {
44 srv = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
45 g_assert (srv != INVALID_SOCKET);
46 result->pipe [1] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
47 g_assert (result->pipe [1] != INVALID_SOCKET);
49 server.sin_family = AF_INET;
50 server.sin_addr.s_addr = inet_addr ("127.0.0.1");
52 if (bind (srv, (SOCKADDR *) &server, sizeof (struct sockaddr_in))) {
53 g_print ("%d\n", WSAGetLastError ());
57 len = sizeof (server);
58 getsockname (srv, (SOCKADDR *) &server, &len);
60 if (connect ((SOCKET) result->pipe [1], (SOCKADDR *) &server, sizeof (server)) == SOCKET_ERROR) {
61 g_print ("%d\n", WSAGetLastError ());
64 len = sizeof (client);
65 result->pipe [0] = accept (srv, (SOCKADDR *) &client, &len);
66 g_assert (result->pipe [0] != INVALID_SOCKET);
69 MONO_SEM_INIT (&result->new_sem, 1);
70 data->shutdown = tp_poll_shutdown;
71 data->modify = tp_poll_modify;
72 data->wait = tp_poll_wait;
77 tp_poll_modify (gpointer p, int fd, int operation, int events, gboolean is_new)
79 SocketIOData *socket_io_data;
85 data = socket_io_data->event_data;
87 mono_mutex_unlock (&socket_io_data->io_lock);
89 MONO_SEM_WAIT (&data->new_sem);
90 INIT_POLLFD (&data->newpfd, GPOINTER_TO_INT (fd), events);
91 *msg = (char) operation;
93 unused = write (data->pipe [1], msg, 1);
95 unused = send ((SOCKET) data->pipe [1], msg, 1, 0);
100 tp_poll_shutdown (gpointer event_data)
102 tp_poll_data *data = event_data;
105 closesocket (data->pipe [0]);
106 closesocket (data->pipe [1]);
108 if (data->pipe [0] > -1)
109 close (data->pipe [0]);
110 if (data->pipe [1] > -1)
111 close (data->pipe [1]);
115 MONO_SEM_DESTROY (&data->new_sem);
119 mark_bad_fds (mono_pollfd *pfds, int nfds)
125 for (i = 0; i < nfds; i++) {
130 ret = mono_poll (pfd, 1, 0);
131 if (ret == -1 && errno == EBADF) {
132 pfd->revents |= MONO_POLLNVAL;
134 } else if (ret == 1) {
143 tp_poll_wait (gpointer p)
145 #if MONO_SMALL_CONFIG
146 #define INITIAL_POLLFD_SIZE 128
148 #define INITIAL_POLLFD_SIZE 1024
150 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
152 #ifdef DISABLE_SOCKETS
153 #define socket_io_cleanup(x)
160 SocketIOData *socket_io_data = p;
161 MonoPtrArray async_results;
164 data = socket_io_data->event_data;
165 allocated = INITIAL_POLLFD_SIZE;
166 pfds = g_new0 (mono_pollfd, allocated);
167 mono_ptr_array_init (async_results, allocated * 2);
168 INIT_POLLFD (pfds, data->pipe [0], MONO_POLLIN);
169 for (i = 1; i < allocated; i++)
170 INIT_POLLFD (&pfds [i], -1, 0);
179 mono_gc_set_skip_thread (TRUE);
183 check_for_interruption_critical ();
186 nsock = mono_poll (pfds, maxfd, -1);
187 } while (nsock == -1 && errno == EINTR);
189 mono_gc_set_skip_thread (FALSE);
192 * Apart from EINTR, we only check EBADF, for the rest:
193 * EINVAL: mono_poll() 'protects' us from descriptor
194 * numbers above the limit if using select() by marking
195 * then as MONO_POLLERR. If a system poll() is being
196 * used, the number of descriptor we're passing will not
197 * be over sysconf(_SC_OPEN_MAX), as the error would have
198 * happened when opening.
200 * EFAULT: we own the memory pointed by pfds.
201 * ENOMEM: we're doomed anyway
205 if (nsock == -1 && errno == EBADF) {
206 pfds->revents = 0; /* Just in case... */
207 nsock = mark_bad_fds (pfds, maxfd);
210 if ((pfds->revents & POLL_ERRORS) != 0) {
211 /* We're supposed to die now, as the pipe has been closed */
213 mono_ptr_array_destroy (async_results);
214 socket_io_cleanup (socket_io_data);
218 /* Got a new socket */
219 if ((pfds->revents & MONO_POLLIN) != 0) {
221 gboolean found = FALSE;
223 for (i = 1; i < allocated; i++) {
225 if (pfd->fd == data->newpfd.fd) {
232 for (i = 1; i < allocated; i++) {
239 if (i == allocated) {
244 allocated = allocated * 2;
245 pfds = g_renew (mono_pollfd, oldfd, allocated);
247 for (; i < allocated; i++)
248 INIT_POLLFD (&pfds [i], -1, 0);
249 //async_results = g_renew (gpointer, async_results, allocated * 2);
252 nread = read (data->pipe [0], one, 1);
254 nread = recv ((SOCKET) data->pipe [0], one, 1, 0);
258 mono_ptr_array_destroy (async_results);
259 return; /* we're closed */
262 INIT_POLLFD (&pfds [i], data->newpfd.fd, data->newpfd.events);
263 memset (&data->newpfd, 0, sizeof (mono_pollfd));
264 MONO_SEM_POST (&data->new_sem);
273 mono_mutex_lock (&socket_io_data->io_lock);
274 if (socket_io_data->inited == 3) {
276 mono_ptr_array_destroy (async_results);
277 mono_mutex_unlock (&socket_io_data->io_lock);
278 return; /* cleanup called */
282 mono_ptr_array_clear (async_results);
284 for (i = 1; i < maxfd && nsock > 0; i++) {
286 if (pfd->fd == -1 || pfd->revents == 0)
290 list = mono_g_hash_table_lookup (socket_io_data->sock_to_state, GINT_TO_POINTER (pfd->fd));
291 if (list != NULL && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
292 ares = get_io_event (&list, MONO_POLLIN);
294 mono_ptr_array_append (async_results, ares);
299 if (list != NULL && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
300 ares = get_io_event (&list, MONO_POLLOUT);
302 mono_ptr_array_append (async_results, ares);
308 mono_g_hash_table_replace (socket_io_data->sock_to_state, GINT_TO_POINTER (pfd->fd), list);
309 pfd->events = get_events_from_list (list);
311 mono_g_hash_table_remove (socket_io_data->sock_to_state, GINT_TO_POINTER (pfd->fd));
317 mono_mutex_unlock (&socket_io_data->io_lock);
318 threadpool_append_jobs (&async_io_tp, (MonoObject **) async_results.data, nresults);
319 mono_ptr_array_clear (async_results);