7cdaf5be6842228814efe36fa9922ba81fbe4ea5
[mono.git] / mono / metadata / threadpool-ms-io.c
1 /*
2  * threadpool-ms-io.c: Microsoft IO threadpool runtime support
3  *
4  * Author:
5  *      Ludovic Henry (ludovic.henry@xamarin.com)
6  *
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.
9  */
10
11 #include <config.h>
12
13 #ifndef DISABLE_SOCKETS
14
15 #include <glib.h>
16
17 #if defined(HOST_WIN32)
18 #include <windows.h>
19 #else
20 #include <errno.h>
21 #include <fcntl.h>
22 #endif
23
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>
32
33 typedef struct {
34         gboolean (*init) (gint wakeup_pipe_fd);
35         void     (*register_fd) (gint fd, gint events, gboolean is_new);
36         void     (*remove_fd) (gint fd);
37         gint     (*event_wait) (void (*callback) (gint fd, gint events, gpointer user_data), gpointer user_data);
38 } ThreadPoolIOBackend;
39
40 /* Keep in sync with System.IOOperation in mcs/class/System/System/IOSelector.cs */
41 enum MonoIOOperation {
42         EVENT_IN   = 1 << 0,
43         EVENT_OUT  = 1 << 1,
44         EVENT_ERR  = 1 << 2, /* not in managed */
45 };
46
47 #include "threadpool-ms-io-epoll.c"
48 #include "threadpool-ms-io-kqueue.c"
49 #include "threadpool-ms-io-poll.c"
50
51 #define UPDATES_CAPACITY 128
52
53 /* Keep in sync with System.IOSelectorJob in mcs/class/System/System/IOSelector.cs */
54 struct _MonoIOSelectorJob {
55         MonoObject object;
56         gint32 operation;
57         MonoObject *callback;
58         MonoObject *state;
59 };
60
61 typedef enum {
62         UPDATE_EMPTY = 0,
63         UPDATE_ADD,
64         UPDATE_REMOVE_SOCKET,
65         UPDATE_REMOVE_DOMAIN,
66 } ThreadPoolIOUpdateType;
67
68 typedef struct {
69         gint fd;
70         MonoIOSelectorJob *job;
71 } ThreadPoolIOUpdate_Add;
72
73 typedef struct {
74         gint fd;
75 } ThreadPoolIOUpdate_RemoveSocket;
76
77 typedef struct {
78         MonoDomain *domain;
79 } ThreadPoolIOUpdate_RemoveDomain;
80
81 typedef struct {
82         ThreadPoolIOUpdateType type;
83         union {
84                 ThreadPoolIOUpdate_Add add;
85                 ThreadPoolIOUpdate_RemoveSocket remove_socket;
86                 ThreadPoolIOUpdate_RemoveDomain remove_domain;
87         } data;
88 } ThreadPoolIOUpdate;
89
90 typedef struct {
91         ThreadPoolIOBackend backend;
92
93         ThreadPoolIOUpdate updates [UPDATES_CAPACITY];
94         gint updates_size;
95         MonoCoopMutex updates_lock;
96         MonoCoopCond updates_cond;
97
98 #if !defined(HOST_WIN32)
99         gint wakeup_pipes [2];
100 #else
101         SOCKET wakeup_pipes [2];
102 #endif
103 } ThreadPoolIO;
104
105 static mono_lazy_init_t io_status = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
106
107 static gboolean io_selector_running = FALSE;
108
109 static ThreadPoolIO* threadpool_io;
110
111 static MonoIOSelectorJob*
112 get_job_for_event (MonoMList **list, gint32 event)
113 {
114         MonoMList *current;
115
116         g_assert (list);
117
118         for (current = *list; current; current = mono_mlist_next (current)) {
119                 MonoIOSelectorJob *job = (MonoIOSelectorJob*) mono_mlist_get_data (current);
120                 if (job->operation == event) {
121                         *list = mono_mlist_remove_item (*list, current);
122                         return job;
123                 }
124         }
125
126         return NULL;
127 }
128
129 static gint
130 get_operations_for_jobs (MonoMList *list)
131 {
132         MonoMList *current;
133         gint operations = 0;
134
135         for (current = list; current; current = mono_mlist_next (current))
136                 operations |= ((MonoIOSelectorJob*) mono_mlist_get_data (current))->operation;
137
138         return operations;
139 }
140
141 static void
142 selector_thread_wakeup (void)
143 {
144         gchar msg = 'c';
145         gint written;
146
147         for (;;) {
148 #if !defined(HOST_WIN32)
149                 written = write (threadpool_io->wakeup_pipes [1], &msg, 1);
150                 if (written == 1)
151                         break;
152                 if (written == -1) {
153                         g_warning ("selector_thread_wakeup: write () failed, error (%d) %s\n", errno, g_strerror (errno));
154                         break;
155                 }
156 #else
157                 written = send (threadpool_io->wakeup_pipes [1], &msg, 1, 0);
158                 if (written == 1)
159                         break;
160                 if (written == SOCKET_ERROR) {
161                         g_warning ("selector_thread_wakeup: write () failed, error (%d)\n", WSAGetLastError ());
162                         break;
163                 }
164 #endif
165         }
166 }
167
168 static void
169 selector_thread_wakeup_drain_pipes (void)
170 {
171         gchar buffer [128];
172         gint received;
173
174         for (;;) {
175 #if !defined(HOST_WIN32)
176                 received = read (threadpool_io->wakeup_pipes [0], buffer, sizeof (buffer));
177                 if (received == 0)
178                         break;
179                 if (received == -1) {
180                         if (errno != EINTR && errno != EAGAIN)
181                                 g_warning ("selector_thread_wakeup_drain_pipes: read () failed, error (%d) %s\n", errno, g_strerror (errno));
182                         break;
183                 }
184 #else
185                 received = recv (threadpool_io->wakeup_pipes [0], buffer, sizeof (buffer), 0);
186                 if (received == 0)
187                         break;
188                 if (received == SOCKET_ERROR) {
189                         if (WSAGetLastError () != WSAEINTR && WSAGetLastError () != WSAEWOULDBLOCK)
190                                 g_warning ("selector_thread_wakeup_drain_pipes: recv () failed, error (%d) %s\n", WSAGetLastError ());
191                         break;
192                 }
193 #endif
194         }
195 }
196
197 typedef struct {
198         MonoDomain *domain;
199         MonoGHashTable *states;
200 } FilterSockaresForDomainData;
201
202 static void
203 filter_jobs_for_domain (gpointer key, gpointer value, gpointer user_data)
204 {
205         FilterSockaresForDomainData *data;
206         MonoMList *list = (MonoMList *)value, *element;
207         MonoDomain *domain;
208         MonoGHashTable *states;
209
210         g_assert (user_data);
211         data = (FilterSockaresForDomainData *)user_data;
212         domain = data->domain;
213         states = data->states;
214
215         for (element = list; element; element = mono_mlist_next (element)) {
216                 MonoIOSelectorJob *job = (MonoIOSelectorJob*) mono_mlist_get_data (element);
217                 if (mono_object_domain (job) == domain)
218                         mono_mlist_set_data (element, NULL);
219         }
220
221         /* we skip all the first elements which are NULL */
222         for (; list; list = mono_mlist_next (list)) {
223                 if (mono_mlist_get_data (list))
224                         break;
225         }
226
227         if (list) {
228                 g_assert (mono_mlist_get_data (list));
229
230                 /* we delete all the NULL elements after the first one */
231                 for (element = list; element;) {
232                         MonoMList *next;
233                         if (!(next = mono_mlist_next (element)))
234                                 break;
235                         if (mono_mlist_get_data (next))
236                                 element = next;
237                         else
238                                 mono_mlist_set_next (element, mono_mlist_next (next));
239                 }
240         }
241
242         mono_g_hash_table_replace (states, key, list);
243 }
244
245 static void
246 wait_callback (gint fd, gint events, gpointer user_data)
247 {
248         MonoError error;
249
250         if (mono_runtime_is_shutting_down ())
251                 return;
252
253         if (fd == threadpool_io->wakeup_pipes [0]) {
254                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: wke");
255                 selector_thread_wakeup_drain_pipes ();
256         } else {
257                 MonoGHashTable *states;
258                 MonoMList *list = NULL;
259                 gpointer k;
260                 gboolean remove_fd = FALSE;
261                 gint operations;
262
263                 g_assert (user_data);
264                 states = (MonoGHashTable *)user_data;
265
266                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: cal fd %3d, events = %2s | %2s | %3s",
267                         fd, (events & EVENT_IN) ? "RD" : "..", (events & EVENT_OUT) ? "WR" : "..", (events & EVENT_ERR) ? "ERR" : "...");
268
269                 if (!mono_g_hash_table_lookup_extended (states, GINT_TO_POINTER (fd), &k, (gpointer*) &list))
270                         g_error ("wait_callback: fd %d not found in states table", fd);
271
272                 if (list && (events & EVENT_IN) != 0) {
273                         MonoIOSelectorJob *job = get_job_for_event (&list, EVENT_IN);
274                         if (job) {
275                                 mono_threadpool_ms_enqueue_work_item (((MonoObject*) job)->vtable->domain, (MonoObject*) job, &error);
276                                 mono_error_assert_ok (&error);
277                         }
278
279                 }
280                 if (list && (events & EVENT_OUT) != 0) {
281                         MonoIOSelectorJob *job = get_job_for_event (&list, EVENT_OUT);
282                         if (job) {
283                                 mono_threadpool_ms_enqueue_work_item (((MonoObject*) job)->vtable->domain, (MonoObject*) job, &error);
284                                 mono_error_assert_ok (&error);
285                         }
286                 }
287
288                 remove_fd = (events & EVENT_ERR) == EVENT_ERR;
289                 if (!remove_fd) {
290                         mono_g_hash_table_replace (states, GINT_TO_POINTER (fd), list);
291
292                         operations = get_operations_for_jobs (list);
293
294                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: res fd %3d, events = %2s | %2s | %3s",
295                                 fd, (operations & EVENT_IN) ? "RD" : "..", (operations & EVENT_OUT) ? "WR" : "..", (operations & EVENT_ERR) ? "ERR" : "...");
296
297                         threadpool_io->backend.register_fd (fd, operations, FALSE);
298                 } else {
299                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: err fd %d", fd);
300
301                         mono_g_hash_table_remove (states, GINT_TO_POINTER (fd));
302
303                         threadpool_io->backend.remove_fd (fd);
304                 }
305         }
306 }
307
308 static void
309 selector_thread (gpointer data)
310 {
311         MonoError error;
312         MonoGHashTable *states;
313
314         io_selector_running = TRUE;
315
316         if (mono_runtime_is_shutting_down ()) {
317                 io_selector_running = FALSE;
318                 return;
319         }
320
321         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");
322
323         for (;;) {
324                 gint i, j;
325                 gint res;
326
327                 mono_coop_mutex_lock (&threadpool_io->updates_lock);
328
329                 for (i = 0; i < threadpool_io->updates_size; ++i) {
330                         ThreadPoolIOUpdate *update = &threadpool_io->updates [i];
331
332                         switch (update->type) {
333                         case UPDATE_EMPTY:
334                                 break;
335                         case UPDATE_ADD: {
336                                 gint fd;
337                                 gint operations;
338                                 gpointer k;
339                                 gboolean exists;
340                                 MonoMList *list = NULL;
341                                 MonoIOSelectorJob *job;
342
343                                 fd = update->data.add.fd;
344                                 g_assert (fd >= 0);
345
346                                 job = update->data.add.job;
347                                 g_assert (job);
348
349                                 exists = mono_g_hash_table_lookup_extended (states, GINT_TO_POINTER (fd), &k, (gpointer*) &list);
350                                 list = mono_mlist_append_checked (list, (MonoObject*) job, &error);
351                                 mono_error_assert_ok (&error);
352                                 mono_g_hash_table_replace (states, GINT_TO_POINTER (fd), list);
353
354                                 operations = get_operations_for_jobs (list);
355
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" : "...");
358
359                                 threadpool_io->backend.register_fd (fd, operations, !exists);
360
361                                 break;
362                         }
363                         case UPDATE_REMOVE_SOCKET: {
364                                 gint fd;
365                                 gpointer k;
366                                 MonoMList *list = NULL;
367
368                                 fd = update->data.remove_socket.fd;
369                                 g_assert (fd >= 0);
370
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));
373
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));
378                                         }
379
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_assert_ok (&error);
383                                         }
384
385                                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: del fd %3d", fd);
386                                         threadpool_io->backend.remove_fd (fd);
387                                 }
388
389                                 break;
390                         }
391                         case UPDATE_REMOVE_DOMAIN: {
392                                 MonoDomain *domain;
393
394                                 domain = update->data.remove_domain.domain;
395                                 g_assert (domain);
396
397                                 FilterSockaresForDomainData user_data = { .domain = domain, .states = states };
398                                 mono_g_hash_table_foreach (states, filter_jobs_for_domain, &user_data);
399
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));
404                                 }
405
406                                 break;
407                         }
408                         default:
409                                 g_assert_not_reached ();
410                         }
411                 }
412
413                 mono_coop_cond_broadcast (&threadpool_io->updates_cond);
414
415                 if (threadpool_io->updates_size > 0) {
416                         threadpool_io->updates_size = 0;
417                         memset (&threadpool_io->updates, 0, UPDATES_CAPACITY * sizeof (ThreadPoolIOUpdate));
418                 }
419
420                 mono_coop_mutex_unlock (&threadpool_io->updates_lock);
421
422                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: wai");
423
424                 res = threadpool_io->backend.event_wait (wait_callback, states);
425
426                 if (res == -1 || mono_runtime_is_shutting_down ())
427                         break;
428         }
429
430         mono_g_hash_table_destroy (states);
431
432         io_selector_running = FALSE;
433 }
434
435 /* Locking: threadpool_io->updates_lock must be held */
436 static ThreadPoolIOUpdate*
437 update_get_new (void)
438 {
439         ThreadPoolIOUpdate *update = NULL;
440         g_assert (threadpool_io->updates_size <= UPDATES_CAPACITY);
441
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);
447         }
448
449         g_assert (threadpool_io->updates_size < UPDATES_CAPACITY);
450
451         update = &threadpool_io->updates [threadpool_io->updates_size ++];
452
453         return update;
454 }
455
456 static void
457 wakeup_pipes_init (void)
458 {
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));
464 #else
465         struct sockaddr_in client;
466         struct sockaddr_in server;
467         SOCKET server_sock;
468         gulong arg;
469         gint size;
470
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);
475
476         server.sin_family = AF_INET;
477         server.sin_addr.s_addr = inet_addr ("127.0.0.1");
478         server.sin_port = 0;
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 ());
482         }
483
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 ());
488         }
489         if (listen (server_sock, 1024) == SOCKET_ERROR) {
490                 closesocket (server_sock);
491                 g_error ("wakeup_pipes_init: listen () failed, error (%d)\n", WSAGetLastError ());
492         }
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 ());
496         }
497
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);
501
502         arg = 1;
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 ());
507         }
508
509         closesocket (server_sock);
510 #endif
511 }
512
513 static void
514 initialize (void)
515 {
516         g_assert (!threadpool_io);
517         threadpool_io = g_new0 (ThreadPoolIO, 1);
518         g_assert (threadpool_io);
519
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");
523
524         threadpool_io->updates_size = 0;
525
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;
532 #endif
533         }
534
535         wakeup_pipes_init ();
536
537         if (!threadpool_io->backend.init (threadpool_io->wakeup_pipes [0]))
538                 g_error ("initialize: backend->init () failed");
539
540         MonoError error;
541         if (!mono_thread_create_internal (mono_get_root_domain (), selector_thread, NULL, TRUE, SMALL_STACK, &error))
542                 g_error ("initialize: mono_thread_create_internal () failed due to %s", mono_error_get_message (&error));
543 }
544
545 static void
546 cleanup (void)
547 {
548         /* we make the assumption along the code that we are
549          * cleaning up only if the runtime is shutting down */
550         g_assert (mono_runtime_is_shutting_down ());
551
552         selector_thread_wakeup ();
553         while (io_selector_running)
554                 mono_thread_info_usleep (1000);
555 }
556
557 void
558 mono_threadpool_ms_io_cleanup (void)
559 {
560         mono_lazy_cleanup (&io_status, cleanup);
561 }
562
563 void
564 ves_icall_System_IOSelector_Add (gpointer handle, MonoIOSelectorJob *job)
565 {
566         ThreadPoolIOUpdate *update;
567
568         g_assert (handle);
569
570         g_assert ((job->operation == EVENT_IN) ^ (job->operation == EVENT_OUT));
571         g_assert (job->callback);
572
573         if (mono_runtime_is_shutting_down ())
574                 return;
575         if (mono_domain_is_unloading (mono_object_domain (job)))
576                 return;
577
578         mono_lazy_initialize (&io_status, initialize);
579
580         mono_coop_mutex_lock (&threadpool_io->updates_lock);
581
582         update = update_get_new ();
583         update->type = UPDATE_ADD;
584         update->data.add.fd = GPOINTER_TO_INT (handle);
585         update->data.add.job = job;
586         mono_memory_barrier (); /* Ensure this is safely published before we wake up the selector */
587
588         selector_thread_wakeup ();
589
590         mono_coop_mutex_unlock (&threadpool_io->updates_lock);
591 }
592
593 void
594 ves_icall_System_IOSelector_Remove (gpointer handle)
595 {
596         mono_threadpool_ms_io_remove_socket (GPOINTER_TO_INT (handle));
597 }
598
599 void
600 mono_threadpool_ms_io_remove_socket (int fd)
601 {
602         ThreadPoolIOUpdate *update;
603
604         if (!mono_lazy_is_initialized (&io_status))
605                 return;
606
607         mono_coop_mutex_lock (&threadpool_io->updates_lock);
608
609         update = update_get_new ();
610         update->type = UPDATE_REMOVE_SOCKET;
611         update->data.add.fd = fd;
612         mono_memory_barrier (); /* Ensure this is safely published before we wake up the selector */
613
614         selector_thread_wakeup ();
615
616         mono_coop_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
617
618         mono_coop_mutex_unlock (&threadpool_io->updates_lock);
619 }
620
621 void
622 mono_threadpool_ms_io_remove_domain_jobs (MonoDomain *domain)
623 {
624         ThreadPoolIOUpdate *update;
625
626         if (!mono_lazy_is_initialized (&io_status))
627                 return;
628
629         mono_coop_mutex_lock (&threadpool_io->updates_lock);
630
631         update = update_get_new ();
632         update->type = UPDATE_REMOVE_DOMAIN;
633         update->data.remove_domain.domain = domain;
634         mono_memory_barrier (); /* Ensure this is safely published before we wake up the selector */
635
636         selector_thread_wakeup ();
637
638         mono_coop_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
639
640         mono_coop_mutex_unlock (&threadpool_io->updates_lock);
641 }
642
643 #else
644
645 void
646 ves_icall_System_IOSelector_Add (gpointer handle, MonoIOSelectorJob *job)
647 {
648         g_assert_not_reached ();
649 }
650
651 void
652 ves_icall_System_IOSelector_Remove (gpointer handle)
653 {
654         g_assert_not_reached ();
655 }
656
657 void
658 mono_threadpool_ms_io_cleanup (void)
659 {
660         g_assert_not_reached ();
661 }
662
663 void
664 mono_threadpool_ms_io_remove_socket (int fd)
665 {
666         g_assert_not_reached ();
667 }
668
669 void
670 mono_threadpool_ms_io_remove_domain_jobs (MonoDomain *domain)
671 {
672         g_assert_not_reached ();
673 }
674
675 #endif