2007-03-28 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mono / metadata / threadpool.c
blob6e73e75a661bd89d962129a138c4e94668cf1d23
1 /*
2 * threadpool.c: global thread pool
4 * Authors:
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * (C) 2001-2003 Ximian, Inc.
9 * (c) 2004,2005 Novell, Inc. (http://www.novell.com)
12 #include <config.h>
13 #include <glib.h>
15 #ifdef PLATFORM_WIN32
16 #define WINVER 0x0500
17 #define _WIN32_WINNT 0x0500
18 #endif
20 #define THREADS_PER_CPU 5 /* 20 + THREADS_PER_CPU * number of CPUs */
22 #include <mono/metadata/domain-internals.h>
23 #include <mono/metadata/tabledefs.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/threads-types.h>
26 #include <mono/metadata/threadpool-internals.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/file-io.h>
29 #include <mono/metadata/monitor.h>
30 #include <mono/metadata/mono-mlist.h>
31 #include <mono/metadata/marshal.h>
32 #include <mono/metadata/socket-io.h>
33 #include <mono/io-layer/io-layer.h>
34 #include <mono/os/gc_wrapper.h>
35 #include <errno.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <string.h>
42 #include <mono/utils/mono-poll.h>
43 #ifdef HAVE_EPOLL
44 #include <sys/epoll.h>
45 #endif
47 #include "mono/io-layer/socket-wrappers.h"
49 #include "threadpool.h"
51 #define THREAD_WANTS_A_BREAK(t) ((t->state & (ThreadState_StopRequested | \
52 ThreadState_SuspendRequested)) != 0)
54 #undef EPOLL_DEBUG
56 /* maximum number of worker threads */
57 static int mono_max_worker_threads;
58 static int mono_min_worker_threads;
59 static int mono_io_max_worker_threads;
61 /* current number of worker threads */
62 static int mono_worker_threads = 0;
63 static int io_worker_threads = 0;
65 /* current number of busy threads */
66 static int busy_worker_threads = 0;
67 static int busy_io_worker_threads;
69 /* mono_thread_pool_init called */
70 static int tp_inited;
72 /* we use this to store a reference to the AsyncResult to avoid GC */
73 static MonoGHashTable *ares_htable = NULL;
75 static CRITICAL_SECTION ares_lock;
76 static CRITICAL_SECTION io_queue_lock;
77 static int pending_io_items;
79 typedef struct {
80 CRITICAL_SECTION io_lock; /* access to sock_to_state */
81 int inited;
82 int pipe [2];
83 MonoGHashTable *sock_to_state;
85 HANDLE new_sem; /* access to newpfd and write side of the pipe */
86 mono_pollfd *newpfd;
87 gboolean epoll_disabled;
88 #ifdef HAVE_EPOLL
89 int epollfd;
90 #endif
91 } SocketIOData;
93 static SocketIOData socket_io_data;
95 /* we append a job */
96 static HANDLE job_added;
97 static HANDLE io_job_added;
99 /* Keep in sync with the System.MonoAsyncCall class which provides GC tracking */
100 typedef struct {
101 MonoObject object;
102 MonoMethodMessage *msg;
103 MonoMethod *cb_method;
104 MonoDelegate *cb_target;
105 MonoObject *state;
106 MonoObject *res;
107 MonoArray *out_args;
108 /* This is a HANDLE, we use guint64 so the managed object layout remains constant */
109 guint64 wait_event;
110 } ASyncCall;
112 typedef struct {
113 MonoArray *array;
114 int first_elem;
115 int next_elem;
116 } TPQueue;
118 static void async_invoke_thread (gpointer data);
119 static void append_job (CRITICAL_SECTION *cs, TPQueue *list, MonoObject *ar);
120 static void start_thread_or_queue (MonoAsyncResult *ares);
121 static void mono_async_invoke (MonoAsyncResult *ares);
122 static MonoObject* dequeue_job (CRITICAL_SECTION *cs, TPQueue *list);
123 static void free_queue (TPQueue *list);
125 static TPQueue async_call_queue = {NULL, 0, 0};
126 static TPQueue async_io_queue = {NULL, 0, 0};
128 static MonoClass *async_call_klass;
129 static MonoClass *socket_async_call_klass;
130 static MonoClass *process_async_call_klass;
132 #define INIT_POLLFD(a, b, c) {(a)->fd = b; (a)->events = c; (a)->revents = 0;}
133 enum {
134 AIO_OP_FIRST,
135 AIO_OP_ACCEPT = 0,
136 AIO_OP_CONNECT,
137 AIO_OP_RECEIVE,
138 AIO_OP_RECEIVEFROM,
139 AIO_OP_SEND,
140 AIO_OP_SENDTO,
141 AIO_OP_RECV_JUST_CALLBACK,
142 AIO_OP_SEND_JUST_CALLBACK,
143 AIO_OP_READPIPE,
144 AIO_OP_LAST
147 static void
148 socket_io_cleanup (SocketIOData *data)
150 gint release;
152 if (data->inited == 0)
153 return;
155 EnterCriticalSection (&data->io_lock);
156 data->inited = 0;
157 #ifdef PLATFORM_WIN32
158 closesocket (data->pipe [0]);
159 closesocket (data->pipe [1]);
160 #else
161 close (data->pipe [0]);
162 close (data->pipe [1]);
163 #endif
164 data->pipe [0] = -1;
165 data->pipe [1] = -1;
166 if (data->new_sem)
167 CloseHandle (data->new_sem);
168 data->new_sem = NULL;
169 mono_g_hash_table_destroy (data->sock_to_state);
170 data->sock_to_state = NULL;
171 free_queue (&async_io_queue);
172 release = (gint) InterlockedCompareExchange (&io_worker_threads, 0, -1);
173 if (io_job_added)
174 ReleaseSemaphore (io_job_added, release, NULL);
175 g_free (data->newpfd);
176 data->newpfd = NULL;
177 #ifdef HAVE_EPOLL
178 if (FALSE == data->epoll_disabled)
179 close (data->epollfd);
180 #endif
181 LeaveCriticalSection (&data->io_lock);
184 static int
185 get_event_from_state (MonoSocketAsyncResult *state)
187 switch (state->operation) {
188 case AIO_OP_ACCEPT:
189 case AIO_OP_RECEIVE:
190 case AIO_OP_RECV_JUST_CALLBACK:
191 case AIO_OP_RECEIVEFROM:
192 case AIO_OP_READPIPE:
193 return MONO_POLLIN;
194 case AIO_OP_SEND:
195 case AIO_OP_SEND_JUST_CALLBACK:
196 case AIO_OP_SENDTO:
197 case AIO_OP_CONNECT:
198 return MONO_POLLOUT;
199 default: /* Should never happen */
200 g_print ("get_event_from_state: unknown value in switch!!!\n");
201 return 0;
205 static int
206 get_events_from_list (MonoMList *list)
208 MonoSocketAsyncResult *state;
209 int events = 0;
211 while (list && (state = (MonoSocketAsyncResult *)mono_mlist_get_data (list))) {
212 events |= get_event_from_state (state);
213 list = mono_mlist_next (list);
216 return events;
219 #define ICALL_RECV(x) ves_icall_System_Net_Sockets_Socket_Receive_internal (\
220 (SOCKET) x->handle, x->buffer, x->offset, x->size,\
221 x->socket_flags, &x->error);
223 #define ICALL_SEND(x) ves_icall_System_Net_Sockets_Socket_Send_internal (\
224 (SOCKET) x->handle, x->buffer, x->offset, x->size,\
225 x->socket_flags, &x->error);
227 static void
228 async_invoke_io_thread (gpointer data)
230 MonoDomain *domain;
231 MonoThread *thread;
232 thread = mono_thread_current ();
233 thread->threadpool_thread = TRUE;
234 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
236 for (;;) {
237 MonoSocketAsyncResult *state;
238 MonoAsyncResult *ar;
240 state = (MonoSocketAsyncResult *) data;
241 if (state) {
242 InterlockedDecrement (&pending_io_items);
243 ar = state->ares;
244 switch (state->operation) {
245 case AIO_OP_RECEIVE:
246 state->total = ICALL_RECV (state);
247 break;
248 case AIO_OP_SEND:
249 state->total = ICALL_SEND (state);
250 break;
253 /* worker threads invokes methods in different domains,
254 * so we need to set the right domain here */
255 domain = ((MonoObject *)ar)->vtable->domain;
256 mono_thread_push_appdomain_ref (domain);
257 if (mono_domain_set (domain, FALSE)) {
258 ASyncCall *ac;
260 mono_async_invoke (ar);
261 ac = (ASyncCall *) ar->object_data;
263 if (ac->msg->exc != NULL)
264 mono_unhandled_exception (ac->msg->exc);
266 mono_domain_set (mono_get_root_domain (), TRUE);
268 mono_thread_pop_appdomain_ref ();
269 InterlockedDecrement (&busy_io_worker_threads);
272 data = dequeue_job (&io_queue_lock, &async_io_queue);
274 if (!data) {
275 guint32 wr;
276 int timeout = 10000;
277 guint32 start_time = GetTickCount ();
279 do {
280 wr = WaitForSingleObjectEx (io_job_added, (guint32)timeout, TRUE);
281 if (THREAD_WANTS_A_BREAK (thread))
282 mono_thread_interruption_checkpoint ();
284 timeout -= GetTickCount () - start_time;
286 if (wr != WAIT_TIMEOUT)
287 data = dequeue_job (&io_queue_lock, &async_io_queue);
289 while (!data && timeout > 0);
292 if (!data) {
293 if (InterlockedDecrement (&io_worker_threads) < 2) {
294 /* If we have pending items, keep the thread alive */
295 if (InterlockedCompareExchange (&pending_io_items, 0, 0) != 0) {
296 InterlockedIncrement (&io_worker_threads);
297 continue;
300 return;
303 InterlockedIncrement (&busy_io_worker_threads);
306 g_assert_not_reached ();
309 static void
310 start_io_thread_or_queue (MonoSocketAsyncResult *ares)
312 int busy, worker;
313 MonoDomain *domain;
315 busy = (int) InterlockedCompareExchange (&busy_io_worker_threads, 0, -1);
316 worker = (int) InterlockedCompareExchange (&io_worker_threads, 0, -1);
317 if (worker <= ++busy &&
318 worker < mono_io_max_worker_threads) {
319 InterlockedIncrement (&busy_io_worker_threads);
320 InterlockedIncrement (&io_worker_threads);
321 domain = ((ares) ? ((MonoObject *) ares)->vtable->domain : mono_domain_get ());
322 mono_thread_create (mono_get_root_domain (), async_invoke_io_thread, ares);
323 } else {
324 append_job (&io_queue_lock, &async_io_queue, (MonoObject*)ares);
325 ReleaseSemaphore (io_job_added, 1, NULL);
329 static MonoMList *
330 process_io_event (MonoMList *list, int event)
332 MonoSocketAsyncResult *state;
333 MonoMList *oldlist;
335 oldlist = list;
336 state = NULL;
337 while (list) {
338 state = (MonoSocketAsyncResult *) mono_mlist_get_data (list);
339 if (get_event_from_state (state) == event)
340 break;
342 list = mono_mlist_next (list);
345 if (list != NULL) {
346 oldlist = mono_mlist_remove_item (oldlist, list);
347 #ifdef EPOLL_DEBUG
348 g_print ("Dispatching event %d on socket %d\n", event, state->handle);
349 #endif
350 InterlockedIncrement (&pending_io_items);
351 start_io_thread_or_queue (state);
354 return oldlist;
357 static int
358 mark_bad_fds (mono_pollfd *pfds, int nfds)
360 int i, ret;
361 mono_pollfd *pfd;
362 int count = 0;
364 for (i = 0; i < nfds; i++) {
365 pfd = &pfds [i];
366 if (pfd->fd == -1)
367 continue;
369 ret = mono_poll (pfd, 1, 0);
370 if (ret == -1 && errno == EBADF) {
371 pfd->revents |= MONO_POLLNVAL;
372 count++;
373 } else if (ret == 1) {
374 count++;
378 return count;
381 static void
382 socket_io_poll_main (gpointer p)
384 #define INITIAL_POLLFD_SIZE 1024
385 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
386 SocketIOData *data = p;
387 mono_pollfd *pfds;
388 gint maxfd = 1;
389 gint allocated;
390 gint i;
391 MonoThread *thread;
393 thread = mono_thread_current ();
394 thread->threadpool_thread = TRUE;
395 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
397 allocated = INITIAL_POLLFD_SIZE;
398 pfds = g_new0 (mono_pollfd, allocated);
399 INIT_POLLFD (pfds, data->pipe [0], MONO_POLLIN);
400 for (i = 1; i < allocated; i++)
401 INIT_POLLFD (&pfds [i], -1, 0);
403 while (1) {
404 int nsock = 0;
405 mono_pollfd *pfd;
406 char one [1];
407 MonoMList *list;
409 do {
410 if (nsock == -1) {
411 if (THREAD_WANTS_A_BREAK (thread))
412 mono_thread_interruption_checkpoint ();
415 nsock = mono_poll (pfds, maxfd, -1);
416 } while (nsock == -1 && errno == EINTR);
419 * Apart from EINTR, we only check EBADF, for the rest:
420 * EINVAL: mono_poll() 'protects' us from descriptor
421 * numbers above the limit if using select() by marking
422 * then as MONO_POLLERR. If a system poll() is being
423 * used, the number of descriptor we're passing will not
424 * be over sysconf(_SC_OPEN_MAX), as the error would have
425 * happened when opening.
427 * EFAULT: we own the memory pointed by pfds.
428 * ENOMEM: we're doomed anyway
432 if (nsock == -1 && errno == EBADF) {
433 pfds->revents = 0; /* Just in case... */
434 nsock = mark_bad_fds (pfds, maxfd);
437 if ((pfds->revents & POLL_ERRORS) != 0) {
438 /* We're supposed to die now, as the pipe has been closed */
439 g_free (pfds);
440 socket_io_cleanup (data);
441 return;
444 /* Got a new socket */
445 if ((pfds->revents & MONO_POLLIN) != 0) {
446 int nread;
448 for (i = 1; i < allocated; i++) {
449 pfd = &pfds [i];
450 if (pfd->fd == -1 || pfd->fd == data->newpfd->fd)
451 break;
454 if (i == allocated) {
455 mono_pollfd *oldfd;
457 oldfd = pfds;
458 i = allocated;
459 allocated = allocated * 2;
460 pfds = g_renew (mono_pollfd, oldfd, allocated);
461 g_free (oldfd);
462 for (; i < allocated; i++)
463 INIT_POLLFD (&pfds [i], -1, 0);
465 #ifndef PLATFORM_WIN32
466 nread = read (data->pipe [0], one, 1);
467 #else
468 nread = recv ((SOCKET) data->pipe [0], one, 1, 0);
469 #endif
470 if (nread <= 0) {
471 g_free (pfds);
472 return; /* we're closed */
475 INIT_POLLFD (&pfds [i], data->newpfd->fd, data->newpfd->events);
476 ReleaseSemaphore (data->new_sem, 1, NULL);
477 if (i >= maxfd)
478 maxfd = i + 1;
479 nsock--;
482 if (nsock == 0)
483 continue;
485 EnterCriticalSection (&data->io_lock);
486 if (data->inited == 0) {
487 g_free (pfds);
488 LeaveCriticalSection (&data->io_lock);
489 return; /* cleanup called */
492 for (i = 1; i < maxfd && nsock > 0; i++) {
493 pfd = &pfds [i];
494 if (pfd->fd == -1 || pfd->revents == 0)
495 continue;
497 nsock--;
498 list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (pfd->fd));
499 if (list != NULL && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
500 list = process_io_event (list, MONO_POLLIN);
503 if (list != NULL && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
504 list = process_io_event (list, MONO_POLLOUT);
507 if (list != NULL) {
508 mono_g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (pfd->fd), list);
509 pfd->events = get_events_from_list (list);
510 } else {
511 mono_g_hash_table_remove (data->sock_to_state, GINT_TO_POINTER (pfd->fd));
512 pfd->fd = -1;
513 if (i == maxfd - 1)
514 maxfd--;
517 LeaveCriticalSection (&data->io_lock);
521 #ifdef HAVE_EPOLL
522 #define EPOLL_ERRORS (EPOLLERR | EPOLLHUP)
523 static void
524 socket_io_epoll_main (gpointer p)
526 SocketIOData *data;
527 int epollfd;
528 MonoThread *thread;
529 struct epoll_event *events, *evt;
530 const int nevents = 512;
531 int ready = 0, i;
533 data = p;
534 epollfd = data->epollfd;
535 thread = mono_thread_current ();
536 thread->threadpool_thread = TRUE;
537 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
538 events = g_new0 (struct epoll_event, nevents);
540 while (1) {
541 do {
542 if (ready == -1) {
543 if (THREAD_WANTS_A_BREAK (thread))
544 mono_thread_interruption_checkpoint ();
546 #ifdef EPOLL_DEBUG
547 g_print ("epoll_wait init\n");
548 #endif
549 ready = epoll_wait (epollfd, events, nevents, -1);
550 #ifdef EPOLL_DEBUG
552 int err = errno;
553 g_print ("epoll_wait end with %d ready sockets (%d %s).\n", ready, err, (err) ? g_strerror (err) : "");
554 errno = err;
556 #endif
557 } while (ready == -1 && errno == EINTR);
559 if (ready == -1) {
560 int err = errno;
561 g_free (events);
562 if (err != EBADF)
563 g_warning ("epoll_wait: %d %s\n", err, g_strerror (err));
565 close (epollfd);
566 return;
569 EnterCriticalSection (&data->io_lock);
570 if (data->inited == 0) {
571 #ifdef EPOLL_DEBUG
572 g_print ("data->inited == 0\n");
573 #endif
574 g_free (events);
575 close (epollfd);
576 return; /* cleanup called */
579 for (i = 0; i < ready; i++) {
580 int fd;
581 MonoMList *list;
583 evt = &events [i];
584 fd = evt->data.fd;
585 list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
586 #ifdef EPOLL_DEBUG
587 g_print ("Event %d on %d list length: %d\n", evt->events, fd, mono_mlist_length (list));
588 #endif
589 if (list != NULL && (evt->events & (EPOLLIN | EPOLL_ERRORS)) != 0) {
590 list = process_io_event (list, MONO_POLLIN);
593 if (list != NULL && (evt->events & (EPOLLOUT | EPOLL_ERRORS)) != 0) {
594 list = process_io_event (list, MONO_POLLOUT);
597 if (list != NULL) {
598 mono_g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (fd), list);
599 evt->events = get_events_from_list (list);
600 #ifdef EPOLL_DEBUG
601 g_print ("MOD %d to %d\n", fd, evt->events);
602 #endif
603 if (epoll_ctl (epollfd, EPOLL_CTL_MOD, fd, evt)) {
604 if (epoll_ctl (epollfd, EPOLL_CTL_ADD, fd, evt) == -1) {
605 #ifdef EPOLL_DEBUG
606 int err = errno;
607 g_message ("epoll_ctl(MOD): %d %s fd: %d events: %d", err, g_strerror (err), fd, evt->events);
608 errno = err;
609 #endif
612 } else {
613 mono_g_hash_table_remove (data->sock_to_state, GINT_TO_POINTER (fd));
614 #ifdef EPOLL_DEBUG
615 g_print ("DEL %d\n", fd);
616 #endif
617 epoll_ctl (epollfd, EPOLL_CTL_DEL, fd, evt);
620 LeaveCriticalSection (&data->io_lock);
623 #endif
626 * select/poll wake up when a socket is closed, but epoll just removes
627 * the socket from its internal list without notification.
629 void
630 mono_thread_pool_remove_socket (int sock)
632 #ifdef HAVE_EPOLL
633 MonoMList *list, *next;
634 MonoSocketAsyncResult *state;
636 if (socket_io_data.epoll_disabled == TRUE || socket_io_data.inited == FALSE)
637 return;
639 EnterCriticalSection (&socket_io_data.io_lock);
640 list = mono_g_hash_table_lookup (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
641 if (list) {
642 mono_g_hash_table_remove (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
644 LeaveCriticalSection (&socket_io_data.io_lock);
646 while (list) {
647 state = (MonoSocketAsyncResult *) mono_mlist_get_data (list);
648 if (state->operation == AIO_OP_RECEIVE)
649 state->operation = AIO_OP_RECV_JUST_CALLBACK;
650 else if (state->operation == AIO_OP_SEND)
651 state->operation = AIO_OP_SEND_JUST_CALLBACK;
653 next = mono_mlist_remove_item (list, list);
654 list = process_io_event (list, MONO_POLLIN);
655 if (list)
656 process_io_event (list, MONO_POLLOUT);
658 list = next;
660 #endif
663 #ifdef PLATFORM_WIN32
664 static void
665 connect_hack (gpointer x)
667 struct sockaddr_in *addr = (struct sockaddr_in *) x;
668 int count = 0;
670 while (connect ((SOCKET) socket_io_data.pipe [1], (SOCKADDR *) addr, sizeof (struct sockaddr_in))) {
671 Sleep (500);
672 if (++count > 3) {
673 g_warning ("Error initializing async. sockets %d.\n", WSAGetLastError ());
674 g_assert (WSAGetLastError ());
678 #endif
680 static void
681 socket_io_init (SocketIOData *data)
683 #ifdef PLATFORM_WIN32
684 struct sockaddr_in server;
685 struct sockaddr_in client;
686 SOCKET srv;
687 int len;
688 #endif
689 int inited;
691 inited = InterlockedCompareExchange (&data->inited, -1, -1);
692 if (inited == 1)
693 return;
695 EnterCriticalSection (&data->io_lock);
696 inited = InterlockedCompareExchange (&data->inited, -1, -1);
697 if (inited == 1) {
698 LeaveCriticalSection (&data->io_lock);
699 return;
702 #ifdef HAVE_EPOLL
703 data->epoll_disabled = (g_getenv ("MONO_DISABLE_AIO") != NULL);
704 if (FALSE == data->epoll_disabled) {
705 data->epollfd = epoll_create (256);
706 data->epoll_disabled = (data->epollfd == -1);
707 if (data->epoll_disabled && g_getenv ("MONO_DEBUG"))
708 g_message ("epoll_create() failed. Using plain poll().");
709 } else {
710 data->epollfd = -1;
712 #else
713 data->epoll_disabled = TRUE;
714 #endif
716 #ifndef PLATFORM_WIN32
717 if (data->epoll_disabled) {
718 if (pipe (data->pipe) != 0) {
719 int err = errno;
720 perror ("mono");
721 g_assert (err);
723 } else {
724 data->pipe [0] = -1;
725 data->pipe [1] = -1;
727 #else
728 srv = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
729 g_assert (srv != INVALID_SOCKET);
730 data->pipe [1] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
731 g_assert (data->pipe [1] != INVALID_SOCKET);
733 server.sin_family = AF_INET;
734 server.sin_addr.s_addr = inet_addr ("127.0.0.1");
735 server.sin_port = 0;
736 if (bind (srv, (SOCKADDR *) &server, sizeof (server))) {
737 g_print ("%d\n", WSAGetLastError ());
738 g_assert (1 != 0);
741 len = sizeof (server);
742 getsockname (srv, (SOCKADDR *) &server, &len);
743 listen (srv, 1);
744 mono_thread_create (mono_get_root_domain (), connect_hack, &server);
745 len = sizeof (server);
746 data->pipe [0] = accept (srv, (SOCKADDR *) &client, &len);
747 g_assert (data->pipe [0] != INVALID_SOCKET);
748 closesocket (srv);
749 #endif
750 mono_io_max_worker_threads = mono_max_worker_threads / 2;
751 if (mono_io_max_worker_threads < 10)
752 mono_io_max_worker_threads = 10;
754 data->sock_to_state = mono_g_hash_table_new_type (g_direct_hash, g_direct_equal, MONO_HASH_VALUE_GC);
756 if (data->epoll_disabled) {
757 data->new_sem = CreateSemaphore (NULL, 1, 1, NULL);
758 g_assert (data->new_sem != NULL);
760 io_job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
761 g_assert (io_job_added != NULL);
762 InitializeCriticalSection (&io_queue_lock);
763 if (data->epoll_disabled) {
764 mono_thread_create (mono_get_root_domain (), socket_io_poll_main, data);
766 #ifdef HAVE_EPOLL
767 else {
768 mono_thread_create (mono_get_root_domain (), socket_io_epoll_main, data);
770 #endif
771 InterlockedCompareExchange (&data->inited, 1, 0);
772 LeaveCriticalSection (&data->io_lock);
775 static void
776 socket_io_add_poll (MonoSocketAsyncResult *state)
778 int events;
779 char msg [1];
780 MonoMList *list;
781 SocketIOData *data = &socket_io_data;
783 #if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD6) || defined(PLATFORM_WIN32)
784 /* select() for connect() does not work well on the Mac. Bug #75436. */
785 /* Bug #77637 for the BSD 6 case */
786 /* Bug #78888 for the Windows case */
787 if (state->operation == AIO_OP_CONNECT && state->blocking == TRUE) {
788 start_io_thread_or_queue (state);
789 return;
791 #endif
792 WaitForSingleObject (data->new_sem, INFINITE);
793 if (data->newpfd == NULL)
794 data->newpfd = g_new0 (mono_pollfd, 1);
796 EnterCriticalSection (&data->io_lock);
797 /* FIXME: 64 bit issue: handle can be a pointer on windows? */
798 list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (state->handle));
799 if (list == NULL) {
800 list = mono_mlist_alloc ((MonoObject*)state);
801 } else {
802 list = mono_mlist_append (list, (MonoObject*)state);
805 events = get_events_from_list (list);
806 INIT_POLLFD (data->newpfd, GPOINTER_TO_INT (state->handle), events);
807 mono_g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (state->handle), list);
808 LeaveCriticalSection (&data->io_lock);
809 *msg = (char) state->operation;
810 #ifndef PLATFORM_WIN32
811 write (data->pipe [1], msg, 1);
812 #else
813 send ((SOCKET) data->pipe [1], msg, 1, 0);
814 #endif
817 #ifdef HAVE_EPOLL
818 static gboolean
819 socket_io_add_epoll (MonoSocketAsyncResult *state)
821 MonoMList *list;
822 SocketIOData *data = &socket_io_data;
823 struct epoll_event event;
824 int epoll_op, ievt;
825 int fd;
827 memset (&event, 0, sizeof (struct epoll_event));
828 fd = GPOINTER_TO_INT (state->handle);
829 EnterCriticalSection (&data->io_lock);
830 list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
831 if (list == NULL) {
832 list = mono_mlist_alloc ((MonoObject*)state);
833 epoll_op = EPOLL_CTL_ADD;
834 } else {
835 list = mono_mlist_append (list, (MonoObject*)state);
836 epoll_op = EPOLL_CTL_MOD;
839 ievt = get_events_from_list (list);
840 if ((ievt & MONO_POLLIN) != 0)
841 event.events |= EPOLLIN;
842 if ((ievt & MONO_POLLOUT) != 0)
843 event.events |= EPOLLOUT;
845 mono_g_hash_table_replace (data->sock_to_state, state->handle, list);
846 event.data.fd = fd;
847 #ifdef EPOLL_DEBUG
848 g_print ("%s %d with %d\n", epoll_op == EPOLL_CTL_ADD ? "ADD" : "MOD", fd, event.events);
849 #endif
850 if (epoll_ctl (data->epollfd, epoll_op, fd, &event) == -1) {
851 int err = errno;
852 if (epoll_op == EPOLL_CTL_ADD && err == EEXIST) {
853 epoll_op = EPOLL_CTL_MOD;
854 if (epoll_ctl (data->epollfd, epoll_op, fd, &event) == -1) {
855 g_message ("epoll_ctl(MOD): %d %s\n", err, g_strerror (err));
860 LeaveCriticalSection (&data->io_lock);
861 return TRUE;
863 #endif
865 static void
866 socket_io_add (MonoAsyncResult *ares, MonoSocketAsyncResult *state)
868 socket_io_init (&socket_io_data);
869 MONO_OBJECT_SETREF (state, ares, ares);
870 #ifdef HAVE_EPOLL
871 if (socket_io_data.epoll_disabled == FALSE) {
872 if (socket_io_add_epoll (state))
873 return;
875 #endif
876 socket_io_add_poll (state);
879 static gboolean
880 socket_io_filter (MonoObject *target, MonoObject *state)
882 gint op;
883 MonoSocketAsyncResult *sock_res = (MonoSocketAsyncResult *) state;
884 MonoClass *klass;
886 if (target == NULL || state == NULL)
887 return FALSE;
889 if (socket_async_call_klass == NULL) {
890 klass = target->vtable->klass;
891 /* Check if it's SocketAsyncCall in System.Net.Sockets
892 * FIXME: check the assembly is signed correctly for extra care
894 if (klass->name [0] == 'S' && strcmp (klass->name, "SocketAsyncCall") == 0
895 && strcmp (mono_image_get_name (klass->image), "System") == 0
896 && klass->nested_in && strcmp (klass->nested_in->name, "Socket") == 0)
897 socket_async_call_klass = klass;
900 if (process_async_call_klass == NULL) {
901 klass = target->vtable->klass;
902 /* Check if it's AsyncReadHandler in System.Diagnostics.Process
903 * FIXME: check the assembly is signed correctly for extra care
905 if (klass->name [0] == 'A' && strcmp (klass->name, "AsyncReadHandler") == 0
906 && strcmp (mono_image_get_name (klass->image), "System") == 0
907 && klass->nested_in && strcmp (klass->nested_in->name, "Process") == 0)
908 process_async_call_klass = klass;
910 /* return both when socket_async_call_klass has not been seen yet and when
911 * the object is not an instance of the class.
913 if (target->vtable->klass != socket_async_call_klass && target->vtable->klass != process_async_call_klass)
914 return FALSE;
916 op = sock_res->operation;
917 if (op < AIO_OP_FIRST || op >= AIO_OP_LAST)
918 return FALSE;
920 return TRUE;
923 static void
924 mono_async_invoke (MonoAsyncResult *ares)
926 ASyncCall *ac = (ASyncCall *)ares->object_data;
927 MonoThread *thread = NULL;
928 MonoObject *res, *exc = NULL;
929 MonoArray *out_args = NULL;
931 if (ares->execution_context) {
932 /* use captured ExecutionContext (if available) */
933 thread = mono_thread_current ();
934 MONO_OBJECT_SETREF (ares, original_context, thread->execution_context);
935 MONO_OBJECT_SETREF (thread, execution_context, ares->execution_context);
936 } else {
937 ares->original_context = NULL;
940 ac->msg->exc = NULL;
941 res = mono_message_invoke (ares->async_delegate, ac->msg, &exc, &out_args);
942 MONO_OBJECT_SETREF (ac, res, res);
943 MONO_OBJECT_SETREF (ac, msg->exc, exc);
944 MONO_OBJECT_SETREF (ac, out_args, out_args);
946 ares->completed = 1;
948 /* call async callback if cb_method != null*/
949 if (ac->cb_method) {
950 MonoObject *exc = NULL;
951 void *pa = &ares;
952 mono_runtime_invoke (ac->cb_method, ac->cb_target, pa, &exc);
953 /* 'exc' will be the previous ac->msg->exc if not NULL and not
954 * catched. If catched, this will be set to NULL and the
955 * exception will not be printed. */
956 MONO_OBJECT_SETREF (ac->msg, exc, exc);
959 /* restore original thread execution context if flow isn't suppressed, i.e. non null */
960 if (ares->original_context) {
961 MONO_OBJECT_SETREF (thread, execution_context, ares->original_context);
962 ares->original_context = NULL;
965 /* notify listeners */
966 mono_monitor_enter ((MonoObject *) ares);
967 if (ares->handle != NULL) {
968 ac->wait_event = (gsize) mono_wait_handle_get_handle ((MonoWaitHandle *) ares->handle);
969 SetEvent ((gpointer)(gsize)ac->wait_event);
971 mono_monitor_exit ((MonoObject *) ares);
973 EnterCriticalSection (&ares_lock);
974 mono_g_hash_table_remove (ares_htable, ares);
975 LeaveCriticalSection (&ares_lock);
978 void
979 mono_thread_pool_init ()
981 SYSTEM_INFO info;
982 int threads_per_cpu = THREADS_PER_CPU;
984 if ((int) InterlockedCompareExchange (&tp_inited, 1, 0) == 1)
985 return;
987 MONO_GC_REGISTER_ROOT (ares_htable);
988 MONO_GC_REGISTER_ROOT (socket_io_data.sock_to_state);
989 InitializeCriticalSection (&socket_io_data.io_lock);
990 InitializeCriticalSection (&ares_lock);
991 ares_htable = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC);
992 job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
993 g_assert (job_added != NULL);
994 GetSystemInfo (&info);
995 if (g_getenv ("MONO_THREADS_PER_CPU") != NULL) {
996 threads_per_cpu = atoi (g_getenv ("MONO_THREADS_PER_CPU"));
997 if (threads_per_cpu <= 0)
998 threads_per_cpu = THREADS_PER_CPU;
1001 mono_max_worker_threads = 20 + threads_per_cpu * info.dwNumberOfProcessors;
1003 async_call_klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
1004 g_assert (async_call_klass);
1007 MonoAsyncResult *
1008 mono_thread_pool_add (MonoObject *target, MonoMethodMessage *msg, MonoDelegate *async_callback,
1009 MonoObject *state)
1011 MonoDomain *domain = mono_domain_get ();
1012 MonoAsyncResult *ares;
1013 ASyncCall *ac;
1015 ac = (ASyncCall*)mono_object_new (mono_domain_get (), async_call_klass);
1016 MONO_OBJECT_SETREF (ac, msg, msg);
1017 MONO_OBJECT_SETREF (ac, state, state);
1019 if (async_callback) {
1020 ac->cb_method = mono_get_delegate_invoke (((MonoObject *)async_callback)->vtable->klass);
1021 MONO_OBJECT_SETREF (ac, cb_target, async_callback);
1024 ares = mono_async_result_new (domain, NULL, ac->state, NULL, (MonoObject*)ac);
1025 MONO_OBJECT_SETREF (ares, async_delegate, target);
1027 EnterCriticalSection (&ares_lock);
1028 mono_g_hash_table_insert (ares_htable, ares, ares);
1029 LeaveCriticalSection (&ares_lock);
1031 if (socket_io_filter (target, state)) {
1032 socket_io_add (ares, (MonoSocketAsyncResult *) state);
1033 return ares;
1036 start_thread_or_queue (ares);
1037 return ares;
1040 static void
1041 start_thread_or_queue (MonoAsyncResult *ares)
1043 int busy, worker;
1045 busy = (int) InterlockedCompareExchange (&busy_worker_threads, 0, -1);
1046 worker = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
1047 if (worker <= ++busy &&
1048 worker < mono_max_worker_threads) {
1049 InterlockedIncrement (&mono_worker_threads);
1050 InterlockedIncrement (&busy_worker_threads);
1051 mono_thread_create (mono_get_root_domain (), async_invoke_thread, ares);
1052 } else {
1053 append_job (&mono_delegate_section, &async_call_queue, (MonoObject*)ares);
1054 ReleaseSemaphore (job_added, 1, NULL);
1058 MonoObject *
1059 mono_thread_pool_finish (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc)
1061 ASyncCall *ac;
1063 *exc = NULL;
1064 *out_args = NULL;
1066 /* check if already finished */
1067 mono_monitor_enter ((MonoObject *) ares);
1069 if (ares->endinvoke_called) {
1070 *exc = (MonoObject *)mono_exception_from_name (mono_defaults.corlib, "System",
1071 "InvalidOperationException");
1072 mono_monitor_exit ((MonoObject *) ares);
1073 return NULL;
1076 ares->endinvoke_called = 1;
1077 ac = (ASyncCall *)ares->object_data;
1079 g_assert (ac != NULL);
1081 /* wait until we are really finished */
1082 if (!ares->completed) {
1083 if (ares->handle == NULL) {
1084 ac->wait_event = (gsize)CreateEvent (NULL, TRUE, FALSE, NULL);
1085 g_assert(ac->wait_event != 0);
1086 MONO_OBJECT_SETREF (ares, handle, (MonoObject *) mono_wait_handle_new (mono_object_domain (ares), (gpointer)(gsize)ac->wait_event));
1088 mono_monitor_exit ((MonoObject *) ares);
1089 WaitForSingleObjectEx ((gpointer)(gsize)ac->wait_event, INFINITE, TRUE);
1090 } else {
1091 mono_monitor_exit ((MonoObject *) ares);
1094 *exc = ac->msg->exc; /* FIXME: GC add write barrier */
1095 *out_args = ac->out_args;
1097 return ac->res;
1100 void
1101 mono_thread_pool_cleanup (void)
1103 gint release;
1105 EnterCriticalSection (&mono_delegate_section);
1106 free_queue (&async_call_queue);
1107 release = (gint) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
1108 LeaveCriticalSection (&mono_delegate_section);
1109 if (job_added)
1110 ReleaseSemaphore (job_added, release, NULL);
1112 socket_io_cleanup (&socket_io_data);
1115 static void
1116 append_job (CRITICAL_SECTION *cs, TPQueue *list, MonoObject *ar)
1118 EnterCriticalSection (cs);
1119 if (list->array && (list->next_elem < mono_array_length (list->array))) {
1120 mono_array_setref (list->array, list->next_elem, ar);
1121 list->next_elem++;
1122 LeaveCriticalSection (cs);
1123 return;
1125 if (!list->array) {
1126 MONO_GC_REGISTER_ROOT (list->array);
1127 list->array = mono_array_new (mono_get_root_domain (), mono_defaults.object_class, 16);
1128 } else {
1129 int count = list->next_elem - list->first_elem;
1130 /* slide the array or create a larger one if it's full */
1131 if (list->first_elem) {
1132 mono_array_memcpy_refs (list->array, 0, list->array, list->first_elem, count);
1133 } else {
1134 MonoArray *newa = mono_array_new (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (list->array) * 2);
1135 mono_array_memcpy_refs (newa, 0, list->array, list->first_elem, count);
1136 list->array = newa;
1138 list->first_elem = 0;
1139 list->next_elem = count;
1141 mono_array_setref (list->array, list->next_elem, ar);
1142 list->next_elem++;
1143 LeaveCriticalSection (cs);
1146 static MonoObject*
1147 dequeue_job (CRITICAL_SECTION *cs, TPQueue *list)
1149 MonoObject *ar;
1150 int count;
1152 EnterCriticalSection (cs);
1153 if (!list->array || list->first_elem == list->next_elem) {
1154 LeaveCriticalSection (cs);
1155 return NULL;
1157 ar = mono_array_get (list->array, MonoObject*, list->first_elem);
1158 list->first_elem++;
1159 count = list->next_elem - list->first_elem;
1160 /* reduce the size of the array if it's mostly empty */
1161 if (mono_array_length (list->array) > 16 && count < (mono_array_length (list->array) / 3)) {
1162 MonoArray *newa = mono_array_new (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (list->array) / 2);
1163 mono_array_memcpy_refs (newa, 0, list->array, list->first_elem, count);
1164 list->array = newa;
1165 list->first_elem = 0;
1166 list->next_elem = count;
1168 LeaveCriticalSection (cs);
1170 return ar;
1173 static void
1174 free_queue (TPQueue *list)
1176 list->array = NULL;
1177 list->first_elem = list->next_elem = 0;
1180 static void
1181 async_invoke_thread (gpointer data)
1183 MonoDomain *domain;
1184 MonoThread *thread;
1185 int workers, min;
1187 thread = mono_thread_current ();
1188 thread->threadpool_thread = TRUE;
1189 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
1191 for (;;) {
1192 MonoAsyncResult *ar;
1194 ar = (MonoAsyncResult *) data;
1195 if (ar) {
1196 /* worker threads invokes methods in different domains,
1197 * so we need to set the right domain here */
1198 domain = ((MonoObject *)ar)->vtable->domain;
1199 mono_thread_push_appdomain_ref (domain);
1200 if (mono_domain_set (domain, FALSE)) {
1201 ASyncCall *ac;
1203 mono_async_invoke (ar);
1204 ac = (ASyncCall *) ar->object_data;
1206 if (ac->msg->exc != NULL)
1207 mono_unhandled_exception (ac->msg->exc);
1209 mono_domain_set (mono_get_root_domain (), TRUE);
1211 mono_thread_pop_appdomain_ref ();
1212 InterlockedDecrement (&busy_worker_threads);
1215 data = dequeue_job (&mono_delegate_section, &async_call_queue);
1217 if (!data) {
1218 guint32 wr;
1219 int timeout = 10000;
1220 guint32 start_time = GetTickCount ();
1222 do {
1223 wr = WaitForSingleObjectEx (job_added, (guint32)timeout, TRUE);
1224 if (THREAD_WANTS_A_BREAK (thread))
1225 mono_thread_interruption_checkpoint ();
1227 timeout -= GetTickCount () - start_time;
1229 if (wr != WAIT_TIMEOUT)
1230 data = dequeue_job (&mono_delegate_section, &async_call_queue);
1232 while (!data && timeout > 0);
1235 if (!data) {
1236 workers = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
1237 min = (int) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1);
1239 while (!data && workers <= min) {
1240 WaitForSingleObjectEx (job_added, INFINITE, TRUE);
1241 if (THREAD_WANTS_A_BREAK (thread))
1242 mono_thread_interruption_checkpoint ();
1244 data = dequeue_job (&mono_delegate_section, &async_call_queue);
1245 workers = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
1246 min = (int) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1);
1250 if (!data) {
1251 InterlockedDecrement (&mono_worker_threads);
1252 return;
1255 InterlockedIncrement (&busy_worker_threads);
1258 g_assert_not_reached ();
1261 void
1262 ves_icall_System_Threading_ThreadPool_GetAvailableThreads (gint *workerThreads, gint *completionPortThreads)
1264 gint busy;
1266 MONO_ARCH_SAVE_REGS;
1268 busy = (gint) InterlockedCompareExchange (&busy_worker_threads, 0, -1);
1269 *workerThreads = mono_max_worker_threads - busy;
1270 *completionPortThreads = 0;
1273 void
1274 ves_icall_System_Threading_ThreadPool_GetMaxThreads (gint *workerThreads, gint *completionPortThreads)
1276 MONO_ARCH_SAVE_REGS;
1278 *workerThreads = mono_max_worker_threads;
1279 *completionPortThreads = 0;
1282 void
1283 ves_icall_System_Threading_ThreadPool_GetMinThreads (gint *workerThreads, gint *completionPortThreads)
1285 gint workers;
1287 MONO_ARCH_SAVE_REGS;
1289 workers = (gint) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1);
1290 *workerThreads = workers;
1291 *completionPortThreads = 0;
1294 MonoBoolean
1295 ves_icall_System_Threading_ThreadPool_SetMinThreads (gint workerThreads, gint completionPortThreads)
1297 MONO_ARCH_SAVE_REGS;
1299 if (workerThreads < 0 || workerThreads > mono_max_worker_threads)
1300 return FALSE;
1301 InterlockedExchange (&mono_min_worker_threads, workerThreads);
1302 /* FIXME: should actually start the idle threads if needed */
1303 return TRUE;