Merge pull request #1325 from madrang/CryptoToolsCtorCheck
[mono.git] / mono / mini / tasklets.c
index a287c7304289633e3e630005851349eb776c970c..9abca97a1b9fd3b11e34748588cd4188cc2d85f9 100644 (file)
@@ -7,20 +7,17 @@
 
 #if defined(MONO_SUPPORT_TASKLETS)
 
-/* keepalive_stacks could be a per-stack var to avoid locking overhead */
-static MonoGHashTable *keepalive_stacks = NULL;
-static CRITICAL_SECTION tasklets_mutex;
-#define tasklets_lock() EnterCriticalSection(&tasklets_mutex)
-#define tasklets_unlock() LeaveCriticalSection(&tasklets_mutex)
+static mono_mutex_t tasklets_mutex;
+#define tasklets_lock() mono_mutex_lock(&tasklets_mutex)
+#define tasklets_unlock() mono_mutex_unlock(&tasklets_mutex)
 
 /* LOCKING: tasklets_mutex is assumed to e taken */
 static void
 internal_init (void)
 {
-       if (keepalive_stacks)
-               return;
-       MONO_GC_REGISTER_ROOT_PINNING (keepalive_stacks);
-       keepalive_stacks = mono_g_hash_table_new (NULL, NULL);
+       if (!mono_gc_is_moving ())
+               /* Boehm requires the keepalive stacks to be kept in a hash since mono_gc_alloc_fixed () returns GC memory */
+               g_assert_not_reached ();
 }
 
 static void*
@@ -33,12 +30,8 @@ continuation_alloc (void)
 static void
 continuation_free (MonoContinuation *cont)
 {
-       if (cont->saved_stack) {
-               tasklets_lock ();
-               mono_g_hash_table_remove (keepalive_stacks, cont->saved_stack);
-               tasklets_unlock ();
+       if (cont->saved_stack)
                mono_gc_free_fixed (cont->saved_stack);
-       }
        g_free (cont);
 }
 
@@ -69,12 +62,12 @@ continuation_mark_frame (MonoContinuation *cont)
                ctx = new_ctx;
                if (endloop)
                        break;
-               if (strcmp (ji->method->name, "Mark") == 0)
+               if (!ji->is_trampoline && strcmp (jinfo_get_method (ji)->name, "Mark") == 0)
                        endloop = TRUE;
        } while (1);
 
        cont->top_sp = MONO_CONTEXT_GET_SP (&ctx);
-       /*g_print ("method: %s, sp: %p\n", ji->method->name, cont->top_sp);*/
+       /*g_print ("method: %s, sp: %p\n", jinfo_get_method (ji)->name, cont->top_sp);*/
 
        return NULL;
 }
@@ -95,7 +88,7 @@ continuation_store (MonoContinuation *cont, int state, MonoException **e)
        }
 
        cont->lmf = lmf;
-       cont->return_ip = __builtin_return_address (0);
+       cont->return_ip = __builtin_extract_return_addr (__builtin_return_address (0));
        cont->return_sp = __builtin_frame_address (0);
 
        num_bytes = (char*)cont->top_sp - (char*)cont->return_sp;
@@ -111,14 +104,11 @@ continuation_store (MonoContinuation *cont, int state, MonoException **e)
        } else {
                tasklets_lock ();
                internal_init ();
-               if (cont->saved_stack) {
-                       mono_g_hash_table_remove (keepalive_stacks, cont->saved_stack);
+               if (cont->saved_stack)
                        mono_gc_free_fixed (cont->saved_stack);
-               }
                cont->stack_used_size = num_bytes;
                cont->stack_alloc_size = num_bytes * 1.1;
-               cont->saved_stack = mono_gc_alloc_fixed (cont->stack_alloc_size, NULL);
-               mono_g_hash_table_insert (keepalive_stacks, cont->saved_stack, cont->saved_stack);
+               cont->saved_stack = mono_gc_alloc_fixed (cont->stack_alloc_size, NULL, MONO_ROOT_SOURCE_THREADING, "saved tasklet stack");
                tasklets_unlock ();
        }
        memcpy (cont->saved_stack, cont->return_sp, num_bytes);
@@ -146,7 +136,7 @@ continuation_restore (MonoContinuation *cont, int state)
 void
 mono_tasklets_init (void)
 {
-       InitializeCriticalSection (&tasklets_mutex);
+       mono_mutex_init_recursive (&tasklets_mutex);
 
        mono_add_internal_call ("Mono.Tasklets.Continuation::alloc", continuation_alloc);
        mono_add_internal_call ("Mono.Tasklets.Continuation::free", continuation_free);