22dd281000c090582d8500061514ebf788dc36b8
[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 (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 = TlsGetValue (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 (ji->method->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", ji->method->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         if (cont->domain != mono_domain_get () || cont->thread_id != GetCurrentThreadId ())
91                 *e = mono_get_exception_argument ("cont", "Continuation from another thread or domain");
92
93         cont->lmf = lmf;
94         cont->return_ip = __builtin_return_address (0);
95         cont->return_sp = __builtin_frame_address (0);
96
97         num_bytes = (char*)cont->top_sp - (char*)cont->return_sp;
98
99         /*g_print ("store: %d bytes, sp: %p, ip: %p, lmf: %p\n", num_bytes, cont->return_sp, cont->return_ip, lmf);*/
100
101         if (cont->saved_stack && num_bytes <= cont->stack_alloc_size) {
102                 /* clear to avoid GC retention */
103                 if (num_bytes < cont->stack_used_size)
104                         memset ((char*)cont->saved_stack + num_bytes, 0, cont->stack_used_size - num_bytes);
105         } else {
106                 tasklets_lock ();
107                 internal_init ();
108                 if (cont->saved_stack) {
109                         mono_g_hash_table_remove (keepalive_stacks, cont->saved_stack);
110                         mono_gc_free_fixed (cont->saved_stack);
111                 }
112                 cont->stack_used_size = num_bytes;
113                 cont->stack_alloc_size = num_bytes * 1.1;
114                 cont->saved_stack = mono_gc_alloc_fixed (cont->stack_alloc_size, NULL);
115                 mono_g_hash_table_insert (keepalive_stacks, cont->saved_stack, cont->saved_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 () || cont->thread_id != GetCurrentThreadId ())
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         InitializeCriticalSection (&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
157 #endif
158