2 * threadpool-ms-io.c: Microsoft IO threadpool runtime support
5 * Ludovic Henry (ludovic.henry@xamarin.com)
7 * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #ifndef DISABLE_SOCKETS
17 #if defined(HOST_WIN32)
24 #include <mono/metadata/gc-internals.h>
25 #include <mono/metadata/mono-mlist.h>
26 #include <mono/metadata/threadpool-ms.h>
27 #include <mono/metadata/threadpool-ms-io.h>
28 #include <mono/utils/atomic.h>
29 #include <mono/utils/mono-threads.h>
30 #include <mono/utils/mono-lazy-init.h>
31 #include <mono/utils/mono-logger-internals.h>
34 gboolean (*init) (gint wakeup_pipe_fd);
35 void (*cleanup) (void);
36 void (*register_fd) (gint fd, gint events, gboolean is_new);
37 void (*remove_fd) (gint fd);
38 gint (*event_wait) (void (*callback) (gint fd, gint events, gpointer user_data), gpointer user_data);
39 } ThreadPoolIOBackend;
41 /* Keep in sync with System.IOOperation in mcs/class/System/System/IOSelector.cs */
42 enum MonoIOOperation {
45 EVENT_ERR = 1 << 2, /* not in managed */
48 #include "threadpool-ms-io-epoll.c"
49 #include "threadpool-ms-io-kqueue.c"
50 #include "threadpool-ms-io-poll.c"
52 #define UPDATES_CAPACITY 128
54 /* Keep in sync with System.IOSelectorJob in mcs/class/System/System/IOSelector.cs */
55 struct _MonoIOSelectorJob {
67 } ThreadPoolIOUpdateType;
71 MonoIOSelectorJob *job;
72 } ThreadPoolIOUpdate_Add;
76 } ThreadPoolIOUpdate_RemoveSocket;
80 } ThreadPoolIOUpdate_RemoveDomain;
83 ThreadPoolIOUpdateType type;
85 ThreadPoolIOUpdate_Add add;
86 ThreadPoolIOUpdate_RemoveSocket remove_socket;
87 ThreadPoolIOUpdate_RemoveDomain remove_domain;
92 ThreadPoolIOBackend backend;
94 ThreadPoolIOUpdate updates [UPDATES_CAPACITY];
96 MonoCoopMutex updates_lock;
97 MonoCoopCond updates_cond;
99 #if !defined(HOST_WIN32)
100 gint wakeup_pipes [2];
102 SOCKET wakeup_pipes [2];
106 static mono_lazy_init_t io_status = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
108 static gboolean io_selector_running = FALSE;
110 static ThreadPoolIO* threadpool_io;
112 static MonoIOSelectorJob*
113 get_job_for_event (MonoMList **list, gint32 event)
119 for (current = *list; current; current = mono_mlist_next (current)) {
120 MonoIOSelectorJob *job = (MonoIOSelectorJob*) mono_mlist_get_data (current);
121 if (job->operation == event) {
122 *list = mono_mlist_remove_item (*list, current);
131 get_operations_for_jobs (MonoMList *list)
136 for (current = list; current; current = mono_mlist_next (current))
137 operations |= ((MonoIOSelectorJob*) mono_mlist_get_data (current))->operation;
143 selector_thread_wakeup (void)
149 #if !defined(HOST_WIN32)
150 written = write (threadpool_io->wakeup_pipes [1], &msg, 1);
154 g_warning ("selector_thread_wakeup: write () failed, error (%d) %s\n", errno, g_strerror (errno));
158 written = send (threadpool_io->wakeup_pipes [1], &msg, 1, 0);
161 if (written == SOCKET_ERROR) {
162 g_warning ("selector_thread_wakeup: write () failed, error (%d)\n", WSAGetLastError ());
170 selector_thread_wakeup_drain_pipes (void)
176 #if !defined(HOST_WIN32)
177 received = read (threadpool_io->wakeup_pipes [0], buffer, sizeof (buffer));
180 if (received == -1) {
181 if (errno != EINTR && errno != EAGAIN)
182 g_warning ("selector_thread_wakeup_drain_pipes: read () failed, error (%d) %s\n", errno, g_strerror (errno));
186 received = recv (threadpool_io->wakeup_pipes [0], buffer, sizeof (buffer), 0);
189 if (received == SOCKET_ERROR) {
190 if (WSAGetLastError () != WSAEINTR && WSAGetLastError () != WSAEWOULDBLOCK)
191 g_warning ("selector_thread_wakeup_drain_pipes: recv () failed, error (%d) %s\n", WSAGetLastError ());
200 MonoGHashTable *states;
201 } FilterSockaresForDomainData;
204 filter_jobs_for_domain (gpointer key, gpointer value, gpointer user_data)
206 FilterSockaresForDomainData *data;
207 MonoMList *list = (MonoMList *)value, *element;
209 MonoGHashTable *states;
211 g_assert (user_data);
212 data = (FilterSockaresForDomainData *)user_data;
213 domain = data->domain;
214 states = data->states;
216 for (element = list; element; element = mono_mlist_next (element)) {
217 MonoIOSelectorJob *job = (MonoIOSelectorJob*) mono_mlist_get_data (element);
218 if (mono_object_domain (job) == domain)
219 mono_mlist_set_data (element, NULL);
222 /* we skip all the first elements which are NULL */
223 for (; list; list = mono_mlist_next (list)) {
224 if (mono_mlist_get_data (list))
229 g_assert (mono_mlist_get_data (list));
231 /* we delete all the NULL elements after the first one */
232 for (element = list; element;) {
234 if (!(next = mono_mlist_next (element)))
236 if (mono_mlist_get_data (next))
239 mono_mlist_set_next (element, mono_mlist_next (next));
243 mono_g_hash_table_replace (states, key, list);
247 wait_callback (gint fd, gint events, gpointer user_data)
251 if (mono_runtime_is_shutting_down ())
254 if (fd == threadpool_io->wakeup_pipes [0]) {
255 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: wke");
256 selector_thread_wakeup_drain_pipes ();
258 MonoGHashTable *states;
259 MonoMList *list = NULL;
261 gboolean remove_fd = FALSE;
264 g_assert (user_data);
265 states = (MonoGHashTable *)user_data;
267 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: cal fd %3d, events = %2s | %2s | %3s",
268 fd, (events & EVENT_IN) ? "RD" : "..", (events & EVENT_OUT) ? "WR" : "..", (events & EVENT_ERR) ? "ERR" : "...");
270 if (!mono_g_hash_table_lookup_extended (states, GINT_TO_POINTER (fd), &k, (gpointer*) &list))
271 g_error ("wait_callback: fd %d not found in states table", fd);
273 if (list && (events & EVENT_IN) != 0) {
274 MonoIOSelectorJob *job = get_job_for_event (&list, EVENT_IN);
276 mono_threadpool_ms_enqueue_work_item (((MonoObject*) job)->vtable->domain, (MonoObject*) job, &error);
277 mono_error_raise_exception (&error); /* FIXME don't raise here */
281 if (list && (events & EVENT_OUT) != 0) {
282 MonoIOSelectorJob *job = get_job_for_event (&list, EVENT_OUT);
284 mono_threadpool_ms_enqueue_work_item (((MonoObject*) job)->vtable->domain, (MonoObject*) job, &error);
285 mono_error_raise_exception (&error); /* FIXME don't raise here */
289 remove_fd = (events & EVENT_ERR) == EVENT_ERR;
291 mono_g_hash_table_replace (states, GINT_TO_POINTER (fd), list);
293 operations = get_operations_for_jobs (list);
295 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: res fd %3d, events = %2s | %2s | %3s",
296 fd, (operations & EVENT_IN) ? "RD" : "..", (operations & EVENT_OUT) ? "WR" : "..", (operations & EVENT_ERR) ? "ERR" : "...");
298 threadpool_io->backend.register_fd (fd, operations, FALSE);
300 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: err fd %d", fd);
302 mono_g_hash_table_remove (states, GINT_TO_POINTER (fd));
304 threadpool_io->backend.remove_fd (fd);
310 selector_thread (gpointer data)
313 MonoGHashTable *states;
315 io_selector_running = TRUE;
317 if (mono_runtime_is_shutting_down ()) {
318 io_selector_running = FALSE;
322 states = mono_g_hash_table_new_type (g_direct_hash, g_direct_equal, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_THREAD_POOL, "i/o thread pool states table");
328 mono_coop_mutex_lock (&threadpool_io->updates_lock);
330 for (i = 0; i < threadpool_io->updates_size; ++i) {
331 ThreadPoolIOUpdate *update = &threadpool_io->updates [i];
333 switch (update->type) {
341 MonoMList *list = NULL;
342 MonoIOSelectorJob *job;
344 fd = update->data.add.fd;
347 job = update->data.add.job;
350 exists = mono_g_hash_table_lookup_extended (states, GINT_TO_POINTER (fd), &k, (gpointer*) &list);
351 list = mono_mlist_append (list, (MonoObject*) job);
352 mono_g_hash_table_replace (states, GINT_TO_POINTER (fd), list);
354 operations = get_operations_for_jobs (list);
356 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: %3s fd %3d, operations = %2s | %2s | %3s",
357 exists ? "mod" : "add", fd, (operations & EVENT_IN) ? "RD" : "..", (operations & EVENT_OUT) ? "WR" : "..", (operations & EVENT_ERR) ? "ERR" : "...");
359 threadpool_io->backend.register_fd (fd, operations, !exists);
363 case UPDATE_REMOVE_SOCKET: {
366 MonoMList *list = NULL;
368 fd = update->data.remove_socket.fd;
371 if (mono_g_hash_table_lookup_extended (states, GINT_TO_POINTER (fd), &k, (gpointer*) &list)) {
372 mono_g_hash_table_remove (states, GINT_TO_POINTER (fd));
374 for (j = i + 1; j < threadpool_io->updates_size; ++j) {
375 ThreadPoolIOUpdate *update = &threadpool_io->updates [j];
376 if (update->type == UPDATE_ADD && update->data.add.fd == fd)
377 memset (update, 0, sizeof (ThreadPoolIOUpdate));
380 for (; list; list = mono_mlist_remove_item (list, list)) {
381 mono_threadpool_ms_enqueue_work_item (mono_object_domain (mono_mlist_get_data (list)), mono_mlist_get_data (list), &error);
382 mono_error_raise_exception (&error); /* FIXME don't raise here */
385 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: del fd %3d", fd);
386 threadpool_io->backend.remove_fd (fd);
391 case UPDATE_REMOVE_DOMAIN: {
394 domain = update->data.remove_domain.domain;
397 FilterSockaresForDomainData user_data = { .domain = domain, .states = states };
398 mono_g_hash_table_foreach (states, filter_jobs_for_domain, &user_data);
400 for (j = i + 1; j < threadpool_io->updates_size; ++j) {
401 ThreadPoolIOUpdate *update = &threadpool_io->updates [j];
402 if (update->type == UPDATE_ADD && mono_object_domain (update->data.add.job) == domain)
403 memset (update, 0, sizeof (ThreadPoolIOUpdate));
409 g_assert_not_reached ();
413 mono_coop_cond_broadcast (&threadpool_io->updates_cond);
415 if (threadpool_io->updates_size > 0) {
416 threadpool_io->updates_size = 0;
417 memset (&threadpool_io->updates, 0, UPDATES_CAPACITY * sizeof (ThreadPoolIOUpdate));
420 mono_coop_mutex_unlock (&threadpool_io->updates_lock);
422 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: wai");
424 res = threadpool_io->backend.event_wait (wait_callback, states);
426 if (res == -1 || mono_runtime_is_shutting_down ())
430 mono_g_hash_table_destroy (states);
432 io_selector_running = FALSE;
435 /* Locking: threadpool_io->updates_lock must be held */
436 static ThreadPoolIOUpdate*
437 update_get_new (void)
439 ThreadPoolIOUpdate *update = NULL;
440 g_assert (threadpool_io->updates_size <= UPDATES_CAPACITY);
442 while (threadpool_io->updates_size == UPDATES_CAPACITY) {
443 /* we wait for updates to be applied in the selector_thread and we loop
444 * as long as none are available. if it happends too much, then we need
445 * to increase UPDATES_CAPACITY */
446 mono_coop_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
449 g_assert (threadpool_io->updates_size < UPDATES_CAPACITY);
451 update = &threadpool_io->updates [threadpool_io->updates_size ++];
457 wakeup_pipes_init (void)
459 #if !defined(HOST_WIN32)
460 if (pipe (threadpool_io->wakeup_pipes) == -1)
461 g_error ("wakeup_pipes_init: pipe () failed, error (%d) %s\n", errno, g_strerror (errno));
462 if (fcntl (threadpool_io->wakeup_pipes [0], F_SETFL, O_NONBLOCK) == -1)
463 g_error ("wakeup_pipes_init: fcntl () failed, error (%d) %s\n", errno, g_strerror (errno));
465 struct sockaddr_in client;
466 struct sockaddr_in server;
471 server_sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
472 g_assert (server_sock != INVALID_SOCKET);
473 threadpool_io->wakeup_pipes [1] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
474 g_assert (threadpool_io->wakeup_pipes [1] != INVALID_SOCKET);
476 server.sin_family = AF_INET;
477 server.sin_addr.s_addr = inet_addr ("127.0.0.1");
479 if (bind (server_sock, (SOCKADDR*) &server, sizeof (server)) == SOCKET_ERROR) {
480 closesocket (server_sock);
481 g_error ("wakeup_pipes_init: bind () failed, error (%d)\n", WSAGetLastError ());
484 size = sizeof (server);
485 if (getsockname (server_sock, (SOCKADDR*) &server, &size) == SOCKET_ERROR) {
486 closesocket (server_sock);
487 g_error ("wakeup_pipes_init: getsockname () failed, error (%d)\n", WSAGetLastError ());
489 if (listen (server_sock, 1024) == SOCKET_ERROR) {
490 closesocket (server_sock);
491 g_error ("wakeup_pipes_init: listen () failed, error (%d)\n", WSAGetLastError ());
493 if (connect ((SOCKET) threadpool_io->wakeup_pipes [1], (SOCKADDR*) &server, sizeof (server)) == SOCKET_ERROR) {
494 closesocket (server_sock);
495 g_error ("wakeup_pipes_init: connect () failed, error (%d)\n", WSAGetLastError ());
498 size = sizeof (client);
499 threadpool_io->wakeup_pipes [0] = accept (server_sock, (SOCKADDR *) &client, &size);
500 g_assert (threadpool_io->wakeup_pipes [0] != INVALID_SOCKET);
503 if (ioctlsocket (threadpool_io->wakeup_pipes [0], FIONBIO, &arg) == SOCKET_ERROR) {
504 closesocket (threadpool_io->wakeup_pipes [0]);
505 closesocket (server_sock);
506 g_error ("wakeup_pipes_init: ioctlsocket () failed, error (%d)\n", WSAGetLastError ());
509 closesocket (server_sock);
516 g_assert (!threadpool_io);
517 threadpool_io = g_new0 (ThreadPoolIO, 1);
518 g_assert (threadpool_io);
520 mono_coop_mutex_init (&threadpool_io->updates_lock);
521 mono_coop_cond_init (&threadpool_io->updates_cond);
522 mono_gc_register_root ((char *)&threadpool_io->updates [0], sizeof (threadpool_io->updates), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_THREAD_POOL, "i/o thread pool updates list");
524 threadpool_io->updates_size = 0;
526 threadpool_io->backend = backend_poll;
527 if (g_getenv ("MONO_ENABLE_AIO") != NULL) {
528 #if defined(HAVE_EPOLL)
529 threadpool_io->backend = backend_epoll;
530 #elif defined(HAVE_KQUEUE)
531 threadpool_io->backend = backend_kqueue;
535 wakeup_pipes_init ();
537 if (!threadpool_io->backend.init (threadpool_io->wakeup_pipes [0]))
538 g_error ("initialize: backend->init () failed");
540 if (!mono_thread_create_internal (mono_get_root_domain (), selector_thread, NULL, TRUE, SMALL_STACK))
541 g_error ("initialize: mono_thread_create_internal () failed");
547 /* we make the assumption along the code that we are
548 * cleaning up only if the runtime is shutting down */
549 g_assert (mono_runtime_is_shutting_down ());
551 selector_thread_wakeup ();
552 while (io_selector_running)
553 mono_thread_info_usleep (1000);
555 mono_coop_mutex_destroy (&threadpool_io->updates_lock);
556 mono_coop_cond_destroy (&threadpool_io->updates_cond);
558 threadpool_io->backend.cleanup ();
560 #if !defined(HOST_WIN32)
561 close (threadpool_io->wakeup_pipes [0]);
562 close (threadpool_io->wakeup_pipes [1]);
564 closesocket (threadpool_io->wakeup_pipes [0]);
565 closesocket (threadpool_io->wakeup_pipes [1]);
568 g_assert (threadpool_io);
569 g_free (threadpool_io);
570 threadpool_io = NULL;
571 g_assert (!threadpool_io);
575 mono_threadpool_ms_io_cleanup (void)
577 mono_lazy_cleanup (&io_status, cleanup);
581 ves_icall_System_IOSelector_Add (gpointer handle, MonoIOSelectorJob *job)
583 ThreadPoolIOUpdate *update;
585 g_assert (handle >= 0);
587 g_assert ((job->operation == EVENT_IN) ^ (job->operation == EVENT_OUT));
588 g_assert (job->callback);
590 if (mono_runtime_is_shutting_down ())
592 if (mono_domain_is_unloading (mono_object_domain (job)))
595 mono_lazy_initialize (&io_status, initialize);
597 mono_coop_mutex_lock (&threadpool_io->updates_lock);
599 update = update_get_new ();
600 update->type = UPDATE_ADD;
601 update->data.add.fd = GPOINTER_TO_INT (handle);
602 update->data.add.job = job;
603 mono_memory_barrier (); /* Ensure this is safely published before we wake up the selector */
605 selector_thread_wakeup ();
607 mono_coop_mutex_unlock (&threadpool_io->updates_lock);
611 ves_icall_System_IOSelector_Remove (gpointer handle)
613 mono_threadpool_ms_io_remove_socket (GPOINTER_TO_INT (handle));
617 mono_threadpool_ms_io_remove_socket (int fd)
619 ThreadPoolIOUpdate *update;
621 if (!mono_lazy_is_initialized (&io_status))
624 mono_coop_mutex_lock (&threadpool_io->updates_lock);
626 update = update_get_new ();
627 update->type = UPDATE_REMOVE_SOCKET;
628 update->data.add.fd = fd;
629 mono_memory_barrier (); /* Ensure this is safely published before we wake up the selector */
631 selector_thread_wakeup ();
633 mono_coop_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
635 mono_coop_mutex_unlock (&threadpool_io->updates_lock);
639 mono_threadpool_ms_io_remove_domain_jobs (MonoDomain *domain)
641 ThreadPoolIOUpdate *update;
643 if (!mono_lazy_is_initialized (&io_status))
646 mono_coop_mutex_lock (&threadpool_io->updates_lock);
648 update = update_get_new ();
649 update->type = UPDATE_REMOVE_DOMAIN;
650 update->data.remove_domain.domain = domain;
651 mono_memory_barrier (); /* Ensure this is safely published before we wake up the selector */
653 selector_thread_wakeup ();
655 mono_coop_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
657 mono_coop_mutex_unlock (&threadpool_io->updates_lock);
663 ves_icall_System_IOSelector_Add (gpointer handle, MonoIOSelectorJob *job)
665 g_assert_not_reached ();
669 ves_icall_System_IOSelector_Remove (gpointer handle)
671 g_assert_not_reached ();
675 mono_threadpool_ms_io_cleanup (void)
677 g_assert_not_reached ();
681 mono_threadpool_ms_io_remove_socket (int fd)
683 g_assert_not_reached ();
687 mono_threadpool_ms_io_remove_domain_jobs (MonoDomain *domain)
689 g_assert_not_reached ();