static HANDLE job_added;
static HANDLE io_job_added;
+/* Keep in sync with the System.MonoAsyncCall class which provides GC tracking */
typedef struct {
+ MonoObject object;
MonoMethodMessage *msg;
- HANDLE wait_event;
MonoMethod *cb_method;
MonoDelegate *cb_target;
MonoObject *state;
MonoObject *res;
MonoArray *out_args;
+ /* This is a HANDLE, we use guint64 so the managed object layout remains constant */
+ guint64 wait_event;
} ASyncCall;
static void async_invoke_thread (gpointer data);
static GList *async_call_queue = NULL;
static GList *async_io_queue = NULL;
+static MonoClass *async_call_klass;
static MonoClass *socket_async_call_klass;
+static MonoClass *process_async_call_klass;
#define INIT_POLLFD(a, b, c) {(a)->fd = b; (a)->events = c; (a)->revents = 0;}
enum {
AIO_OP_SENDTO,
AIO_OP_RECV_JUST_CALLBACK,
AIO_OP_SEND_JUST_CALLBACK,
+ AIO_OP_READPIPE,
AIO_OP_LAST
};
case AIO_OP_RECEIVE:
case AIO_OP_RECV_JUST_CALLBACK:
case AIO_OP_RECEIVEFROM:
+ case AIO_OP_READPIPE:
return MONO_POLLIN;
case AIO_OP_SEND:
case AIO_OP_SEND_JUST_CALLBACK:
if (state) {
InterlockedDecrement (&pending_io_items);
ar = state->ares;
- /* worker threads invokes methods in different domains,
- * so we need to set the right domain here */
switch (state->operation) {
case AIO_OP_RECEIVE:
state->total = ICALL_RECV (state);
break;
}
+ /* worker threads invokes methods in different domains,
+ * so we need to set the right domain here */
domain = ((MonoObject *)ar)->vtable->domain;
+ mono_thread_push_appdomain_ref (domain);
if (mono_domain_set (domain, FALSE)) {
ASyncCall *ac;
- mono_thread_push_appdomain_ref (domain);
mono_async_invoke (ar);
- ac = (ASyncCall *) ar->data;
+ ac = (ASyncCall *) ar->object_data;
+ /*
if (ac->msg->exc != NULL)
mono_unhandled_exception (ac->msg->exc);
- mono_thread_pop_appdomain_ref ();
+ */
+ mono_domain_set (mono_get_root_domain (), TRUE);
}
+ mono_thread_pop_appdomain_ref ();
InterlockedDecrement (&busy_io_worker_threads);
}
InterlockedIncrement (&busy_io_worker_threads);
InterlockedIncrement (&io_worker_threads);
domain = ((ares) ? ((MonoObject *) ares)->vtable->domain : mono_domain_get ());
- mono_thread_create (domain, async_invoke_io_thread, ares);
+ mono_thread_create (mono_get_root_domain (), async_invoke_io_thread, ares);
} else {
append_job (&io_queue_lock, &async_io_queue, ares);
ReleaseSemaphore (io_job_added, 1, NULL);
if (pfd->fd == -1)
continue;
- ret = mono_poll (pfds, 1, 0);
+ ret = mono_poll (pfd, 1, 0);
if (ret == -1 && errno == EBADF) {
pfd->revents |= MONO_POLLNVAL;
count++;
EnterCriticalSection (&data->io_lock);
if (data->inited == 0) {
g_free (pfds);
+ LeaveCriticalSection (&data->io_lock);
return; /* cleanup called */
}
data->sock_to_state = g_hash_table_new (g_direct_hash, g_direct_equal);
- if (data->epoll_disabled)
+ if (data->epoll_disabled) {
data->new_sem = CreateSemaphore (NULL, 1, 1, NULL);
+ g_assert (data->new_sem != NULL);
+ }
io_job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
+ g_assert (io_job_added != NULL);
InitializeCriticalSection (&io_queue_lock);
if (data->epoll_disabled) {
mono_thread_create (mono_get_root_domain (), socket_io_poll_main, data);
GSList *list;
SocketIOData *data = &socket_io_data;
+#if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD6) || defined(PLATFORM_WIN32)
+ /* select() for connect() does not work well on the Mac. Bug #75436. */
+ /* Bug #77637 for the BSD 6 case */
+ /* Bug #78888 for the Windows case */
+ if (state->operation == AIO_OP_CONNECT && state->blocking == TRUE) {
+ start_io_thread_or_queue (state);
+ return;
+ }
+#endif
WaitForSingleObject (data->new_sem, INFINITE);
if (data->newpfd == NULL)
data->newpfd = g_new0 (mono_pollfd, 1);
EnterCriticalSection (&data->io_lock);
+ /* FIXME: 64 bit issue: handle can be a pointer on windows? */
list = g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (state->handle));
+ /* FIXME: GC issue: state is an object stored in a GList */
if (list == NULL) {
list = g_slist_alloc ();
list->data = state;
fd = GPOINTER_TO_INT (state->handle);
EnterCriticalSection (&data->io_lock);
list = g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
+ /* FIXME: GC issue: state is an object stored in a GList */
if (list == NULL) {
list = g_slist_alloc ();
list->data = state;
socket_io_add (MonoAsyncResult *ares, MonoSocketAsyncResult *state)
{
socket_io_init (&socket_io_data);
- state->ares = ares;
+ MONO_OBJECT_SETREF (state, ares, ares);
#ifdef HAVE_EPOLL
if (socket_io_data.epoll_disabled == FALSE) {
if (socket_io_add_epoll (state))
if (socket_async_call_klass == NULL) {
klass = target->vtable->klass;
- /* Check if it's SocketAsyncCall in System
+ /* Check if it's SocketAsyncCall in System.Net.Sockets
* FIXME: check the assembly is signed correctly for extra care
*/
if (klass->name [0] == 'S' && strcmp (klass->name, "SocketAsyncCall") == 0
socket_async_call_klass = klass;
}
+ if (process_async_call_klass == NULL) {
+ klass = target->vtable->klass;
+ /* Check if it's AsyncReadHandler in System.Diagnostics.Process
+ * FIXME: check the assembly is signed correctly for extra care
+ */
+ if (klass->name [0] == 'A' && strcmp (klass->name, "AsyncReadHandler") == 0
+ && strcmp (mono_image_get_name (klass->image), "System") == 0
+ && klass->nested_in && strcmp (klass->nested_in->name, "Process") == 0)
+ process_async_call_klass = klass;
+ }
/* return both when socket_async_call_klass has not been seen yet and when
* the object is not an instance of the class.
*/
- if (target->vtable->klass != socket_async_call_klass)
+ if (target->vtable->klass != socket_async_call_klass && target->vtable->klass != process_async_call_klass)
return FALSE;
op = sock_res->operation;
static void
mono_async_invoke (MonoAsyncResult *ares)
{
- ASyncCall *ac = (ASyncCall *)ares->data;
+ ASyncCall *ac = (ASyncCall *)ares->object_data;
+ MonoThread *thread = NULL;
+ MonoObject *res, *exc = NULL;
+ MonoArray *out_args = NULL;
+
+ if (ares->execution_context) {
+ /* use captured ExecutionContext (if available) */
+ thread = mono_thread_current ();
+ MONO_OBJECT_SETREF (ares, original_context, thread->execution_context);
+ MONO_OBJECT_SETREF (thread, execution_context, ares->execution_context);
+ } else {
+ ares->original_context = NULL;
+ }
ac->msg->exc = NULL;
- ac->res = mono_message_invoke (ares->async_delegate, ac->msg,
- &ac->msg->exc, &ac->out_args);
+ res = mono_message_invoke (ares->async_delegate, ac->msg, &exc, &out_args);
+ MONO_OBJECT_SETREF (ac, res, res);
+ MONO_OBJECT_SETREF (ac, msg->exc, exc);
+ MONO_OBJECT_SETREF (ac, out_args, out_args);
ares->completed = 1;
MonoObject *exc = NULL;
void *pa = &ares;
mono_runtime_invoke (ac->cb_method, ac->cb_target, pa, &exc);
- if (!ac->msg->exc)
- ac->msg->exc = exc;
+ /* 'exc' will be the previous ac->msg->exc if not NULL and not
+ * catched. If catched, this will be set to NULL and the
+ * exception will not be printed. */
+ MONO_OBJECT_SETREF (ac->msg, exc, exc);
+ }
+
+ /* restore original thread execution context if flow isn't suppressed, i.e. non null */
+ if (ares->original_context) {
+ MONO_OBJECT_SETREF (thread, execution_context, ares->original_context);
+ ares->original_context = NULL;
}
/* notify listeners */
mono_monitor_enter ((MonoObject *) ares);
if (ares->handle != NULL) {
- ac->wait_event = ((MonoWaitHandle *) ares->handle)->handle;
- SetEvent (ac->wait_event);
+ ac->wait_event = (gsize)((MonoWaitHandle *) ares->handle)->handle;
+ SetEvent ((gpointer)(gsize)ac->wait_event);
}
mono_monitor_exit ((MonoObject *) ares);
MONO_GC_REGISTER_ROOT (ares_htable);
InitializeCriticalSection (&socket_io_data.io_lock);
InitializeCriticalSection (&ares_lock);
- ares_htable = mono_g_hash_table_new (NULL, NULL);
+ ares_htable = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC);
job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
+ g_assert (job_added != NULL);
GetSystemInfo (&info);
if (g_getenv ("MONO_THREADS_PER_CPU") != NULL) {
threads_per_cpu = atoi (g_getenv ("MONO_THREADS_PER_CPU"));
}
mono_max_worker_threads = 20 + threads_per_cpu * info.dwNumberOfProcessors;
+
+ async_call_klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
+ g_assert (async_call_klass);
}
MonoAsyncResult *
MonoAsyncResult *ares;
ASyncCall *ac;
-#ifdef HAVE_BOEHM_GC
- ac = GC_MALLOC (sizeof (ASyncCall));
-#else
- /* We'll leak the event if creaated... */
- ac = g_new0 (ASyncCall, 1);
-#endif
- ac->wait_event = NULL;
- ac->msg = msg;
- ac->state = state;
+ ac = (ASyncCall*)mono_object_new (mono_domain_get (), async_call_klass);
+ MONO_OBJECT_SETREF (ac, msg, msg);
+ MONO_OBJECT_SETREF (ac, state, state);
if (async_callback) {
ac->cb_method = mono_get_delegate_invoke (((MonoObject *)async_callback)->vtable->klass);
- ac->cb_target = async_callback;
+ MONO_OBJECT_SETREF (ac, cb_target, async_callback);
}
- ares = mono_async_result_new (domain, NULL, ac->state, ac);
- ares->async_delegate = target;
+ ares = mono_async_result_new (domain, NULL, ac->state, NULL, (MonoObject*)ac);
+ MONO_OBJECT_SETREF (ares, async_delegate, target);
EnterCriticalSection (&ares_lock);
mono_g_hash_table_insert (ares_htable, ares, ares);
start_thread_or_queue (MonoAsyncResult *ares)
{
int busy, worker;
- MonoDomain *domain;
busy = (int) InterlockedCompareExchange (&busy_worker_threads, 0, -1);
worker = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
worker < mono_max_worker_threads) {
InterlockedIncrement (&mono_worker_threads);
InterlockedIncrement (&busy_worker_threads);
- domain = ((MonoObject *) ares)->vtable->domain;
- mono_thread_create (domain, async_invoke_thread, ares);
+ mono_thread_create (mono_get_root_domain (), async_invoke_thread, ares);
} else {
append_job (&mono_delegate_section, &async_call_queue, ares);
ReleaseSemaphore (job_added, 1, NULL);
}
ares->endinvoke_called = 1;
- ac = (ASyncCall *)ares->data;
+ ac = (ASyncCall *)ares->object_data;
g_assert (ac != NULL);
/* wait until we are really finished */
if (!ares->completed) {
if (ares->handle == NULL) {
- ac->wait_event = CreateEvent (NULL, TRUE, FALSE, NULL);
- ares->handle = (MonoObject *) mono_wait_handle_new (mono_object_domain (ares), ac->wait_event);
+ ac->wait_event = (gsize)CreateEvent (NULL, TRUE, FALSE, NULL);
+ g_assert(ac->wait_event != 0);
+ MONO_OBJECT_SETREF (ares, handle, (MonoObject *) mono_wait_handle_new (mono_object_domain (ares), (gpointer)(gsize)ac->wait_event));
}
mono_monitor_exit ((MonoObject *) ares);
- WaitForSingleObjectEx (ac->wait_event, INFINITE, TRUE);
+ WaitForSingleObjectEx ((gpointer)(gsize)ac->wait_event, INFINITE, TRUE);
} else {
mono_monitor_exit ((MonoObject *) ares);
}
- *exc = ac->msg->exc;
+ *exc = ac->msg->exc; /* FIXME: GC add write barrier */
*out_args = ac->out_args;
return ac->res;
socket_io_cleanup (&socket_io_data);
}
+/* FIXME: GC: adds managed objects to the list... */
static void
append_job (CRITICAL_SECTION *cs, GList **plist, gpointer ar)
{
/* worker threads invokes methods in different domains,
* so we need to set the right domain here */
domain = ((MonoObject *)ar)->vtable->domain;
+ mono_thread_push_appdomain_ref (domain);
if (mono_domain_set (domain, FALSE)) {
ASyncCall *ac;
- mono_thread_push_appdomain_ref (domain);
mono_async_invoke (ar);
- ac = (ASyncCall *) ar->data;
+ ac = (ASyncCall *) ar->object_data;
+ /*
if (ac->msg->exc != NULL)
mono_unhandled_exception (ac->msg->exc);
- mono_thread_pop_appdomain_ref ();
+ */
+ mono_domain_set (mono_get_root_domain (), TRUE);
}
+ mono_thread_pop_appdomain_ref ();
InterlockedDecrement (&busy_worker_threads);
}
data = dequeue_job (&mono_delegate_section, &async_call_queue);
-
+
if (!data) {
guint32 wr;
int timeout = 10000;