2 * threadpool.c: global thread pool
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-2009 Novell, Inc (http://www.novell.com)
15 #define THREADS_PER_CPU 10 /* 20 + THREADS_PER_CPU * number of CPUs */
16 #define THREAD_EXIT_TIMEOUT 1000
18 #include <mono/metadata/domain-internals.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/threads-types.h>
22 #include <mono/metadata/threadpool-internals.h>
23 #include <mono/metadata/exception.h>
24 #include <mono/metadata/file-io.h>
25 #include <mono/metadata/monitor.h>
26 #include <mono/metadata/mono-mlist.h>
27 #include <mono/metadata/marshal.h>
28 #include <mono/metadata/socket-io.h>
29 #include <mono/io-layer/io-layer.h>
30 #include <mono/metadata/gc-internal.h>
31 #include <mono/utils/mono-time.h>
32 #include <mono/utils/mono-proclib.h>
34 #ifdef HAVE_SYS_TIME_H
37 #include <sys/types.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
46 #include <mono/utils/mono-poll.h>
48 #include <sys/epoll.h>
51 #ifndef DISABLE_SOCKETS
52 #include "mono/io-layer/socket-wrappers.h"
55 #include "threadpool.h"
57 #define THREAD_WANTS_A_BREAK(t) ((t->state & (ThreadState_StopRequested | \
58 ThreadState_SuspendRequested)) != 0)
62 /* maximum number of worker threads */
63 static int mono_max_worker_threads;
64 static int mono_min_worker_threads;
65 static int mono_io_max_worker_threads;
66 static int mono_io_min_worker_threads;
68 /* current number of worker threads */
69 static int mono_worker_threads = 0;
70 static int io_worker_threads = 0;
72 /* current number of busy threads */
73 static int busy_worker_threads = 0;
74 static int busy_io_worker_threads;
76 /* mono_thread_pool_init called */
78 /* started idle threads */
79 static int tp_idle_started;
82 /* we use this to store a reference to the AsyncResult to avoid GC */
83 static MonoGHashTable *ares_htable = NULL;
85 static CRITICAL_SECTION ares_lock;
86 static CRITICAL_SECTION io_queue_lock;
87 static int pending_io_items;
90 CRITICAL_SECTION io_lock; /* access to sock_to_state */
93 MonoGHashTable *sock_to_state;
95 HANDLE new_sem; /* access to newpfd and write side of the pipe */
97 gboolean epoll_disabled;
103 static SocketIOData socket_io_data;
105 /* we append a job */
106 static HANDLE job_added;
107 static HANDLE io_job_added;
109 /* Keep in sync with the System.MonoAsyncCall class which provides GC tracking */
112 MonoMethodMessage *msg;
113 MonoMethod *cb_method;
114 MonoDelegate *cb_target;
118 /* This is a HANDLE, we use guint64 so the managed object layout remains constant */
128 static void async_invoke_thread (gpointer data);
129 static void append_job (CRITICAL_SECTION *cs, TPQueue *list, MonoObject *ar);
130 static void start_thread_or_queue (MonoAsyncResult *ares);
131 static void start_tpthread (MonoAsyncResult *ares);
132 static void mono_async_invoke (MonoAsyncResult *ares);
133 static MonoObject* dequeue_job (CRITICAL_SECTION *cs, TPQueue *list);
134 static void free_queue (TPQueue *list);
136 static TPQueue async_call_queue = {NULL, 0, 0};
137 static TPQueue async_io_queue = {NULL, 0, 0};
139 static MonoClass *async_call_klass;
140 static MonoClass *socket_async_call_klass;
141 static MonoClass *process_async_call_klass;
143 #define INIT_POLLFD(a, b, c) {(a)->fd = b; (a)->events = c; (a)->revents = 0;}
152 AIO_OP_RECV_JUST_CALLBACK,
153 AIO_OP_SEND_JUST_CALLBACK,
158 #ifdef DISABLE_SOCKETS
159 #define socket_io_cleanup(x)
162 socket_io_cleanup (SocketIOData *data)
166 if (data->inited == 0)
169 EnterCriticalSection (&data->io_lock);
171 #ifdef PLATFORM_WIN32
172 closesocket (data->pipe [0]);
173 closesocket (data->pipe [1]);
175 close (data->pipe [0]);
176 close (data->pipe [1]);
181 CloseHandle (data->new_sem);
182 data->new_sem = NULL;
183 mono_g_hash_table_destroy (data->sock_to_state);
184 data->sock_to_state = NULL;
185 free_queue (&async_io_queue);
186 release = (gint) InterlockedCompareExchange (&io_worker_threads, 0, -1);
188 ReleaseSemaphore (io_job_added, release, NULL);
189 g_free (data->newpfd);
192 if (FALSE == data->epoll_disabled)
193 close (data->epollfd);
195 LeaveCriticalSection (&data->io_lock);
199 get_event_from_state (MonoSocketAsyncResult *state)
201 switch (state->operation) {
204 case AIO_OP_RECV_JUST_CALLBACK:
205 case AIO_OP_RECEIVEFROM:
206 case AIO_OP_READPIPE:
209 case AIO_OP_SEND_JUST_CALLBACK:
213 default: /* Should never happen */
214 g_print ("get_event_from_state: unknown value in switch!!!\n");
220 get_events_from_list (MonoMList *list)
222 MonoSocketAsyncResult *state;
225 while (list && (state = (MonoSocketAsyncResult *)mono_mlist_get_data (list))) {
226 events |= get_event_from_state (state);
227 list = mono_mlist_next (list);
233 #define ICALL_RECV(x) ves_icall_System_Net_Sockets_Socket_Receive_internal (\
234 (SOCKET)(gssize)x->handle, x->buffer, x->offset, x->size,\
235 x->socket_flags, &x->error);
237 #define ICALL_SEND(x) ves_icall_System_Net_Sockets_Socket_Send_internal (\
238 (SOCKET)(gssize)x->handle, x->buffer, x->offset, x->size,\
239 x->socket_flags, &x->error);
241 #endif /* !DISABLE_SOCKETS */
244 unregister_job (MonoAsyncResult *obj)
246 EnterCriticalSection (&ares_lock);
247 mono_g_hash_table_remove (ares_htable, obj);
248 LeaveCriticalSection (&ares_lock);
252 threadpool_jobs_inc (MonoObject *obj)
255 InterlockedIncrement (&obj->vtable->domain->threadpool_jobs);
259 threadpool_jobs_dec (MonoObject *obj)
261 MonoDomain *domain = obj->vtable->domain;
262 int remaining_jobs = InterlockedDecrement (&domain->threadpool_jobs);
263 if (remaining_jobs == 0 && domain->cleanup_semaphore) {
264 ReleaseSemaphore (domain->cleanup_semaphore, 1, NULL);
270 #ifndef DISABLE_SOCKETS
272 async_invoke_io_thread (gpointer data)
276 const gchar *version;
278 thread = mono_thread_current ();
280 version = mono_get_runtime_info ()->framework_version;
282 MonoSocketAsyncResult *state;
285 state = (MonoSocketAsyncResult *) data;
287 InterlockedDecrement (&pending_io_items);
289 switch (state->operation) {
291 state->total = ICALL_RECV (state);
294 state->total = ICALL_SEND (state);
298 /* worker threads invokes methods in different domains,
299 * so we need to set the right domain here */
300 domain = ((MonoObject *)ar)->vtable->domain;
304 if (domain->state == MONO_APPDOMAIN_UNLOADED || domain->state == MONO_APPDOMAIN_UNLOADING) {
305 threadpool_jobs_dec ((MonoObject *)ar);
309 mono_thread_push_appdomain_ref (domain);
310 if (threadpool_jobs_dec ((MonoObject *)ar)) {
313 mono_thread_pop_appdomain_ref ();
316 if (mono_domain_set (domain, FALSE)) {
319 mono_async_invoke (ar);
320 ac = (ASyncCall *) ar->object_data;
322 if (ac->msg->exc != NULL)
323 mono_unhandled_exception (ac->msg->exc);
325 mono_domain_set (mono_get_root_domain (), TRUE);
327 mono_thread_pop_appdomain_ref ();
328 InterlockedDecrement (&busy_io_worker_threads);
329 /* If the callee changes the background status, set it back to TRUE */
330 if (*version != '1' && !mono_thread_test_state (thread , ThreadState_Background))
331 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
335 data = dequeue_job (&io_queue_lock, &async_io_queue);
339 int timeout = THREAD_EXIT_TIMEOUT;
340 guint32 start_time = mono_msec_ticks ();
343 wr = WaitForSingleObjectEx (io_job_added, (guint32)timeout, TRUE);
344 if (THREAD_WANTS_A_BREAK (thread))
345 mono_thread_interruption_checkpoint ();
347 timeout -= mono_msec_ticks () - start_time;
349 if (wr != WAIT_TIMEOUT && wr != WAIT_IO_COMPLETION)
350 data = dequeue_job (&io_queue_lock, &async_io_queue);
352 while (!data && timeout > 0);
356 if (InterlockedDecrement (&io_worker_threads) < 2) {
357 /* If we have pending items, keep the thread alive */
358 if (InterlockedCompareExchange (&pending_io_items, 0, 0) != 0) {
359 InterlockedIncrement (&io_worker_threads);
366 InterlockedIncrement (&busy_io_worker_threads);
369 g_assert_not_reached ();
373 start_io_thread_or_queue (MonoSocketAsyncResult *ares)
377 busy = (int) InterlockedCompareExchange (&busy_io_worker_threads, 0, -1);
378 worker = (int) InterlockedCompareExchange (&io_worker_threads, 0, -1);
379 if (worker <= ++busy &&
380 worker < mono_io_max_worker_threads) {
381 InterlockedIncrement (&busy_io_worker_threads);
382 InterlockedIncrement (&io_worker_threads);
383 threadpool_jobs_inc ((MonoObject *)ares);
384 mono_thread_create_internal (mono_get_root_domain (), async_invoke_io_thread, ares, TRUE);
386 append_job (&io_queue_lock, &async_io_queue, (MonoObject*)ares);
387 ReleaseSemaphore (io_job_added, 1, NULL);
392 process_io_event (MonoMList *list, int event)
394 MonoSocketAsyncResult *state;
400 state = (MonoSocketAsyncResult *) mono_mlist_get_data (list);
401 if (get_event_from_state (state) == event)
404 list = mono_mlist_next (list);
408 oldlist = mono_mlist_remove_item (oldlist, list);
410 g_print ("Dispatching event %d on socket %d\n", event, state->handle);
412 InterlockedIncrement (&pending_io_items);
413 start_io_thread_or_queue (state);
420 mark_bad_fds (mono_pollfd *pfds, int nfds)
426 for (i = 0; i < nfds; i++) {
431 ret = mono_poll (pfd, 1, 0);
432 if (ret == -1 && errno == EBADF) {
433 pfd->revents |= MONO_POLLNVAL;
435 } else if (ret == 1) {
444 socket_io_poll_main (gpointer p)
446 #define INITIAL_POLLFD_SIZE 1024
447 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
448 SocketIOData *data = p;
455 thread = mono_thread_current ();
457 allocated = INITIAL_POLLFD_SIZE;
458 pfds = g_new0 (mono_pollfd, allocated);
459 INIT_POLLFD (pfds, data->pipe [0], MONO_POLLIN);
460 for (i = 1; i < allocated; i++)
461 INIT_POLLFD (&pfds [i], -1, 0);
471 if (THREAD_WANTS_A_BREAK (thread))
472 mono_thread_interruption_checkpoint ();
475 nsock = mono_poll (pfds, maxfd, -1);
476 } while (nsock == -1 && errno == EINTR);
479 * Apart from EINTR, we only check EBADF, for the rest:
480 * EINVAL: mono_poll() 'protects' us from descriptor
481 * numbers above the limit if using select() by marking
482 * then as MONO_POLLERR. If a system poll() is being
483 * used, the number of descriptor we're passing will not
484 * be over sysconf(_SC_OPEN_MAX), as the error would have
485 * happened when opening.
487 * EFAULT: we own the memory pointed by pfds.
488 * ENOMEM: we're doomed anyway
492 if (nsock == -1 && errno == EBADF) {
493 pfds->revents = 0; /* Just in case... */
494 nsock = mark_bad_fds (pfds, maxfd);
497 if ((pfds->revents & POLL_ERRORS) != 0) {
498 /* We're supposed to die now, as the pipe has been closed */
500 socket_io_cleanup (data);
504 /* Got a new socket */
505 if ((pfds->revents & MONO_POLLIN) != 0) {
508 for (i = 1; i < allocated; i++) {
510 if (pfd->fd == -1 || pfd->fd == data->newpfd->fd)
514 if (i == allocated) {
519 allocated = allocated * 2;
520 pfds = g_renew (mono_pollfd, oldfd, allocated);
522 for (; i < allocated; i++)
523 INIT_POLLFD (&pfds [i], -1, 0);
525 #ifndef PLATFORM_WIN32
526 nread = read (data->pipe [0], one, 1);
528 nread = recv ((SOCKET) data->pipe [0], one, 1, 0);
532 return; /* we're closed */
535 INIT_POLLFD (&pfds [i], data->newpfd->fd, data->newpfd->events);
536 ReleaseSemaphore (data->new_sem, 1, NULL);
545 EnterCriticalSection (&data->io_lock);
546 if (data->inited == 0) {
548 LeaveCriticalSection (&data->io_lock);
549 return; /* cleanup called */
552 for (i = 1; i < maxfd && nsock > 0; i++) {
554 if (pfd->fd == -1 || pfd->revents == 0)
558 list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (pfd->fd));
559 if (list != NULL && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
560 list = process_io_event (list, MONO_POLLIN);
563 if (list != NULL && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
564 list = process_io_event (list, MONO_POLLOUT);
568 mono_g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (pfd->fd), list);
569 pfd->events = get_events_from_list (list);
571 mono_g_hash_table_remove (data->sock_to_state, GINT_TO_POINTER (pfd->fd));
577 LeaveCriticalSection (&data->io_lock);
582 #define EPOLL_ERRORS (EPOLLERR | EPOLLHUP)
584 socket_io_epoll_main (gpointer p)
589 struct epoll_event *events, *evt;
590 const int nevents = 512;
594 epollfd = data->epollfd;
595 thread = mono_thread_current ();
596 events = g_new0 (struct epoll_event, nevents);
601 if (THREAD_WANTS_A_BREAK (thread))
602 mono_thread_interruption_checkpoint ();
605 g_print ("epoll_wait init\n");
607 ready = epoll_wait (epollfd, events, nevents, -1);
611 g_print ("epoll_wait end with %d ready sockets (%d %s).\n", ready, err, (err) ? g_strerror (err) : "");
615 } while (ready == -1 && errno == EINTR);
621 g_warning ("epoll_wait: %d %s\n", err, g_strerror (err));
627 EnterCriticalSection (&data->io_lock);
628 if (data->inited == 0) {
630 g_print ("data->inited == 0\n");
634 return; /* cleanup called */
637 for (i = 0; i < ready; i++) {
643 list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
645 g_print ("Event %d on %d list length: %d\n", evt->events, fd, mono_mlist_length (list));
647 if (list != NULL && (evt->events & (EPOLLIN | EPOLL_ERRORS)) != 0) {
648 list = process_io_event (list, MONO_POLLIN);
651 if (list != NULL && (evt->events & (EPOLLOUT | EPOLL_ERRORS)) != 0) {
652 list = process_io_event (list, MONO_POLLOUT);
656 mono_g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (fd), list);
657 evt->events = get_events_from_list (list);
659 g_print ("MOD %d to %d\n", fd, evt->events);
661 if (epoll_ctl (epollfd, EPOLL_CTL_MOD, fd, evt)) {
662 if (epoll_ctl (epollfd, EPOLL_CTL_ADD, fd, evt) == -1) {
665 g_message ("epoll_ctl(MOD): %d %s fd: %d events: %d", err, g_strerror (err), fd, evt->events);
671 mono_g_hash_table_remove (data->sock_to_state, GINT_TO_POINTER (fd));
673 g_print ("DEL %d\n", fd);
675 epoll_ctl (epollfd, EPOLL_CTL_DEL, fd, evt);
678 LeaveCriticalSection (&data->io_lock);
684 * select/poll wake up when a socket is closed, but epoll just removes
685 * the socket from its internal list without notification.
688 mono_thread_pool_remove_socket (int sock)
690 MonoMList *list, *next;
691 MonoSocketAsyncResult *state;
693 if (socket_io_data.inited == FALSE)
696 EnterCriticalSection (&socket_io_data.io_lock);
697 list = mono_g_hash_table_lookup (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
699 mono_g_hash_table_remove (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
701 LeaveCriticalSection (&socket_io_data.io_lock);
704 state = (MonoSocketAsyncResult *) mono_mlist_get_data (list);
705 if (state->operation == AIO_OP_RECEIVE)
706 state->operation = AIO_OP_RECV_JUST_CALLBACK;
707 else if (state->operation == AIO_OP_SEND)
708 state->operation = AIO_OP_SEND_JUST_CALLBACK;
710 next = mono_mlist_remove_item (list, list);
711 list = process_io_event (list, MONO_POLLIN);
713 process_io_event (list, MONO_POLLOUT);
719 #ifdef PLATFORM_WIN32
721 connect_hack (gpointer x)
723 struct sockaddr_in *addr = (struct sockaddr_in *) x;
726 while (connect ((SOCKET) socket_io_data.pipe [1], (SOCKADDR *) addr, sizeof (struct sockaddr_in))) {
729 g_warning ("Error initializing async. sockets %d.\n", WSAGetLastError ());
730 g_assert (WSAGetLastError ());
737 socket_io_init (SocketIOData *data)
739 #ifdef PLATFORM_WIN32
740 struct sockaddr_in server;
741 struct sockaddr_in client;
747 inited = InterlockedCompareExchange (&data->inited, -1, -1);
751 EnterCriticalSection (&data->io_lock);
752 inited = InterlockedCompareExchange (&data->inited, -1, -1);
754 LeaveCriticalSection (&data->io_lock);
759 data->epoll_disabled = (g_getenv ("MONO_DISABLE_AIO") != NULL);
760 if (FALSE == data->epoll_disabled) {
761 data->epollfd = epoll_create (256);
762 data->epoll_disabled = (data->epollfd == -1);
763 if (data->epoll_disabled && g_getenv ("MONO_DEBUG"))
764 g_message ("epoll_create() failed. Using plain poll().");
769 data->epoll_disabled = TRUE;
772 #ifndef PLATFORM_WIN32
773 if (data->epoll_disabled) {
774 if (pipe (data->pipe) != 0) {
784 srv = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
785 g_assert (srv != INVALID_SOCKET);
786 data->pipe [1] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
787 g_assert (data->pipe [1] != INVALID_SOCKET);
789 server.sin_family = AF_INET;
790 server.sin_addr.s_addr = inet_addr ("127.0.0.1");
792 if (bind (srv, (SOCKADDR *) &server, sizeof (server))) {
793 g_print ("%d\n", WSAGetLastError ());
797 len = sizeof (server);
798 getsockname (srv, (SOCKADDR *) &server, &len);
800 mono_thread_create (mono_get_root_domain (), connect_hack, &server);
801 len = sizeof (server);
802 data->pipe [0] = accept (srv, (SOCKADDR *) &client, &len);
803 g_assert (data->pipe [0] != INVALID_SOCKET);
806 mono_io_max_worker_threads = mono_max_worker_threads / 2;
807 if (mono_io_max_worker_threads < 10)
808 mono_io_max_worker_threads = 10;
810 data->sock_to_state = mono_g_hash_table_new_type (g_direct_hash, g_direct_equal, MONO_HASH_VALUE_GC);
812 if (data->epoll_disabled) {
813 data->new_sem = CreateSemaphore (NULL, 1, 1, NULL);
814 g_assert (data->new_sem != NULL);
816 io_job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
817 g_assert (io_job_added != NULL);
818 if (data->epoll_disabled) {
819 mono_thread_create_internal (mono_get_root_domain (), socket_io_poll_main, data, TRUE);
823 mono_thread_create_internal (mono_get_root_domain (), socket_io_epoll_main, data, TRUE);
826 InterlockedCompareExchange (&data->inited, 1, 0);
827 LeaveCriticalSection (&data->io_lock);
831 socket_io_add_poll (MonoSocketAsyncResult *state)
836 SocketIOData *data = &socket_io_data;
838 #if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD) || defined(PLATFORM_WIN32) || defined(PLATFORM_SOLARIS)
839 /* select() for connect() does not work well on the Mac. Bug #75436. */
840 /* Bug #77637 for the BSD 6 case */
841 /* Bug #78888 for the Windows case */
842 if (state->operation == AIO_OP_CONNECT && state->blocking == TRUE) {
843 start_io_thread_or_queue (state);
847 WaitForSingleObject (data->new_sem, INFINITE);
848 if (data->newpfd == NULL)
849 data->newpfd = g_new0 (mono_pollfd, 1);
851 EnterCriticalSection (&data->io_lock);
852 /* FIXME: 64 bit issue: handle can be a pointer on windows? */
853 list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (state->handle));
855 list = mono_mlist_alloc ((MonoObject*)state);
857 list = mono_mlist_append (list, (MonoObject*)state);
860 events = get_events_from_list (list);
861 INIT_POLLFD (data->newpfd, GPOINTER_TO_INT (state->handle), events);
862 mono_g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (state->handle), list);
863 LeaveCriticalSection (&data->io_lock);
864 *msg = (char) state->operation;
865 #ifndef PLATFORM_WIN32
866 write (data->pipe [1], msg, 1);
868 send ((SOCKET) data->pipe [1], msg, 1, 0);
874 socket_io_add_epoll (MonoSocketAsyncResult *state)
877 SocketIOData *data = &socket_io_data;
878 struct epoll_event event;
882 memset (&event, 0, sizeof (struct epoll_event));
883 fd = GPOINTER_TO_INT (state->handle);
884 EnterCriticalSection (&data->io_lock);
885 list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
887 list = mono_mlist_alloc ((MonoObject*)state);
888 epoll_op = EPOLL_CTL_ADD;
890 list = mono_mlist_append (list, (MonoObject*)state);
891 epoll_op = EPOLL_CTL_MOD;
894 ievt = get_events_from_list (list);
895 if ((ievt & MONO_POLLIN) != 0)
896 event.events |= EPOLLIN;
897 if ((ievt & MONO_POLLOUT) != 0)
898 event.events |= EPOLLOUT;
900 mono_g_hash_table_replace (data->sock_to_state, state->handle, list);
903 g_print ("%s %d with %d\n", epoll_op == EPOLL_CTL_ADD ? "ADD" : "MOD", fd, event.events);
905 if (epoll_ctl (data->epollfd, epoll_op, fd, &event) == -1) {
907 if (epoll_op == EPOLL_CTL_ADD && err == EEXIST) {
908 epoll_op = EPOLL_CTL_MOD;
909 if (epoll_ctl (data->epollfd, epoll_op, fd, &event) == -1) {
910 g_message ("epoll_ctl(MOD): %d %s\n", err, g_strerror (err));
915 LeaveCriticalSection (&data->io_lock);
921 socket_io_add (MonoAsyncResult *ares, MonoSocketAsyncResult *state)
923 socket_io_init (&socket_io_data);
924 MONO_OBJECT_SETREF (state, ares, ares);
926 if (socket_io_data.epoll_disabled == FALSE) {
927 if (socket_io_add_epoll (state))
931 socket_io_add_poll (state);
935 socket_io_filter (MonoObject *target, MonoObject *state)
938 MonoSocketAsyncResult *sock_res = (MonoSocketAsyncResult *) state;
941 if (target == NULL || state == NULL)
944 if (socket_async_call_klass == NULL) {
945 klass = target->vtable->klass;
946 /* Check if it's SocketAsyncCall in System.Net.Sockets
947 * FIXME: check the assembly is signed correctly for extra care
949 if (klass->name [0] == 'S' && strcmp (klass->name, "SocketAsyncCall") == 0
950 && strcmp (mono_image_get_name (klass->image), "System") == 0
951 && klass->nested_in && strcmp (klass->nested_in->name, "Socket") == 0)
952 socket_async_call_klass = klass;
955 if (process_async_call_klass == NULL) {
956 klass = target->vtable->klass;
957 /* Check if it's AsyncReadHandler in System.Diagnostics.Process
958 * FIXME: check the assembly is signed correctly for extra care
960 if (klass->name [0] == 'A' && strcmp (klass->name, "AsyncReadHandler") == 0
961 && strcmp (mono_image_get_name (klass->image), "System") == 0
962 && klass->nested_in && strcmp (klass->nested_in->name, "Process") == 0)
963 process_async_call_klass = klass;
965 /* return both when socket_async_call_klass has not been seen yet and when
966 * the object is not an instance of the class.
968 if (target->vtable->klass != socket_async_call_klass && target->vtable->klass != process_async_call_klass)
971 op = sock_res->operation;
972 if (op < AIO_OP_FIRST || op >= AIO_OP_LAST)
977 #endif /* !DISABLE_SOCKETS */
980 mono_async_invoke (MonoAsyncResult *ares)
982 ASyncCall *ac = (ASyncCall *)ares->object_data;
983 MonoThread *thread = NULL;
984 MonoObject *res, *exc = NULL;
985 MonoArray *out_args = NULL;
987 if (ares->execution_context) {
988 /* use captured ExecutionContext (if available) */
989 thread = mono_thread_current ();
990 MONO_OBJECT_SETREF (ares, original_context, mono_thread_get_execution_context ());
991 mono_thread_set_execution_context (ares->execution_context);
993 ares->original_context = NULL;
997 res = mono_message_invoke (ares->async_delegate, ac->msg, &exc, &out_args);
998 MONO_OBJECT_SETREF (ac, res, res);
999 MONO_OBJECT_SETREF (ac, msg->exc, exc);
1000 MONO_OBJECT_SETREF (ac, out_args, out_args);
1002 ares->completed = 1;
1004 /* call async callback if cb_method != null*/
1005 if (ac->cb_method) {
1006 MonoObject *exc = NULL;
1008 mono_runtime_invoke (ac->cb_method, ac->cb_target, pa, &exc);
1009 /* 'exc' will be the previous ac->msg->exc if not NULL and not
1010 * catched. If catched, this will be set to NULL and the
1011 * exception will not be printed. */
1012 MONO_OBJECT_SETREF (ac->msg, exc, exc);
1015 /* restore original thread execution context if flow isn't suppressed, i.e. non null */
1016 if (ares->original_context) {
1017 mono_thread_set_execution_context (ares->original_context);
1018 ares->original_context = NULL;
1021 /* notify listeners */
1022 mono_monitor_enter ((MonoObject *) ares);
1023 if (ares->handle != NULL) {
1024 ac->wait_event = (gsize) mono_wait_handle_get_handle ((MonoWaitHandle *) ares->handle);
1025 SetEvent ((gpointer)(gsize)ac->wait_event);
1027 mono_monitor_exit ((MonoObject *) ares);
1029 EnterCriticalSection (&ares_lock);
1030 mono_g_hash_table_remove (ares_htable, ares);
1031 LeaveCriticalSection (&ares_lock);
1035 start_idle_threads (MonoAsyncResult *data)
1040 needed = (int) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1);
1042 existing = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
1043 if ((needed - existing) > 0) {
1044 start_tpthread (data);
1046 threadpool_jobs_dec ((MonoObject*)data);
1050 } while ((needed - existing) > 0);
1052 /* If we don't start any thread here, make sure 'data' is processed. */
1054 start_thread_or_queue (data);
1055 threadpool_jobs_dec ((MonoObject*)data);
1060 start_tpthread (MonoAsyncResult *data)
1062 InterlockedIncrement (&mono_worker_threads);
1063 InterlockedIncrement (&busy_worker_threads);
1064 threadpool_jobs_inc ((MonoObject *)data);
1065 mono_thread_create_internal (mono_get_root_domain (), async_invoke_thread, data, TRUE);
1069 mono_thread_pool_init ()
1071 int threads_per_cpu = THREADS_PER_CPU;
1074 if ((int) InterlockedCompareExchange (&tp_inited, 1, 0) == 1)
1077 MONO_GC_REGISTER_ROOT (ares_htable);
1078 MONO_GC_REGISTER_ROOT (socket_io_data.sock_to_state);
1079 InitializeCriticalSection (&socket_io_data.io_lock);
1080 InitializeCriticalSection (&ares_lock);
1081 InitializeCriticalSection (&io_queue_lock);
1082 ares_htable = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC);
1083 job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
1084 g_assert (job_added != NULL);
1085 if (g_getenv ("MONO_THREADS_PER_CPU") != NULL) {
1086 threads_per_cpu = atoi (g_getenv ("MONO_THREADS_PER_CPU"));
1087 if (threads_per_cpu <= 0)
1088 threads_per_cpu = THREADS_PER_CPU;
1091 cpu_count = mono_cpu_count ();
1092 mono_max_worker_threads = 20 + threads_per_cpu * cpu_count;
1093 mono_min_worker_threads = cpu_count; /* 1 idle thread per cpu */
1095 async_call_klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
1096 g_assert (async_call_klass);
1100 mono_thread_pool_add (MonoObject *target, MonoMethodMessage *msg, MonoDelegate *async_callback,
1103 MonoDomain *domain = mono_domain_get ();
1104 MonoAsyncResult *ares;
1107 ac = (ASyncCall*)mono_object_new (mono_domain_get (), async_call_klass);
1108 MONO_OBJECT_SETREF (ac, msg, msg);
1109 MONO_OBJECT_SETREF (ac, state, state);
1111 if (async_callback) {
1112 ac->cb_method = mono_get_delegate_invoke (((MonoObject *)async_callback)->vtable->klass);
1113 MONO_OBJECT_SETREF (ac, cb_target, async_callback);
1116 ares = mono_async_result_new (domain, NULL, ac->state, NULL, (MonoObject*)ac);
1117 MONO_OBJECT_SETREF (ares, async_delegate, target);
1119 if (domain->state == MONO_APPDOMAIN_UNLOADED || domain->state == MONO_APPDOMAIN_UNLOADING)
1122 EnterCriticalSection (&ares_lock);
1123 mono_g_hash_table_insert (ares_htable, ares, ares);
1124 LeaveCriticalSection (&ares_lock);
1126 #ifndef DISABLE_SOCKETS
1127 if (socket_io_filter (target, state)) {
1128 socket_io_add (ares, (MonoSocketAsyncResult *) state);
1133 start_thread_or_queue (ares);
1138 start_thread_or_queue (MonoAsyncResult *ares)
1142 if ((int) InterlockedCompareExchange (&tp_idle_started, 1, 0) == 0) {
1143 threadpool_jobs_inc ((MonoObject*)ares);
1144 mono_thread_create_internal (mono_get_root_domain (), start_idle_threads, ares, TRUE);
1148 busy = (int) InterlockedCompareExchange (&busy_worker_threads, 0, -1);
1149 worker = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
1150 if (worker <= ++busy &&
1151 worker < mono_max_worker_threads) {
1152 start_tpthread (ares);
1154 append_job (&mono_delegate_section, &async_call_queue, (MonoObject*)ares);
1155 ReleaseSemaphore (job_added, 1, NULL);
1160 mono_thread_pool_finish (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc)
1167 /* check if already finished */
1168 mono_monitor_enter ((MonoObject *) ares);
1170 if (ares->endinvoke_called) {
1171 *exc = (MonoObject *)mono_exception_from_name (mono_defaults.corlib, "System",
1172 "InvalidOperationException");
1173 mono_monitor_exit ((MonoObject *) ares);
1177 ares->endinvoke_called = 1;
1178 ac = (ASyncCall *)ares->object_data;
1180 g_assert (ac != NULL);
1182 /* wait until we are really finished */
1183 if (!ares->completed) {
1184 if (ares->handle == NULL) {
1185 ac->wait_event = (gsize)CreateEvent (NULL, TRUE, FALSE, NULL);
1186 g_assert(ac->wait_event != 0);
1187 MONO_OBJECT_SETREF (ares, handle, (MonoObject *) mono_wait_handle_new (mono_object_domain (ares), (gpointer)(gsize)ac->wait_event));
1189 mono_monitor_exit ((MonoObject *) ares);
1190 WaitForSingleObjectEx ((gpointer)(gsize)ac->wait_event, INFINITE, TRUE);
1192 mono_monitor_exit ((MonoObject *) ares);
1195 *exc = ac->msg->exc; /* FIXME: GC add write barrier */
1196 *out_args = ac->out_args;
1202 mono_thread_pool_cleanup (void)
1206 EnterCriticalSection (&mono_delegate_section);
1207 free_queue (&async_call_queue);
1208 release = (gint) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
1209 LeaveCriticalSection (&mono_delegate_section);
1211 ReleaseSemaphore (job_added, release, NULL);
1213 socket_io_cleanup (&socket_io_data);
1217 append_job (CRITICAL_SECTION *cs, TPQueue *list, MonoObject *ar)
1219 threadpool_jobs_inc (ar);
1221 EnterCriticalSection (cs);
1222 if (list->array && (list->next_elem < mono_array_length (list->array))) {
1223 mono_array_setref (list->array, list->next_elem, ar);
1225 LeaveCriticalSection (cs);
1229 MONO_GC_REGISTER_ROOT (list->array);
1230 list->array = mono_array_new_cached (mono_get_root_domain (), mono_defaults.object_class, 16);
1232 int count = list->next_elem - list->first_elem;
1233 /* slide the array or create a larger one if it's full */
1234 if (list->first_elem) {
1235 mono_array_memcpy_refs (list->array, 0, list->array, list->first_elem, count);
1237 MonoArray *newa = mono_array_new_cached (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (list->array) * 2);
1238 mono_array_memcpy_refs (newa, 0, list->array, list->first_elem, count);
1241 list->first_elem = 0;
1242 list->next_elem = count;
1244 mono_array_setref (list->array, list->next_elem, ar);
1246 LeaveCriticalSection (cs);
1251 clear_queue (CRITICAL_SECTION *cs, TPQueue *list, MonoDomain *domain)
1254 EnterCriticalSection (cs);
1256 for (i = list->first_elem; i < list->next_elem; ++i) {
1257 MonoObject *obj = mono_array_get (list->array, MonoObject*, i);
1258 if (obj->vtable->domain == domain) {
1259 unregister_job ((MonoAsyncResult*)obj);
1261 mono_array_set (list->array, MonoObject*, i, NULL);
1262 InterlockedDecrement (&domain->threadpool_jobs);
1269 for (i = list->first_elem; i < list->next_elem; ++i) {
1270 MonoObject *obj = mono_array_get (list->array, MonoObject*, i);
1272 mono_array_set (list->array, MonoObject*, idx++, obj);
1274 list->first_elem = 0;
1275 list->next_elem = count;
1277 LeaveCriticalSection (cs);
1281 * Clean up the threadpool of all domain jobs.
1282 * Can only be called as part of the domain unloading process as
1283 * it will wait for all jobs to be visible to the interruption code.
1286 mono_thread_pool_remove_domain_jobs (MonoDomain *domain, int timeout)
1290 guint32 start_time = 0;
1292 clear_queue (&mono_delegate_section, &async_call_queue, domain);
1293 clear_queue (&io_queue_lock, &async_io_queue, domain);
1296 * There might be some threads out that could be about to execute stuff from the given domain.
1297 * We avoid that by setting up a semaphore to be pulsed by the thread that reaches zero.
1299 sem_handle = CreateSemaphore (NULL, 0, 1, NULL);
1301 domain->cleanup_semaphore = sem_handle;
1303 * The memory barrier here is required to have global ordering between assigning to cleanup_semaphone
1304 * and reading threadpool_jobs.
1305 * Otherwise this thread could read a stale version of threadpool_jobs and wait forever.
1307 mono_memory_write_barrier ();
1309 if (domain->threadpool_jobs && timeout != -1)
1310 start_time = mono_msec_ticks ();
1311 while (domain->threadpool_jobs) {
1312 WaitForSingleObject (sem_handle, timeout);
1313 if (timeout != -1 && (mono_msec_ticks () - start_time) > timeout) {
1319 domain->cleanup_semaphore = NULL;
1320 CloseHandle (sem_handle);
1326 dequeue_job (CRITICAL_SECTION *cs, TPQueue *list)
1331 EnterCriticalSection (cs);
1332 if (!list->array || list->first_elem == list->next_elem) {
1333 LeaveCriticalSection (cs);
1336 ar = mono_array_get (list->array, MonoObject*, list->first_elem);
1337 mono_array_setref (list->array, list->first_elem, NULL);
1339 count = list->next_elem - list->first_elem;
1340 /* reduce the size of the array if it's mostly empty */
1341 if (mono_array_length (list->array) > 16 && count < (mono_array_length (list->array) / 3)) {
1342 MonoArray *newa = mono_array_new_cached (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (list->array) / 2);
1343 mono_array_memcpy_refs (newa, 0, list->array, list->first_elem, count);
1345 list->first_elem = 0;
1346 list->next_elem = count;
1348 LeaveCriticalSection (cs);
1354 free_queue (TPQueue *list)
1357 list->first_elem = list->next_elem = 0;
1361 async_invoke_thread (gpointer data)
1366 const gchar *version;
1368 thread = mono_thread_current ();
1369 version = mono_get_runtime_info ()->framework_version;
1371 MonoAsyncResult *ar;
1373 ar = (MonoAsyncResult *) data;
1375 /* worker threads invokes methods in different domains,
1376 * so we need to set the right domain here */
1377 domain = ((MonoObject *)ar)->vtable->domain;
1381 if (domain->state == MONO_APPDOMAIN_UNLOADED || domain->state == MONO_APPDOMAIN_UNLOADING) {
1382 threadpool_jobs_dec ((MonoObject *)ar);
1383 unregister_job (ar);
1386 mono_thread_push_appdomain_ref (domain);
1387 if (threadpool_jobs_dec ((MonoObject *)ar)) {
1388 unregister_job (ar);
1390 mono_thread_pop_appdomain_ref ();
1394 if (mono_domain_set (domain, FALSE)) {
1397 mono_async_invoke (ar);
1398 ac = (ASyncCall *) ar->object_data;
1400 if (ac->msg->exc != NULL)
1401 mono_unhandled_exception (ac->msg->exc);
1403 mono_domain_set (mono_get_root_domain (), TRUE);
1405 mono_thread_pop_appdomain_ref ();
1406 InterlockedDecrement (&busy_worker_threads);
1407 /* If the callee changes the background status, set it back to TRUE */
1408 if (*version != '1' && !mono_thread_test_state (thread , ThreadState_Background))
1409 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
1413 data = dequeue_job (&mono_delegate_section, &async_call_queue);
1417 int timeout = THREAD_EXIT_TIMEOUT;
1418 guint32 start_time = mono_msec_ticks ();
1421 wr = WaitForSingleObjectEx (job_added, (guint32)timeout, TRUE);
1422 if (THREAD_WANTS_A_BREAK (thread))
1423 mono_thread_interruption_checkpoint ();
1425 timeout -= mono_msec_ticks () - start_time;
1427 if (wr != WAIT_TIMEOUT && wr != WAIT_IO_COMPLETION)
1428 data = dequeue_job (&mono_delegate_section, &async_call_queue);
1430 while (!data && timeout > 0);
1434 workers = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
1435 min = (int) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1);
1437 while (!data && workers <= min) {
1438 WaitForSingleObjectEx (job_added, INFINITE, TRUE);
1439 if (THREAD_WANTS_A_BREAK (thread))
1440 mono_thread_interruption_checkpoint ();
1442 data = dequeue_job (&mono_delegate_section, &async_call_queue);
1443 workers = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
1444 min = (int) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1);
1449 InterlockedDecrement (&mono_worker_threads);
1453 InterlockedIncrement (&busy_worker_threads);
1456 g_assert_not_reached ();
1460 ves_icall_System_Threading_ThreadPool_GetAvailableThreads (gint *workerThreads, gint *completionPortThreads)
1464 MONO_ARCH_SAVE_REGS;
1466 busy = (gint) InterlockedCompareExchange (&busy_worker_threads, 0, -1);
1467 busy_io = (gint) InterlockedCompareExchange (&busy_io_worker_threads, 0, -1);
1468 *workerThreads = mono_max_worker_threads - busy;
1469 *completionPortThreads = mono_io_max_worker_threads - busy_io;
1473 ves_icall_System_Threading_ThreadPool_GetMaxThreads (gint *workerThreads, gint *completionPortThreads)
1475 MONO_ARCH_SAVE_REGS;
1477 *workerThreads = mono_max_worker_threads;
1478 *completionPortThreads = mono_io_max_worker_threads;
1482 ves_icall_System_Threading_ThreadPool_GetMinThreads (gint *workerThreads, gint *completionPortThreads)
1484 gint workers, workers_io;
1486 MONO_ARCH_SAVE_REGS;
1488 workers = (gint) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1);
1489 workers_io = (gint) InterlockedCompareExchange (&mono_io_min_worker_threads, 0, -1);
1491 *workerThreads = workers;
1492 *completionPortThreads = workers_io;
1496 ves_icall_System_Threading_ThreadPool_SetMinThreads (gint workerThreads, gint completionPortThreads)
1498 MONO_ARCH_SAVE_REGS;
1500 if (workerThreads < 0 || workerThreads > mono_max_worker_threads)
1503 if (completionPortThreads < 0 || completionPortThreads > mono_io_max_worker_threads)
1506 InterlockedExchange (&mono_min_worker_threads, workerThreads);
1507 InterlockedExchange (&mono_io_min_worker_threads, completionPortThreads);
1508 mono_thread_create_internal (mono_get_root_domain (), start_idle_threads, NULL, TRUE);
1513 ves_icall_System_Threading_ThreadPool_SetMaxThreads (gint workerThreads, gint completionPortThreads)
1515 MONO_ARCH_SAVE_REGS;
1517 if (workerThreads < mono_max_worker_threads)
1520 if (completionPortThreads < mono_io_max_worker_threads)
1523 InterlockedExchange (&mono_max_worker_threads, workerThreads);
1524 InterlockedExchange (&mono_io_max_worker_threads, completionPortThreads);