Merge pull request #820 from brendanzagaeski/master
[mono.git] / mono / mini / tasklets.c
1
2 #include "config.h"
3 #include "tasklets.h"
4 #include "mono/metadata/exception.h"
5 #include "mono/metadata/gc-internal.h"
6 #include "mini.h"
7
8 #if defined(MONO_SUPPORT_TASKLETS)
9
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)
15
16 /* LOCKING: tasklets_mutex is assumed to e taken */
17 static void
18 internal_init (void)
19 {
20         if (keepalive_stacks)
21                 return;
22         MONO_GC_REGISTER_ROOT_PINNING (keepalive_stacks);
23         keepalive_stacks = mono_g_hash_table_new (NULL, NULL);
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                 tasklets_lock ();
38                 mono_g_hash_table_remove (keepalive_stacks, cont->saved_stack);
39                 tasklets_unlock ();
40                 mono_gc_free_fixed (cont->saved_stack);
41         }
42         g_free (cont);
43 }
44
45 static MonoException*
46 continuation_mark_frame (MonoContinuation *cont)
47 {
48         MonoJitTlsData *jit_tls;
49         MonoLMF *lmf;
50         MonoContext ctx, new_ctx;
51         MonoJitInfo *ji, rji;
52         int endloop = FALSE;
53
54         if (cont->domain)
55                 return mono_get_exception_argument ("cont", "Already marked");
56
57         jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
58         lmf = mono_get_lmf();
59         cont->domain = mono_domain_get ();
60         cont->thread_id = GetCurrentThreadId ();
61
62         /* get to the frame that called Mark () */
63         memset (&rji, 0, sizeof (rji));
64         do {
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");
68                 }
69                 ctx = new_ctx;
70                 if (endloop)
71                         break;
72                 if (strcmp (jinfo_get_method (ji)->name, "Mark") == 0)
73                         endloop = TRUE;
74         } while (1);
75
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);*/
78
79         return NULL;
80 }
81
82 static int
83 continuation_store (MonoContinuation *cont, int state, MonoException **e)
84 {
85         MonoLMF *lmf = mono_get_lmf ();
86         gsize num_bytes;
87
88         if (!cont->domain) {
89                 *e =  mono_get_exception_argument ("cont", "Continuation not initialized");
90                 return 0;
91         }
92         if (cont->domain != mono_domain_get () || cont->thread_id != GetCurrentThreadId ()) {
93                 *e = mono_get_exception_argument ("cont", "Continuation from another thread or domain");
94                 return 0;
95         }
96
97         cont->lmf = lmf;
98         cont->return_ip = __builtin_extract_return_addr (__builtin_return_address (0));
99         cont->return_sp = __builtin_frame_address (0);
100
101         num_bytes = (char*)cont->top_sp - (char*)cont->return_sp;
102
103         /*g_print ("store: %d bytes, sp: %p, ip: %p, lmf: %p\n", num_bytes, cont->return_sp, cont->return_ip, lmf);*/
104
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);
109                 }
110                 cont->stack_used_size = num_bytes;
111         } else {
112                 tasklets_lock ();
113                 internal_init ();
114                 if (cont->saved_stack) {
115                         mono_g_hash_table_remove (keepalive_stacks, cont->saved_stack);
116                         mono_gc_free_fixed (cont->saved_stack);
117                 }
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);
122                 tasklets_unlock ();
123         }
124         memcpy (cont->saved_stack, cont->return_sp, num_bytes);
125
126         return state;
127 }
128
129 static MonoException*
130 continuation_restore (MonoContinuation *cont, int state)
131 {
132         MonoLMF **lmf_addr = mono_get_lmf_addr ();
133         MonoContinuationRestore restore_state = mono_tasklets_arch_restore ();
134
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");
139
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 ();
144 }
145
146 void
147 mono_tasklets_init (void)
148 {
149         InitializeCriticalSection (&tasklets_mutex);
150
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);
156 }
157
158 void
159 mono_tasklets_cleanup (void)
160 {
161 }
162
163 #endif
164