4 #include "mono/metadata/exception.h"
5 #include "mono/metadata/gc-internal.h"
8 #if defined(MONO_SUPPORT_TASKLETS)
10 /* keepalive_stacks could be a per-stack var to avoid locking overhead */
11 static MonoGHashTable *keepalive_stacks = NULL;
12 static CRITICAL_SECTION tasklets_mutex;
13 #define tasklets_lock() EnterCriticalSection(&tasklets_mutex)
14 #define tasklets_unlock() LeaveCriticalSection(&tasklets_mutex)
16 /* LOCKING: tasklets_mutex is assumed to e taken */
22 MONO_GC_REGISTER_ROOT (keepalive_stacks);
23 keepalive_stacks = mono_g_hash_table_new (NULL, NULL);
27 continuation_alloc (void)
29 MonoContinuation *cont = g_new0 (MonoContinuation, 1);
34 continuation_free (MonoContinuation *cont)
36 if (cont->saved_stack) {
38 mono_g_hash_table_remove (keepalive_stacks, cont->saved_stack);
40 mono_gc_free_fixed (cont->saved_stack);
46 continuation_mark_frame (MonoContinuation *cont)
48 MonoJitTlsData *jit_tls;
50 MonoContext ctx, new_ctx;
55 return mono_get_exception_argument ("cont", "Already marked");
57 jit_tls = TlsGetValue (mono_jit_tls_id);
59 cont->domain = mono_domain_get ();
60 cont->thread_id = GetCurrentThreadId ();
62 /* get to the frame that called Mark () */
63 memset (&rji, 0, sizeof (rji));
65 ji = mono_find_jit_info (cont->domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
66 if (!ji || ji == (gpointer)-1) {
67 return mono_get_exception_not_supported ("Invalid stack frame");
72 if (strcmp (ji->method->name, "Mark") == 0)
76 cont->top_sp = MONO_CONTEXT_GET_SP (&ctx);
77 /*g_print ("method: %s, sp: %p\n", ji->method->name, cont->top_sp);*/
83 continuation_store (MonoContinuation *cont, int state, MonoException **e)
85 MonoLMF *lmf = mono_get_lmf ();
89 *e = mono_get_exception_argument ("cont", "Continuation not initialized");
90 if (cont->domain != mono_domain_get () || cont->thread_id != GetCurrentThreadId ())
91 *e = mono_get_exception_argument ("cont", "Continuation from another thread or domain");
94 cont->return_ip = __builtin_return_address (0);
95 cont->return_sp = __builtin_frame_address (0);
97 num_bytes = (char*)cont->top_sp - (char*)cont->return_sp;
99 /*g_print ("store: %d bytes, sp: %p, ip: %p, lmf: %p\n", num_bytes, cont->return_sp, cont->return_ip, lmf);*/
101 if (cont->saved_stack && num_bytes <= cont->stack_alloc_size) {
102 /* clear to avoid GC retention */
103 if (num_bytes < cont->stack_used_size)
104 memset ((char*)cont->saved_stack + num_bytes, 0, cont->stack_used_size - num_bytes);
108 if (cont->saved_stack) {
109 mono_g_hash_table_remove (keepalive_stacks, cont->saved_stack);
110 mono_gc_free_fixed (cont->saved_stack);
112 cont->stack_used_size = num_bytes;
113 cont->stack_alloc_size = num_bytes * 1.1;
114 cont->saved_stack = mono_gc_alloc_fixed (cont->stack_alloc_size, NULL);
115 mono_g_hash_table_insert (keepalive_stacks, cont->saved_stack, cont->saved_stack);
118 memcpy (cont->saved_stack, cont->return_sp, num_bytes);
123 static MonoException*
124 continuation_restore (MonoContinuation *cont, int state)
126 MonoLMF **lmf_addr = mono_get_lmf_addr ();
127 MonoContinuationRestore restore_state = mono_tasklets_arch_restore ();
129 if (!cont->domain || !cont->return_sp)
130 return mono_get_exception_argument ("cont", "Continuation not initialized");
131 if (cont->domain != mono_domain_get () || cont->thread_id != GetCurrentThreadId ())
132 return mono_get_exception_argument ("cont", "Continuation from another thread or domain");
134 /*g_print ("restore: %p, state: %d\n", cont, state);*/
135 *lmf_addr = cont->lmf;
136 restore_state (cont, state, lmf_addr);
137 g_assert_not_reached ();
141 mono_tasklets_init (void)
143 InitializeCriticalSection (&tasklets_mutex);
145 mono_add_internal_call ("Mono.Tasklets.Continuation::alloc", continuation_alloc);
146 mono_add_internal_call ("Mono.Tasklets.Continuation::free", continuation_free);
147 mono_add_internal_call ("Mono.Tasklets.Continuation::mark", continuation_mark_frame);
148 mono_add_internal_call ("Mono.Tasklets.Continuation::store", continuation_store);
149 mono_add_internal_call ("Mono.Tasklets.Continuation::restore", continuation_restore);
153 mono_tasklets_cleanup (void)