7 #include "mono/metadata/exception.h"
8 #include "mono/metadata/gc-internals.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 */
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 ();
28 continuation_alloc (void)
30 MonoContinuation
*cont
= g_new0 (MonoContinuation
, 1);
35 continuation_free (MonoContinuation
*cont
)
37 if (cont
->saved_stack
)
38 mono_gc_free_fixed (cont
->saved_stack
);
43 continuation_mark_frame (MonoContinuation
*cont
)
45 MonoJitTlsData
*jit_tls
;
47 MonoContext ctx
, new_ctx
;
52 return mono_get_exception_argument ("cont", "Already marked");
54 jit_tls
= (MonoJitTlsData
*)mono_tls_get_jit_tls ();
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
));
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");
70 if (!ji
->is_trampoline
&& strcmp (jinfo_get_method (ji
)->name
, "Mark") == 0)
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);*/
81 continuation_store (MonoContinuation
*cont
, int state
, MonoException
**e
)
83 MonoLMF
*lmf
= mono_get_lmf ();
87 *e
= mono_get_exception_argument ("cont", "Continuation not initialized");
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");
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
;
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");
119 memcpy (cont
->saved_stack
, cont
->return_sp
, num_bytes
);
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 ();
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
);
154 mono_tasklets_cleanup (void)
160 void continuations_not_supported (void)
163 mono_error_set_not_implemented (error
, "Tasklets are not implemented on this platform.");
164 mono_error_set_pending_exception (error
);
168 continuation_alloc (void)
170 continuations_not_supported ();
175 continuation_free (MonoContinuation
*cont
)
177 continuations_not_supported ();
180 static MonoException
*
181 continuation_mark_frame (MonoContinuation
*cont
)
183 continuations_not_supported ();
188 continuation_store (MonoContinuation
*cont
, int state
, MonoException
**e
)
190 continuations_not_supported ();
194 static MonoException
*
195 continuation_restore (MonoContinuation
*cont
, int state
)
197 continuations_not_supported ();
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
);