Merge pull request #498 from Unroll-Me/master
[mono.git] / mono / metadata / threads.c
index 537de66bca1d8d5b783887c3aa824e69e9bb8a11..2e05a9357930978ab2af75e1f0eee831ae411f46 100644 (file)
@@ -763,6 +763,9 @@ MonoInternalThread* mono_thread_create_internal (MonoDomain *domain, gpointer fu
        if (handle_store (thread))
                ResumeThread (thread_handle);
 
+       /* Check that the managed and unmanaged layout of MonoInternalThread matches */
+       g_assert (((char*)&internal->unused2 - (char*)internal) == mono_defaults.internal_thread_class->fields [mono_defaults.internal_thread_class->field.count - 1].offset);
+
        return internal;
 }
 
@@ -1359,6 +1362,44 @@ gboolean ves_icall_System_Threading_Thread_Join_internal(MonoInternalThread *thi
        return(FALSE);
 }
 
+static gint32
+mono_wait_uninterrupted (MonoInternalThread *thread, gboolean multiple, guint32 numhandles, gpointer *handles, gboolean waitall, gint32 ms, gboolean alertable)
+{
+       MonoException *exc;
+       guint32 ret;
+       gint64 start;
+       gint32 diff_ms;
+       gint32 wait = ms;
+
+       start = (ms == -1) ? 0 : mono_100ns_ticks ();
+       do {
+               if (multiple)
+                       ret = WaitForMultipleObjectsEx (numhandles, handles, waitall, wait, alertable);
+               else
+                       ret = WaitForSingleObjectEx (handles [0], ms, alertable);
+
+               if (ret != WAIT_IO_COMPLETION)
+                       break;
+
+               exc = mono_thread_execute_interruption (thread);
+               if (exc)
+                       mono_raise_exception (exc);
+
+               if (ms == -1)
+                       continue;
+
+               /* Re-calculate ms according to the time passed */
+               diff_ms = (mono_100ns_ticks () - start) / 10000;
+               if (diff_ms >= ms) {
+                       ret = WAIT_TIMEOUT;
+                       break;
+               }
+               wait = ms - diff_ms;
+       } while (TRUE);
+       
+       return ret;
+}
+
 /* FIXME: exitContext isnt documented */
 gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms, gboolean exitContext)
 {
@@ -1386,7 +1427,7 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_
 
        mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
        
-       ret=WaitForMultipleObjectsEx(numhandles, handles, TRUE, ms, TRUE);
+       ret = mono_wait_uninterrupted (thread, TRUE, numhandles, handles, TRUE, ms, TRUE);
 
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
 
@@ -1395,12 +1436,7 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_
        if(ret==WAIT_FAILED) {
                THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
                return(FALSE);
-       } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
-               /* Do we want to try again if we get
-                * WAIT_IO_COMPLETION? The documentation for
-                * WaitHandle doesn't give any clues.  (We'd have to
-                * fiddle with the timeout if we retry.)
-                */
+       } else if(ret==WAIT_TIMEOUT) {
                THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
                return(FALSE);
        }
@@ -1417,7 +1453,6 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_ha
        guint32 i;
        MonoObject *waitHandle;
        MonoInternalThread *thread = mono_thread_internal_current ();
-       guint32 start;
 
        /* Do this WaitSleepJoin check before creating objects */
        mono_thread_current_check_pending_interrupt ();
@@ -1437,20 +1472,7 @@ gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_ha
 
        mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
 
-       start = (ms == -1) ? 0 : mono_msec_ticks ();
-       do {
-               ret = WaitForMultipleObjectsEx (numhandles, handles, FALSE, ms, TRUE);
-               if (ret != WAIT_IO_COMPLETION)
-                       break;
-               if (ms != -1) {
-                       guint32 diff;
-
-                       diff = mono_msec_ticks () - start;
-                       ms -= diff;
-                       if (ms <= 0)
-                               break;
-               }
-       } while (ms == -1 || ms > 0);
+       ret = mono_wait_uninterrupted (thread, TRUE, numhandles, handles, FALSE, ms, TRUE);
 
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
 
