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_PINNING (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 = mono_native_tls_get_value (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 (jinfo_get_method (ji)->name, "Mark") == 0)
76 cont->top_sp = MONO_CONTEXT_GET_SP (&ctx);
77 /*g_print ("method: %s, sp: %p\n", jinfo_get_method (ji)->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");
92 if (cont->domain != mono_domain_get () || cont->thread_id != GetCurrentThreadId ()) {
93 *e = mono_get_exception_argument ("cont", "Continuation from another thread or domain");
98 cont->return_ip = __builtin_extract_return_addr (__builtin_return_address (0));
99 cont->return_sp = __builtin_frame_address (0);
101 num_bytes = (char*)cont->top_sp - (char*)cont->return_sp;
103 /*g_print ("store: %d bytes, sp: %p, ip: %p, lmf: %p\n", num_bytes, cont->return_sp, cont->return_ip, lmf);*/
105 if (cont->saved_stack && num_bytes <= cont->stack_alloc_size) {
106 /* clear to avoid GC retention */
107 if (num_bytes < cont->stack_used_size) {
108 memset ((char*)cont->saved_stack + num_bytes, 0, cont->stack_used_size - num_bytes);
110 cont->stack_used_size = num_bytes;
114 if (cont->saved_stack) {
115 mono_g_hash_table_remove (keepalive_stacks, cont->saved_stack);
116 mono_gc_free_fixed (cont->saved_stack);
118 cont->stack_used_size = num_bytes;
119 cont->stack_alloc_size = num_bytes * 1.1;
120 cont->saved_stack = mono_gc_alloc_fixed (cont->stack_alloc_size, NULL);
121 mono_g_hash_table_insert (keepalive_stacks, cont->saved_stack, cont->saved_stack);
124 memcpy (cont->saved_stack, cont->return_sp, num_bytes);
129 static MonoException*
130 continuation_restore (MonoContinuation *cont, int state)
132 MonoLMF **lmf_addr = mono_get_lmf_addr ();
133 MonoContinuationRestore restore_state = mono_tasklets_arch_restore ();
135 if (!cont->domain || !cont->return_sp)
136 return mono_get_exception_argument ("cont", "Continuation not initialized");
137 if (cont->domain != mono_domain_get () || cont->thread_id != GetCurrentThreadId ())
138 return mono_get_exception_argument ("cont", "Continuation from another thread or domain");
140 /*g_print ("restore: %p, state: %d\n", cont, state);*/
141 *lmf_addr = cont->lmf;
142 restore_state (cont, state, lmf_addr);
143 g_assert_not_reached ();
147 mono_tasklets_init (void)
149 InitializeCriticalSection (&tasklets_mutex);
151 mono_add_internal_call ("Mono.Tasklets.Continuation::alloc", continuation_alloc);
152 mono_add_internal_call ("Mono.Tasklets.Continuation::free", continuation_free);
153 mono_add_internal_call ("Mono.Tasklets.Continuation::mark", continuation_mark_frame);
154 mono_add_internal_call ("Mono.Tasklets.Continuation::store", continuation_store);
155 mono_add_internal_call ("Mono.Tasklets.Continuation::restore", continuation_restore);
159 mono_tasklets_cleanup (void)