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 event_data, 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 event_data, int fd, int operation, int events, gboolean is_new)
79 tp_poll_data *data = event_data;
82 MONO_SEM_WAIT (&data->new_sem);
83 INIT_POLLFD (&data->newpfd, GPOINTER_TO_INT (fd), events);
84 *msg = (char) operation;
86 if (write (data->pipe [1], msg, 1));
88 send ((SOCKET) data->pipe [1], msg, 1, 0);
93 tp_poll_shutdown (gpointer event_data)
95 tp_poll_data *data = event_data;
98 closesocket (data->pipe [0]);
99 closesocket (data->pipe [1]);
101 if (data->pipe [0] > -1)
102 close (data->pipe [0]);
103 if (data->pipe [1] > -1)
104 close (data->pipe [1]);
108 MONO_SEM_DESTROY (&data->new_sem);
112 mark_bad_fds (mono_pollfd *pfds, int nfds)
118 for (i = 0; i < nfds; i++) {
123 ret = mono_poll (pfd, 1, 0);
124 if (ret == -1 && errno == EBADF) {
125 pfd->revents |= MONO_POLLNVAL;
127 } else if (ret == 1) {
136 tp_poll_wait (gpointer p)
138 #if MONO_SMALL_CONFIG
139 #define INITIAL_POLLFD_SIZE 128
141 #define INITIAL_POLLFD_SIZE 1024
143 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
145 #ifdef DISABLE_SOCKETS
146 #define socket_io_cleanup(x)
152 MonoInternalThread *thread;
154 SocketIOData *socket_io_data = p;
155 MonoPtrArray async_results;
158 thread = mono_thread_internal_current ();
160 data = socket_io_data->event_data;
161 allocated = INITIAL_POLLFD_SIZE;
162 pfds = g_new0 (mono_pollfd, allocated);
163 mono_ptr_array_init (async_results, allocated * 2);
164 INIT_POLLFD (pfds, data->pipe [0], MONO_POLLIN);
165 for (i = 1; i < allocated; i++)
166 INIT_POLLFD (&pfds [i], -1, 0);
177 if (THREAD_WANTS_A_BREAK (thread))
178 mono_thread_interruption_checkpoint ();
181 nsock = mono_poll (pfds, maxfd, -1);
182 } while (nsock == -1 && errno == EINTR);
185 * Apart from EINTR, we only check EBADF, for the rest:
186 * EINVAL: mono_poll() 'protects' us from descriptor
187 * numbers above the limit if using select() by marking
188 * then as MONO_POLLERR. If a system poll() is being
189 * used, the number of descriptor we're passing will not
190 * be over sysconf(_SC_OPEN_MAX), as the error would have
191 * happened when opening.
193 * EFAULT: we own the memory pointed by pfds.
194 * ENOMEM: we're doomed anyway
198 if (nsock == -1 && errno == EBADF) {
199 pfds->revents = 0; /* Just in case... */
200 nsock = mark_bad_fds (pfds, maxfd);
203 if ((pfds->revents & POLL_ERRORS) != 0) {
204 /* We're supposed to die now, as the pipe has been closed */
206 mono_ptr_array_destroy (async_results);
207 socket_io_cleanup (socket_io_data);
211 /* Got a new socket */
212 if ((pfds->revents & MONO_POLLIN) != 0) {
215 for (i = 1; i < allocated; i++) {
217 if (pfd->fd == -1 || pfd->fd == data->newpfd.fd)
221 if (i == allocated) {
226 allocated = allocated * 2;
227 pfds = g_renew (mono_pollfd, oldfd, allocated);
229 for (; i < allocated; i++)
230 INIT_POLLFD (&pfds [i], -1, 0);
231 //async_results = g_renew (gpointer, async_results, allocated * 2);
234 nread = read (data->pipe [0], one, 1);
236 nread = recv ((SOCKET) data->pipe [0], one, 1, 0);
240 mono_ptr_array_destroy (async_results);
241 return; /* we're closed */
244 INIT_POLLFD (&pfds [i], data->newpfd.fd, data->newpfd.events);
245 memset (&data->newpfd, 0, sizeof (mono_pollfd));
246 MONO_SEM_POST (&data->new_sem);
255 EnterCriticalSection (&socket_io_data->io_lock);
256 if (socket_io_data->inited == 3) {
258 mono_ptr_array_destroy (async_results);
259 LeaveCriticalSection (&socket_io_data->io_lock);
260 return; /* cleanup called */
264 mono_ptr_array_clear (async_results);
266 for (i = 1; i < maxfd && nsock > 0; i++) {
268 if (pfd->fd == -1 || pfd->revents == 0)
272 list = mono_g_hash_table_lookup (socket_io_data->sock_to_state, GINT_TO_POINTER (pfd->fd));
273 if (list != NULL && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
274 ares = get_io_event (&list, MONO_POLLIN);
276 mono_ptr_array_append (async_results, ares);
281 if (list != NULL && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
282 ares = get_io_event (&list, MONO_POLLOUT);
284 mono_ptr_array_append (async_results, ares);
290 mono_g_hash_table_replace (socket_io_data->sock_to_state, GINT_TO_POINTER (pfd->fd), list);
291 pfd->events = get_events_from_list (list);
293 mono_g_hash_table_remove (socket_io_data->sock_to_state, GINT_TO_POINTER (pfd->fd));
299 LeaveCriticalSection (&socket_io_data->io_lock);
300 threadpool_append_jobs (&async_io_tp, (MonoObject **) async_results.data, nresults);
301 mono_ptr_array_clear (async_results);