1 #define INIT_POLLFD(a, b, c) {(a)->fd = b; (a)->events = c; (a)->revents = 0;}
8 typedef struct _tp_poll_data tp_poll_data;
10 static void tp_poll_shutdown (gpointer event_data);
11 static void tp_poll_modify (gpointer event_data, int fd, int operation, int events, gboolean is_new);
12 static void tp_poll_wait (gpointer p);
16 connect_hack (gpointer x)
18 struct sockaddr_in *addr = (struct sockaddr_in *) x;
19 tp_poll_data *data = socket_io_data.event_data;
22 while (connect ((SOCKET) data->pipe [1], (SOCKADDR *) addr, sizeof (struct sockaddr_in))) {
25 g_warning ("Error initializing async. sockets %d.", WSAGetLastError ());
26 g_assert (WSAGetLastError ());
33 tp_poll_init (SocketIOData *data)
37 struct sockaddr_in server;
38 struct sockaddr_in client;
43 result = g_new0 (tp_poll_data, 1);
45 if (pipe (result->pipe) != 0) {
51 srv = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
52 g_assert (srv != INVALID_SOCKET);
53 result->pipe [1] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
54 g_assert (result->pipe [1] != INVALID_SOCKET);
56 server.sin_family = AF_INET;
57 server.sin_addr.s_addr = inet_addr ("127.0.0.1");
59 if (bind (srv, (SOCKADDR *) &server, sizeof (server))) {
60 g_print ("%d\n", WSAGetLastError ());
64 len = sizeof (server);
65 getsockname (srv, (SOCKADDR *) &server, &len);
67 mono_thread_create (mono_get_root_domain (), connect_hack, &server);
68 len = sizeof (server);
69 result->pipe [0] = accept (srv, (SOCKADDR *) &client, &len);
70 g_assert (result->pipe [0] != INVALID_SOCKET);
73 MONO_SEM_INIT (&result->new_sem, 1);
74 data->shutdown = tp_poll_shutdown;
75 data->modify = tp_poll_modify;
76 data->wait = tp_poll_wait;
81 tp_poll_modify (gpointer event_data, int fd, int operation, int events, gboolean is_new)
83 tp_poll_data *data = event_data;
87 MONO_SEM_WAIT (&data->new_sem);
88 INIT_POLLFD (&data->newpfd, GPOINTER_TO_INT (fd), events);
89 *msg = (char) operation;
91 w = write (data->pipe [1], msg, 1);
93 send ((SOCKET) data->pipe [1], msg, 1, 0);
98 tp_poll_shutdown (gpointer event_data)
100 tp_poll_data *data = event_data;
103 closesocket (data->pipe [0]);
104 closesocket (data->pipe [1]);
106 if (data->pipe [0] > -1)
107 close (data->pipe [0]);
108 if (data->pipe [1] > -1)
109 close (data->pipe [1]);
113 MONO_SEM_DESTROY (&data->new_sem);
117 mark_bad_fds (mono_pollfd *pfds, int nfds)
123 for (i = 0; i < nfds; i++) {
128 ret = mono_poll (pfd, 1, 0);
129 if (ret == -1 && errno == EBADF) {
130 pfd->revents |= MONO_POLLNVAL;
132 } else if (ret == 1) {
141 tp_poll_wait (gpointer p)
143 #if MONO_SMALL_CONFIG
144 #define INITIAL_POLLFD_SIZE 128
146 #define INITIAL_POLLFD_SIZE 1024
148 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
153 MonoInternalThread *thread;
155 SocketIOData *socket_io_data = p;
156 gpointer *async_results;
159 thread = mono_thread_internal_current ();
161 data = socket_io_data->event_data;
162 allocated = INITIAL_POLLFD_SIZE;
163 pfds = g_new0 (mono_pollfd, allocated);
164 async_results = g_new0 (gpointer, allocated * 2);
165 INIT_POLLFD (pfds, data->pipe [0], MONO_POLLIN);
166 for (i = 1; i < allocated; i++)
167 INIT_POLLFD (&pfds [i], -1, 0);
169 printf ("poll_wait\n");
179 if (THREAD_WANTS_A_BREAK (thread))
180 mono_thread_interruption_checkpoint ();
183 nsock = mono_poll (pfds, maxfd, -1);
184 } while (nsock == -1 && errno == EINTR);
187 * Apart from EINTR, we only check EBADF, for the rest:
188 * EINVAL: mono_poll() 'protects' us from descriptor
189 * numbers above the limit if using select() by marking
190 * then as MONO_POLLERR. If a system poll() is being
191 * used, the number of descriptor we're passing will not
192 * be over sysconf(_SC_OPEN_MAX), as the error would have
193 * happened when opening.
195 * EFAULT: we own the memory pointed by pfds.
196 * ENOMEM: we're doomed anyway
200 if (nsock == -1 && errno == EBADF) {
201 pfds->revents = 0; /* Just in case... */
202 nsock = mark_bad_fds (pfds, maxfd);
205 if ((pfds->revents & POLL_ERRORS) != 0) {
206 /* We're supposed to die now, as the pipe has been closed */
208 g_free (async_results);
209 socket_io_cleanup (socket_io_data);
213 /* Got a new socket */
214 if ((pfds->revents & MONO_POLLIN) != 0) {
217 for (i = 1; i < allocated; i++) {
219 if (pfd->fd == -1 || pfd->fd == data->newpfd.fd)
223 if (i == allocated) {
228 allocated = allocated * 2;
229 pfds = g_renew (mono_pollfd, oldfd, allocated);
231 for (; i < allocated; i++)
232 INIT_POLLFD (&pfds [i], -1, 0);
233 async_results = g_renew (gpointer, async_results, allocated * 2);
236 nread = read (data->pipe [0], one, 1);
238 nread = recv ((SOCKET) data->pipe [0], one, 1, 0);
242 g_free (async_results);
243 return; /* we're closed */
246 INIT_POLLFD (&pfds [i], data->newpfd.fd, data->newpfd.events);
247 memset (&data->newpfd, 0, sizeof (mono_pollfd));
248 MONO_SEM_POST (&data->new_sem);
257 EnterCriticalSection (&socket_io_data->io_lock);
258 if (socket_io_data->inited == 3) {
260 g_free (async_results);
261 LeaveCriticalSection (&socket_io_data->io_lock);
262 return; /* cleanup called */
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 async_results [nresults++] = ares;
279 if (list != NULL && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
280 ares = get_io_event (&list, MONO_POLLOUT);
282 async_results [nresults++] = ares;
286 mono_g_hash_table_replace (socket_io_data->sock_to_state, GINT_TO_POINTER (pfd->fd), list);
287 pfd->events = get_events_from_list (list);
289 mono_g_hash_table_remove (socket_io_data->sock_to_state, GINT_TO_POINTER (pfd->fd));
295 LeaveCriticalSection (&socket_io_data->io_lock);
296 threadpool_append_jobs (&async_io_tp, (MonoObject **) async_results, nresults);
297 memset (async_results, 0, sizeof (gpointer) * nresults);