System.Drawing: added email to icon and test file headers
[mono.git] / mono / utils / mono-threads.c
1 /*
2  * mono-threads.c: Low-level threading
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * Copyright 2011 Novell, Inc (http://www.novell.com)
8  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
9  */
10
11 #include <mono/utils/mono-compiler.h>
12 #include <mono/utils/mono-semaphore.h>
13 #include <mono/utils/mono-threads.h>
14 #include <mono/utils/mono-tls.h>
15 #include <mono/utils/hazard-pointer.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/domain-internals.h>
18
19 #include <errno.h>
20
21 #define THREADS_DEBUG(...)
22 //#define THREADS_DEBUG(...) g_message(__VA_ARGS__)
23
24 /*
25 Mutex that makes sure only a single thread can be suspending others.
26 Suspend is a very racy operation since it requires restarting until
27 the target thread is not on an unsafe region.
28
29 We could implement this using critical regions, but would be much much
30 harder for an operation that is hardly performance critical.
31
32 The GC has to acquire this lock before starting a STW to make sure
33 a runtime suspend won't make it wronly see a thread in a safepoint
34 when it is in fact not.
35 */
36 static CRITICAL_SECTION global_suspend_lock;
37
38
39 static int thread_info_size;
40 static MonoThreadInfoCallbacks threads_callbacks;
41 static MonoThreadInfoRuntimeCallbacks runtime_callbacks;
42 static MonoNativeTlsKey thread_info_key, small_id_key;
43 static MonoLinkedListSet thread_list;
44 static gboolean disable_new_interrupt = FALSE;
45
46 static inline void
47 mono_hazard_pointer_clear_all (MonoThreadHazardPointers *hp, int retain)
48 {
49         if (retain != 0)
50                 mono_hazard_pointer_clear (hp, 0);
51         if (retain != 1)
52                 mono_hazard_pointer_clear (hp, 1);
53         if (retain != 2)
54                 mono_hazard_pointer_clear (hp, 2);
55 }
56
57 /*
58 If return non null Hazard Pointer 1 holds the return value.
59 */
60 MonoThreadInfo*
61 mono_thread_info_lookup (MonoNativeThreadId id)
62 {
63                 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
64
65         if (!mono_lls_find (&thread_list, hp, (uintptr_t)id)) {
66                 mono_hazard_pointer_clear_all (hp, -1);
67                 return NULL;
68         } 
69
70         mono_hazard_pointer_clear_all (hp, 1);
71         return mono_hazard_pointer_get_val (hp, 1);
72 }
73
74 static gboolean
75 mono_thread_info_insert (MonoThreadInfo *info)
76 {
77         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
78
79         if (!mono_lls_insert (&thread_list, hp, (MonoLinkedListSetNode*)info)) {
80                 mono_hazard_pointer_clear_all (hp, -1);
81                 return FALSE;
82         } 
83
84         mono_hazard_pointer_clear_all (hp, -1);
85         return TRUE;
86 }
87
88 static gboolean
89 mono_thread_info_remove (MonoThreadInfo *info)
90 {
91         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
92         gboolean res;
93
94         THREADS_DEBUG ("removing info %p\n", info);
95         res = mono_lls_remove (&thread_list, hp, (MonoLinkedListSetNode*)info);
96         mono_hazard_pointer_clear_all (hp, -1);
97         return res;
98 }
99
100 static void
101 free_thread_info (gpointer mem)
102 {
103         MonoThreadInfo *info = mem;
104
105         DeleteCriticalSection (&info->suspend_lock);
106         MONO_SEM_DESTROY (&info->resume_semaphore);
107         MONO_SEM_DESTROY (&info->finish_resume_semaphore);
108         mono_threads_platform_free (info);
109
110         g_free (info);
111 }
112
113 int
114 mono_thread_info_register_small_id (void)
115 {
116         int small_id = mono_thread_small_id_alloc ();
117         mono_native_tls_set_value (small_id_key, GUINT_TO_POINTER (small_id + 1));
118         return small_id;
119 }
120
121 static void*
122 register_thread (MonoThreadInfo *info, gpointer baseptr)
123 {
124         int small_id = mono_thread_info_register_small_id ();
125         gboolean result;
126         mono_thread_info_set_tid (info, mono_native_thread_id_get ());
127         info->small_id = small_id;
128
129         InitializeCriticalSection (&info->suspend_lock);
130         MONO_SEM_INIT (&info->resume_semaphore, 0);
131         MONO_SEM_INIT (&info->finish_resume_semaphore, 0);
132
133         /*set TLS early so SMR works */
134         mono_native_tls_set_value (thread_info_key, info);
135
136         THREADS_DEBUG ("registering info %p tid %p small id %x\n", info, mono_thread_info_get_tid (info), info->small_id);
137
138         if (threads_callbacks.thread_register) {
139                 if (threads_callbacks.thread_register (info, baseptr) == NULL) {
140                         g_warning ("thread registation failed\n");
141                         g_free (info);
142                         return NULL;
143                 }
144         }
145
146         mono_threads_platform_register (info);
147
148         /*If this fail it means a given thread has been registered twice, which doesn't make sense. */
149         result = mono_thread_info_insert (info);
150         g_assert (result);
151         return info;
152 }
153
154 static void
155 unregister_thread (void *arg)
156 {
157         MonoThreadInfo *info = arg;
158         int small_id = info->small_id;
159         g_assert (info);
160
161         THREADS_DEBUG ("unregistering info %p\n", info);
162
163         /*
164          * TLS destruction order is not reliable so small_id might be cleaned up
165          * before us.
166          */
167         mono_native_tls_set_value (small_id_key, GUINT_TO_POINTER (info->small_id + 1));
168
169         /*
170         The unregister callback is reposible for calling mono_threads_unregister_current_thread
171         since it usually needs to be done in sync with the GC does a stop-the-world.
172         */
173         if (threads_callbacks.thread_unregister)
174                 threads_callbacks.thread_unregister (info);
175         else
176                 mono_threads_unregister_current_thread (info);
177
178         /*now it's safe to free the thread info.*/
179         mono_thread_hazardous_free_or_queue (info, free_thread_info);
180         mono_thread_small_id_free (small_id);
181 }
182
183 /**
184  * Removes the current thread from the thread list.
185  * This must be called from the thread unregister callback and nowhere else.
186  * The current thread must be passed as TLS might have already been cleaned up.
187 */
188 void
189 mono_threads_unregister_current_thread (MonoThreadInfo *info)
190 {
191         gboolean result;
192         g_assert (mono_thread_info_get_tid (info) == mono_native_thread_id_get ());
193         result = mono_thread_info_remove (info);
194         g_assert (result);
195
196 }
197
198 MonoThreadInfo*
199 mono_thread_info_current (void)
200 {
201         return mono_native_tls_get_value (thread_info_key);
202 }
203
204 int
205 mono_thread_info_get_small_id (void)
206 {
207         gpointer val = mono_native_tls_get_value (small_id_key);
208         if (!val)
209                 return -1;
210         return GPOINTER_TO_INT (val) - 1;
211 }
212
213 MonoLinkedListSet*
214 mono_thread_info_list_head (void)
215 {
216         return &thread_list;
217 }
218
219 MonoThreadInfo*
220 mono_thread_info_attach (void *baseptr)
221 {
222         MonoThreadInfo *info = mono_native_tls_get_value (thread_info_key);
223         if (!info) {
224                 info = g_malloc0 (thread_info_size);
225                 THREADS_DEBUG ("attaching %p\n", info);
226                 if (!register_thread (info, baseptr))
227                         return NULL;
228         } else if (threads_callbacks.thread_attach) {
229                 threads_callbacks.thread_attach (info);
230         }
231         return info;
232 }
233
234 void
235 mono_thread_info_dettach (void)
236 {
237         MonoThreadInfo *info = mono_native_tls_get_value (thread_info_key);
238         if (info) {
239                 THREADS_DEBUG ("detaching %p\n", info);
240                 unregister_thread (info);
241         }
242 }
243
244 void
245 mono_threads_init (MonoThreadInfoCallbacks *callbacks, size_t info_size)
246 {
247         gboolean res;
248         threads_callbacks = *callbacks;
249         thread_info_size = info_size;
250 #ifdef HOST_WIN32
251         res = mono_native_tls_alloc (&thread_info_key, NULL);
252 #else
253         res = mono_native_tls_alloc (&thread_info_key, unregister_thread);
254 #endif
255         g_assert (res);
256
257         res = mono_native_tls_alloc (&small_id_key, NULL);
258         g_assert (res);
259
260         InitializeCriticalSection (&global_suspend_lock);
261
262         mono_lls_init (&thread_list, NULL);
263         mono_thread_smr_init ();
264         mono_threads_init_platform ();
265
266         g_assert (sizeof (MonoNativeThreadId) <= sizeof (uintptr_t));
267 }
268
269 void
270 mono_threads_runtime_init (MonoThreadInfoRuntimeCallbacks *callbacks)
271 {
272         runtime_callbacks = *callbacks;
273 }
274
275 MonoThreadInfoCallbacks *
276 mono_threads_get_callbacks (void)
277 {
278         return &threads_callbacks;
279 }
280
281 MonoThreadInfoRuntimeCallbacks *
282 mono_threads_get_runtime_callbacks (void)
283 {
284         return &runtime_callbacks;
285 }
286
287 /*
288 The return value is only valid until a matching mono_thread_info_resume is called
289 */
290 static MonoThreadInfo*
291 mono_thread_info_suspend_sync (MonoNativeThreadId tid, gboolean interrupt_kernel)
292 {
293         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();      
294         MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
295         if (!info)
296                 return NULL;
297
298         EnterCriticalSection (&info->suspend_lock);
299
300         /*thread is on the process of detaching*/
301         if (mono_thread_info_run_state (info) > STATE_RUNNING) {
302                 mono_hazard_pointer_clear (hp, 1);
303                 return NULL;
304         }
305
306         THREADS_DEBUG ("suspend %x IN COUNT %d\n", tid, info->suspend_count);
307
308         if (info->suspend_count) {
309                 ++info->suspend_count;
310                 mono_hazard_pointer_clear (hp, 1);
311                 LeaveCriticalSection (&info->suspend_lock);
312                 return info;
313         }
314
315         if (!mono_threads_core_suspend (info)) {
316                 LeaveCriticalSection (&info->suspend_lock);
317                 mono_hazard_pointer_clear (hp, 1);
318                 return NULL;
319         }
320
321         if (interrupt_kernel) 
322                 mono_threads_core_interrupt (info);
323
324         ++info->suspend_count;
325         info->thread_state |= STATE_SUSPENDED;
326         LeaveCriticalSection (&info->suspend_lock);
327         mono_hazard_pointer_clear (hp, 1);
328
329         return info;
330 }
331
332 void
333 mono_thread_info_self_suspend (void)
334 {
335         gboolean ret;
336         MonoThreadInfo *info = mono_thread_info_current ();
337         if (!info)
338                 return;
339
340         EnterCriticalSection (&info->suspend_lock);
341
342         THREADS_DEBUG ("self suspend IN COUNT %d\n", info->suspend_count);
343
344         g_assert (info->suspend_count == 0);
345         ++info->suspend_count;
346
347         info->thread_state |= STATE_SELF_SUSPENDED;
348
349         ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&info->suspend_state, NULL);
350         g_assert (ret);
351
352         LeaveCriticalSection (&info->suspend_lock);
353
354         while (MONO_SEM_WAIT (&info->resume_semaphore) != 0) {
355                 /*if (EINTR != errno) ABORT("sem_wait failed"); */
356         }
357
358         g_assert (!info->async_target); /*FIXME this should happen normally for suspend. */
359         MONO_SEM_POST (&info->finish_resume_semaphore);
360 }
361
362 static gboolean
363 mono_thread_info_resume_internal (MonoThreadInfo *info)
364 {
365         gboolean result;
366         if (mono_thread_info_suspend_state (info) == STATE_SELF_SUSPENDED) {
367                 MONO_SEM_POST (&info->resume_semaphore);
368                 while (MONO_SEM_WAIT (&info->finish_resume_semaphore) != 0) {
369                         /* g_assert (errno == EINTR); */
370                 }
371                 result = TRUE;
372         } else {
373                 result = mono_threads_core_resume (info);
374         }
375         info->thread_state &= ~SUSPEND_STATE_MASK;
376         return result;
377 }
378
379 gboolean
380 mono_thread_info_resume (MonoNativeThreadId tid)
381 {
382         gboolean result = TRUE;
383         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();      
384         MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
385         if (!info)
386                 return FALSE;
387
388         EnterCriticalSection (&info->suspend_lock);
389
390         THREADS_DEBUG ("resume %x IN COUNT %d\n",tid, info->suspend_count);
391
392         if (info->suspend_count <= 0) {
393                 LeaveCriticalSection (&info->suspend_lock);
394                 mono_hazard_pointer_clear (hp, 1);
395                 return FALSE;
396         }
397
398         /*
399          * The theory here is that if we manage to suspend the thread it means it did not
400          * start cleanup since it take the same lock. 
401         */
402         g_assert (mono_thread_info_get_tid (info));
403
404         if (--info->suspend_count == 0)
405                 result = mono_thread_info_resume_internal (info);
406
407         LeaveCriticalSection (&info->suspend_lock);
408         mono_hazard_pointer_clear (hp, 1);
409
410         return result;
411 }
412
413 /*
414 FIXME fix cardtable WB to be out of line and check with the runtime if the target is not the
415 WB trampoline. Another option is to encode wb ranges in MonoJitInfo, but that is somewhat hard.
416 */
417 static gboolean
418 is_thread_in_critical_region (MonoThreadInfo *info)
419 {
420         MonoMethod *method;
421         MonoJitInfo *ji = mono_jit_info_table_find (
422                 info->suspend_state.unwind_data [MONO_UNWIND_DATA_DOMAIN],
423                 MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
424
425         if (!ji)
426                 return FALSE;
427
428         method = ji->method;
429
430         return threads_callbacks.mono_method_is_critical (method);
431 }
432
433 /*
434 WARNING:
435 If we are trying to suspend a target that is on a critical region
436 and running a syscall we risk looping forever if @interrupt_kernel is FALSE.
437 So, be VERY carefull in calling this with @interrupt_kernel == FALSE.
438 */
439 MonoThreadInfo*
440 mono_thread_info_safe_suspend_sync (MonoNativeThreadId id, gboolean interrupt_kernel)
441 {
442         MonoThreadInfo *info = NULL;
443         int sleep_duration = 0;
444
445         /*FIXME: unify this with self-suspend*/
446         g_assert (id != mono_native_thread_id_get ());
447
448         mono_thread_info_suspend_lock ();
449
450         for (;;) {
451                 if (!(info = mono_thread_info_suspend_sync (id, interrupt_kernel))) {
452                         g_warning ("failed to suspend thread %p, hopefully it is dead", (gpointer)id);
453                         mono_thread_info_suspend_unlock ();
454                         return NULL;
455                 }
456                 /*WARNING: We now are in interrupt context until we resume the thread. */
457                 if (!is_thread_in_critical_region (info))
458                         break;
459
460                 if (!mono_thread_info_resume (id)) {
461                         g_warning ("failed to result thread %p, hopefully it is dead", (gpointer)id);
462                         mono_thread_info_suspend_unlock ();
463                         return NULL;
464                 }
465                 THREADS_DEBUG ("restarted thread %p\n", (gpointer)id);
466
467                 if (!sleep_duration) {
468 #ifdef HOST_WIN32
469                         SwitchToThread ();
470 #else
471                         sched_yield ();
472 #endif
473                 }
474                 else {
475                         g_usleep (sleep_duration);
476                 }
477                 sleep_duration += 10;
478         }
479
480         mono_thread_info_suspend_unlock ();
481         return info;
482 }
483
484 /**
485 Inject an assynchronous call into the target thread. The target thread must be suspended and
486 only a single async call can be setup for a given suspend cycle.
487 This async call must cause stack unwinding as the current implementation doesn't save enough state
488 to resume execution of the top-of-stack function. It's an acceptable limitation since this is
489 currently used only to deliver exceptions.
490 */
491 void
492 mono_thread_info_setup_async_call (MonoThreadInfo *info, void (*target_func)(void*), void *user_data)
493 {
494         g_assert (info->suspend_count);
495         /*FIXME this is a bad assert, we probably should do proper locking and fail if one is already set*/
496         g_assert (!info->async_target);
497         info->async_target = target_func;
498         /* This is not GC tracked */
499         info->user_data = user_data;
500 }
501
502 /*
503 The suspend lock is held during any suspend in progress.
504 A GC that has safepoints must take this lock as part of its
505 STW to make sure no unsafe pending suspend is in progress.   
506 */
507 void
508 mono_thread_info_suspend_lock (void)
509 {
510         EnterCriticalSection (&global_suspend_lock);
511 }
512
513 void
514 mono_thread_info_suspend_unlock (void)
515 {
516         LeaveCriticalSection (&global_suspend_lock);
517 }
518
519 void
520 mono_thread_info_disable_new_interrupt (gboolean disable)
521 {
522         disable_new_interrupt = disable;
523 }
524 /*
525 Disabled by default for now.
526 To enable this we need mini to implement the callbacks by MonoThreadInfoRuntimeCallbacks
527 which means mono-context and setup_async_callback, and we need a mono-threads backend.
528 */
529 gboolean
530 mono_thread_info_new_interrupt_enabled (void)
531 {
532         /*We need STW gc events to work correctly*/
533 #if defined (HAVE_BOEHM_GC) && !defined (USE_INCLUDED_LIBGC)
534         return FALSE;
535 #endif
536         /*port not done*/
537 #if defined(HOST_WIN32)
538         return FALSE;
539 #endif
540 #if defined (__i386__)
541         return !disable_new_interrupt;
542 #endif
543         return FALSE;
544 }