[build] Skips RemoteExecuted bases tests on monodroid
[mono-project.git] / mono / mini / tasklets.c
blobcde3881e89bb83f2e5572d0e4604b8cb0e4a0f30
1 /**
2 * \file
3 */
5 #include "config.h"
6 #include "tasklets.h"
7 #include "mono/metadata/exception.h"
8 #include "mono/metadata/gc-internals.h"
9 #include "mini.h"
10 #include "mini-runtime.h"
12 #if defined(MONO_SUPPORT_TASKLETS)
14 static mono_mutex_t tasklets_mutex;
15 #define tasklets_lock() mono_os_mutex_lock(&tasklets_mutex)
16 #define tasklets_unlock() mono_os_mutex_unlock(&tasklets_mutex)
18 /* LOCKING: tasklets_mutex is assumed to e taken */
19 static void
20 internal_init (void)
22 if (!mono_gc_is_moving ())
23 /* Boehm requires the keepalive stacks to be kept in a hash since mono_gc_alloc_fixed () returns GC memory */
24 g_assert_not_reached ();
27 static void*
28 continuation_alloc (void)
30 MonoContinuation *cont = g_new0 (MonoContinuation, 1);
31 return cont;
34 static void
35 continuation_free (MonoContinuation *cont)
37 if (cont->saved_stack)
38 mono_gc_free_fixed (cont->saved_stack);
39 g_free (cont);
42 static MonoException*
43 continuation_mark_frame (MonoContinuation *cont)
45 MonoJitTlsData *jit_tls;
46 MonoLMF *lmf;
47 MonoContext ctx, new_ctx;
48 MonoJitInfo *ji, rji;
49 int endloop = FALSE;
51 if (cont->domain)
52 return mono_get_exception_argument ("cont", "Already marked");
54 jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
55 lmf = mono_get_lmf();
56 cont->domain = mono_domain_get ();
57 cont->thread_id = mono_native_thread_id_get ();
59 /* get to the frame that called Mark () */
60 memset (&rji, 0, sizeof (rji));
61 memset (&ctx, 0, sizeof (ctx));
62 do {
63 ji = mono_find_jit_info (cont->domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
64 if (!ji || ji == (gpointer)-1) {
65 return mono_get_exception_not_supported ("Invalid stack frame");
67 ctx = new_ctx;
68 if (endloop)
69 break;
70 if (!ji->is_trampoline && strcmp (jinfo_get_method (ji)->name, "Mark") == 0)
71 endloop = TRUE;
72 } while (1);
74 cont->top_sp = MONO_CONTEXT_GET_SP (&ctx);
75 /*g_print ("method: %s, sp: %p\n", jinfo_get_method (ji)->name, cont->top_sp);*/
77 return NULL;
80 static int
81 continuation_store (MonoContinuation *cont, int state, MonoException **e)
83 MonoLMF *lmf = mono_get_lmf ();
84 gsize num_bytes;
86 if (!cont->domain) {
87 *e = mono_get_exception_argument ("cont", "Continuation not initialized");
88 return 0;
90 if (cont->domain != mono_domain_get () || !mono_native_thread_id_equals (cont->thread_id, mono_native_thread_id_get ())) {
91 *e = mono_get_exception_argument ("cont", "Continuation from another thread or domain");
92 return 0;
95 cont->lmf = lmf;
96 cont->return_ip = __builtin_extract_return_addr (__builtin_return_address (0));
97 cont->return_sp = __builtin_frame_address (0);
99 num_bytes = (char*)cont->top_sp - (char*)cont->return_sp;
101 /*g_print ("store: %d bytes, sp: %p, ip: %p, lmf: %p\n", num_bytes, cont->return_sp, cont->return_ip, lmf);*/
103 if (cont->saved_stack && num_bytes <= cont->stack_alloc_size) {
104 /* clear to avoid GC retention */
105 if (num_bytes < cont->stack_used_size) {
106 memset ((char*)cont->saved_stack + num_bytes, 0, cont->stack_used_size - num_bytes);
108 cont->stack_used_size = num_bytes;
109 } else {
110 tasklets_lock ();
111 internal_init ();
112 if (cont->saved_stack)
113 mono_gc_free_fixed (cont->saved_stack);
114 cont->stack_used_size = num_bytes;
115 cont->stack_alloc_size = num_bytes * 1.1;
116 cont->saved_stack = mono_gc_alloc_fixed (cont->stack_alloc_size, NULL, MONO_ROOT_SOURCE_THREADING, NULL, "Tasklet Saved Stack");
117 tasklets_unlock ();
119 memcpy (cont->saved_stack, cont->return_sp, num_bytes);
121 return state;
124 static MonoException*
125 continuation_restore (MonoContinuation *cont, int state)
127 MonoLMF **lmf_addr = mono_get_lmf_addr ();
128 MonoContinuationRestore restore_state = mono_tasklets_arch_restore ();
130 if (!cont->domain || !cont->return_sp)
131 return mono_get_exception_argument ("cont", "Continuation not initialized");
132 if (cont->domain != mono_domain_get () || !mono_native_thread_id_equals (cont->thread_id, mono_native_thread_id_get ()))
133 return mono_get_exception_argument ("cont", "Continuation from another thread or domain");
135 /*g_print ("restore: %p, state: %d\n", cont, state);*/
136 *lmf_addr = cont->lmf;
137 restore_state (cont, state, lmf_addr);
138 g_assert_not_reached ();
141 void
142 mono_tasklets_init (void)
144 mono_os_mutex_init_recursive (&tasklets_mutex);
146 mono_add_internal_call ("Mono.Tasklets.Continuation::alloc", continuation_alloc);
147 mono_add_internal_call ("Mono.Tasklets.Continuation::free", continuation_free);
148 mono_add_internal_call ("Mono.Tasklets.Continuation::mark", continuation_mark_frame);
149 mono_add_internal_call ("Mono.Tasklets.Continuation::store", continuation_store);
150 mono_add_internal_call ("Mono.Tasklets.Continuation::restore", continuation_restore);
153 void
154 mono_tasklets_cleanup (void)
157 #else
159 static
160 void continuations_not_supported (void)
162 ERROR_DECL (error);
163 mono_error_set_not_implemented (error, "Tasklets are not implemented on this platform.");
164 mono_error_set_pending_exception (error);
167 static void*
168 continuation_alloc (void)
170 continuations_not_supported ();
171 return NULL;
174 static void
175 continuation_free (MonoContinuation *cont)
177 continuations_not_supported ();
180 static MonoException*
181 continuation_mark_frame (MonoContinuation *cont)
183 continuations_not_supported ();
184 return NULL;
187 static int
188 continuation_store (MonoContinuation *cont, int state, MonoException **e)
190 continuations_not_supported ();
191 return 0;
194 static MonoException*
195 continuation_restore (MonoContinuation *cont, int state)
197 continuations_not_supported ();
198 return NULL;
201 void
202 mono_tasklets_init(void)
204 mono_add_internal_call ("Mono.Tasklets.Continuation::alloc", continuation_alloc);
205 mono_add_internal_call ("Mono.Tasklets.Continuation::free", continuation_free);
206 mono_add_internal_call ("Mono.Tasklets.Continuation::mark", continuation_mark_frame);
207 mono_add_internal_call ("Mono.Tasklets.Continuation::store", continuation_store);
208 mono_add_internal_call ("Mono.Tasklets.Continuation::restore", continuation_restore);
211 #endif