Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / tasklets.c
1 /**
2  * \file
3  */
4
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
11 #if defined(MONO_SUPPORT_TASKLETS)
12
13 static mono_mutex_t tasklets_mutex;
14 #define tasklets_lock() mono_os_mutex_lock(&tasklets_mutex)
15 #define tasklets_unlock() mono_os_mutex_unlock(&tasklets_mutex)
16
17 /* LOCKING: tasklets_mutex is assumed to e taken */
18 static void
19 internal_init (void)
20 {
21         if (!mono_gc_is_moving ())
22                 /* Boehm requires the keepalive stacks to be kept in a hash since mono_gc_alloc_fixed () returns GC memory */
23                 g_assert_not_reached ();
24 }
25
26 static void*
27 continuation_alloc (void)
28 {
29         MonoContinuation *cont = g_new0 (MonoContinuation, 1);
30         return cont;
31 }
32
33 static void
34 continuation_free (MonoContinuation *cont)
35 {
36         if (cont->saved_stack)
37                 mono_gc_free_fixed (cont->saved_stack);
38         g_free (cont);
39 }
40
41 static MonoException*
42 continuation_mark_frame (MonoContinuation *cont)
43 {
44         MonoJitTlsData *jit_tls;
45         MonoLMF *lmf;
46         MonoContext ctx, new_ctx;
47         MonoJitInfo *ji, rji;
48         int endloop = FALSE;
49
50         if (cont->domain)
51                 return mono_get_exception_argument ("cont", "Already marked");
52
53         jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
54         lmf = mono_get_lmf();
55         cont->domain = mono_domain_get ();
56         cont->thread_id = mono_native_thread_id_get ();
57
58         /* get to the frame that called Mark () */
59         memset (&rji, 0, sizeof (rji));
60         memset (&ctx, 0, sizeof (ctx));
61         do {
62                 ji = mono_find_jit_info (cont->domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
63                 if (!ji || ji == (gpointer)-1) {
64                         return mono_get_exception_not_supported ("Invalid stack frame");
65                 }
66                 ctx = new_ctx;
67                 if (endloop)
68                         break;
69                 if (!ji->is_trampoline && strcmp (jinfo_get_method (ji)->name, "Mark") == 0)
70                         endloop = TRUE;
71         } while (1);
72
73         cont->top_sp = MONO_CONTEXT_GET_SP (&ctx);
74         /*g_print ("method: %s, sp: %p\n", jinfo_get_method (ji)->name, cont->top_sp);*/
75
76         return NULL;
77 }
78
79 static int
80 continuation_store (MonoContinuation *cont, int state, MonoException **e)
81 {
82         MonoLMF *lmf = mono_get_lmf ();
83         gsize num_bytes;
84
85         if (!cont->domain) {
86                 *e =  mono_get_exception_argument ("cont", "Continuation not initialized");
87                 return 0;
88         }
89         if (cont->domain != mono_domain_get () || !mono_native_thread_id_equals (cont->thread_id, mono_native_thread_id_get ())) {
90                 *e = mono_get_exception_argument ("cont", "Continuation from another thread or domain");
91                 return 0;
92         }
93
94         cont->lmf = lmf;
95         cont->return_ip = __builtin_extract_return_addr (__builtin_return_address (0));
96         cont->return_sp = __builtin_frame_address (0);
97
98         num_bytes = (char*)cont->top_sp - (char*)cont->return_sp;
99
100         /*g_print ("store: %d bytes, sp: %p, ip: %p, lmf: %p\n", num_bytes, cont->return_sp, cont->return_ip, lmf);*/
101
102         if (cont->saved_stack && num_bytes <= cont->stack_alloc_size) {
103                 /* clear to avoid GC retention */
104                 if (num_bytes < cont->stack_used_size) {
105                         memset ((char*)cont->saved_stack + num_bytes, 0, cont->stack_used_size - num_bytes);
106                 }
107                 cont->stack_used_size = num_bytes;
108         } else {
109                 tasklets_lock ();
110                 internal_init ();
111                 if (cont->saved_stack)
112                         mono_gc_free_fixed (cont->saved_stack);
113                 cont->stack_used_size = num_bytes;
114                 cont->stack_alloc_size = num_bytes * 1.1;
115                 cont->saved_stack = mono_gc_alloc_fixed (cont->stack_alloc_size, NULL, MONO_ROOT_SOURCE_THREADING, "saved tasklet stack");
116                 tasklets_unlock ();
117         }
118         memcpy (cont->saved_stack, cont->return_sp, num_bytes);
119
120         return state;
121 }
122
123 static MonoException*
124 continuation_restore (MonoContinuation *cont, int state)
125 {
126         MonoLMF **lmf_addr = mono_get_lmf_addr ();
127         MonoContinuationRestore restore_state = mono_tasklets_arch_restore ();
128
129         if (!cont->domain || !cont->return_sp)
130                 return mono_get_exception_argument ("cont", "Continuation not initialized");
131         if (cont->domain != mono_domain_get () || !mono_native_thread_id_equals (cont->thread_id, mono_native_thread_id_get ()))
132                 return mono_get_exception_argument ("cont", "Continuation from another thread or domain");
133
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 ();
138 }
139
140 void
141 mono_tasklets_init (void)
142 {
143         mono_os_mutex_init_recursive (&tasklets_mutex);
144
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);
150 }
151
152 void
153 mono_tasklets_cleanup (void)
154 {
155 }
156 #else
157
158 static
159 void continuations_not_supported (void)
160 {
161         mono_set_pending_exception (mono_get_exception_not_implemented ("Tasklets are not implemented on this platform."));
162 }
163
164 static void*
165 continuation_alloc (void)
166 {
167         continuations_not_supported ();
168         return NULL;
169 }
170
171 static void
172 continuation_free (MonoContinuation *cont)
173 {
174         continuations_not_supported ();
175 }
176
177 static MonoException*
178 continuation_mark_frame (MonoContinuation *cont)
179 {
180         continuations_not_supported ();
181         return NULL;
182 }
183
184 static int
185 continuation_store (MonoContinuation *cont, int state, MonoException **e)
186 {
187         continuations_not_supported ();
188         return 0;
189 }
190
191 static MonoException*
192 continuation_restore (MonoContinuation *cont, int state)
193 {
194         continuations_not_supported ();
195         return NULL;
196 }
197
198 void
199 mono_tasklets_init(void)
200 {
201         mono_add_internal_call ("Mono.Tasklets.Continuation::alloc", continuation_alloc);
202         mono_add_internal_call ("Mono.Tasklets.Continuation::free", continuation_free);
203         mono_add_internal_call ("Mono.Tasklets.Continuation::mark", continuation_mark_frame);
204         mono_add_internal_call ("Mono.Tasklets.Continuation::store", continuation_store);
205         mono_add_internal_call ("Mono.Tasklets.Continuation::restore", continuation_restore);
206
207 }
208 #endif
209