@@ -1486,19 +1508,14 @@ gboolean ves_icall_System_Threading_WaitHandle_WaitOne_internal(MonoObject *this
 
        mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
        
-       ret=WaitForSingleObjectEx (handle, ms, TRUE);
+       ret = mono_wait_uninterrupted (thread, FALSE, 1, &handle, FALSE, ms, TRUE);
        
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
        
        if(ret==WAIT_FAILED) {
                THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait failed", __func__, GetCurrentThreadId ()));
                return(FALSE);
-       } else if(ret==WAIT_TIMEOUT || ret == WAIT_IO_COMPLETION) {
-               /* Do we want to try again if we get
-                * WAIT_IO_COMPLETION? The documentation for
-                * WaitHandle doesn't give any clues.  (We'd have to
-                * fiddle with the timeout if we retry.)
-                */
+       } else if(ret==WAIT_TIMEOUT) {
                THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Wait timed out", __func__, GetCurrentThreadId ()));
                return(FALSE);
        }
@@ -3688,7 +3705,7 @@ search_tls_slot_in_freelist (StaticDataInfo *static_data, guint32 size, guint32
 }
 
 static void
-update_tls_reference_bitmap (guint32 offset, uintptr_t *bitmap, int max_set)
+update_tls_reference_bitmap (guint32 offset, uintptr_t *bitmap, int numbits)
 {
        int i;
        int idx = (offset >> 24) - 1;
@@ -3699,7 +3716,7 @@ update_tls_reference_bitmap (guint32 offset, uintptr_t *bitmap, int max_set)
        offset &= 0xffffff;
        offset /= sizeof (gpointer);
        /* offset is now the bitmap offset */
-       for (i = 0; i <= max_set; ++i) {
+       for (i = 0; i < numbits; ++i) {
                if (bitmap [i / sizeof (uintptr_t)] & (1L << (i & (sizeof (uintptr_t) * 8 -1))))
                        rb [(offset + i) / (sizeof (uintptr_t) * 8)] |= (1L << ((offset + i) & (sizeof (uintptr_t) * 8 -1)));
        }
@@ -3729,7 +3746,7 @@ clear_reference_bitmap (guint32 offset, guint32 size)
  */
 
 guint32
-mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align, uintptr_t *bitmap, int max_set)
+mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align, uintptr_t *bitmap, int numbits)
 {
        guint32 offset;
        if (static_type == SPECIAL_STATIC_THREAD) {
@@ -3743,7 +3760,7 @@ mono_alloc_special_static_data (guint32 static_type, guint32 size, guint32 align
                } else {
                        offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
                }
-               update_tls_reference_bitmap (offset, bitmap, max_set);
+               update_tls_reference_bitmap (offset, bitmap, numbits);
                /* This can be called during startup */
                if (threads != NULL)
                        mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
@@ -3886,7 +3903,7 @@ mono_thread_alloc_tls (MonoReflectionType *type)
        /* TlsDatum is a struct, so we subtract the object header size offset */
        bitmap = mono_class_compute_bitmap (klass, default_bitmap, sizeof (default_bitmap) * 8, - (int)(sizeof (MonoObject) / sizeof (gpointer)), &max_set, FALSE);
        size = mono_type_size (type->type, &align);
-       tls_offset = mono_alloc_special_static_data (SPECIAL_STATIC_THREAD, size, align, (uintptr_t*)bitmap, max_set);
+       tls_offset = mono_alloc_special_static_data (SPECIAL_STATIC_THREAD, size, align, (uintptr_t*)bitmap, max_set + 1);
        if (bitmap != default_bitmap)
                g_free (bitmap);
        tlsrec = g_new0 (MonoTlsDataRecord, 1);