[System.Net] Add support for .pac proxy config scripts on mac
[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/utils/mono-memory-model.h>
17
18 #include <errno.h>
19
20 #if defined(__MACH__)
21 #include <mono/utils/mach-support.h>
22 #endif
23
24 #define THREADS_DEBUG(...)
25 //#define THREADS_DEBUG(...) g_message(__VA_ARGS__)
26
27 /*
28 Mutex that makes sure only a single thread can be suspending others.
29 Suspend is a very racy operation since it requires restarting until
30 the target thread is not on an unsafe region.
31
32 We could implement this using critical regions, but would be much much
33 harder for an operation that is hardly performance critical.
34
35 The GC has to acquire this lock before starting a STW to make sure
36 a runtime suspend won't make it wronly see a thread in a safepoint
37 when it is in fact not.
38 */
39 static MonoSemType global_suspend_semaphore;
40
41 static size_t thread_info_size;
42 static MonoThreadInfoCallbacks threads_callbacks;
43 static MonoThreadInfoRuntimeCallbacks runtime_callbacks;
44 static MonoNativeTlsKey thread_info_key, small_id_key;
45 static MonoLinkedListSet thread_list;
46 static gboolean disable_new_interrupt = FALSE;
47 static gboolean mono_threads_inited = FALSE;
48
49 static void mono_threads_unregister_current_thread (MonoThreadInfo *info);
50
51
52 static inline void
53 mono_hazard_pointer_clear_all (MonoThreadHazardPointers *hp, int retain)
54 {
55         if (retain != 0)
56                 mono_hazard_pointer_clear (hp, 0);
57         if (retain != 1)
58                 mono_hazard_pointer_clear (hp, 1);
59         if (retain != 2)
60                 mono_hazard_pointer_clear (hp, 2);
61 }
62
63 /*
64 If return non null Hazard Pointer 1 holds the return value.
65 */
66 MonoThreadInfo*
67 mono_thread_info_lookup (MonoNativeThreadId id)
68 {
69                 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
70
71         if (!mono_lls_find (&thread_list, hp, (uintptr_t)id)) {
72                 mono_hazard_pointer_clear_all (hp, -1);
73                 return NULL;
74         } 
75
76         mono_hazard_pointer_clear_all (hp, 1);
77         return mono_hazard_pointer_get_val (hp, 1);
78 }
79
80 static gboolean
81 mono_thread_info_insert (MonoThreadInfo *info)
82 {
83         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
84
85         if (!mono_lls_insert (&thread_list, hp, (MonoLinkedListSetNode*)info)) {
86                 mono_hazard_pointer_clear_all (hp, -1);
87                 return FALSE;
88         } 
89
90         mono_hazard_pointer_clear_all (hp, -1);
91         return TRUE;
92 }
93
94 static gboolean
95 mono_thread_info_remove (MonoThreadInfo *info)
96 {
97         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
98         gboolean res;
99
100         THREADS_DEBUG ("removing info %p\n", info);
101         res = mono_lls_remove (&thread_list, hp, (MonoLinkedListSetNode*)info);
102         mono_hazard_pointer_clear_all (hp, -1);
103         return res;
104 }
105
106 static void
107 free_thread_info (gpointer mem)
108 {
109         MonoThreadInfo *info = mem;
110
111         MONO_SEM_DESTROY (&info->suspend_semaphore);
112         MONO_SEM_DESTROY (&info->resume_semaphore);
113         MONO_SEM_DESTROY (&info->finish_resume_semaphore);
114         mono_threads_platform_free (info);
115
116         g_free (info);
117 }
118
119 int
120 mono_thread_info_register_small_id (void)
121 {
122         int small_id = mono_thread_small_id_alloc ();
123         mono_native_tls_set_value (small_id_key, GUINT_TO_POINTER (small_id + 1));
124         return small_id;
125 }
126
127 static void*
128 register_thread (MonoThreadInfo *info, gpointer baseptr)
129 {
130         int small_id = mono_thread_info_register_small_id ();
131         gboolean result;
132         mono_thread_info_set_tid (info, mono_native_thread_id_get ());
133         info->small_id = small_id;
134
135         MONO_SEM_INIT (&info->suspend_semaphore, 1);
136         MONO_SEM_INIT (&info->resume_semaphore, 0);
137         MONO_SEM_INIT (&info->finish_resume_semaphore, 0);
138
139         /*set TLS early so SMR works */
140         mono_native_tls_set_value (thread_info_key, info);
141
142         THREADS_DEBUG ("registering info %p tid %p small id %x\n", info, mono_thread_info_get_tid (info), info->small_id);
143
144         if (threads_callbacks.thread_register) {
145                 if (threads_callbacks.thread_register (info, baseptr) == NULL) {
146                         g_warning ("thread registation failed\n");
147                         g_free (info);
148                         return NULL;
149                 }
150         }
151
152         mono_threads_platform_register (info);
153         info->thread_state = STATE_RUNNING;
154         mono_thread_info_suspend_lock ();
155         /*If this fail it means a given thread has been registered twice, which doesn't make sense. */
156         result = mono_thread_info_insert (info);
157         g_assert (result);
158         mono_thread_info_suspend_unlock ();
159         return info;
160 }
161
162 static void
163 unregister_thread (void *arg)
164 {
165         MonoThreadInfo *info = arg;
166         int small_id = info->small_id;
167         g_assert (info);
168
169         THREADS_DEBUG ("unregistering info %p\n", info);
170
171         mono_threads_core_unregister (info);
172
173         /*
174          * TLS destruction order is not reliable so small_id might be cleaned up
175          * before us.
176          */
177         mono_native_tls_set_value (small_id_key, GUINT_TO_POINTER (info->small_id + 1));
178
179         info->thread_state = STATE_SHUTTING_DOWN;
180
181         /*
182         First perform the callback that requires no locks.
183         This callback has the potential of taking other locks, so we do it before.
184         After it completes, the thread remains functional.
185         */
186         if (threads_callbacks.thread_detach)
187                 threads_callbacks.thread_detach (info);
188
189         mono_thread_info_suspend_lock ();
190
191         /*
192         Now perform the callback that must be done under locks.
193         This will render the thread useless and non-suspendable, so it must
194         be done while holding the suspend lock to give no other thread chance
195         to suspend it.
196         */
197         if (threads_callbacks.thread_unregister)
198                 threads_callbacks.thread_unregister (info);
199         mono_threads_unregister_current_thread (info);
200
201         info->thread_state = STATE_DEAD;
202         mono_thread_info_suspend_unlock ();
203
204         /*now it's safe to free the thread info.*/
205         mono_thread_hazardous_free_or_queue (info, free_thread_info, TRUE, FALSE);
206         mono_thread_small_id_free (small_id);
207 }
208
209 /**
210  * Removes the current thread from the thread list.
211  * This must be called from the thread unregister callback and nowhere else.
212  * The current thread must be passed as TLS might have already been cleaned up.
213 */
214 static void
215 mono_threads_unregister_current_thread (MonoThreadInfo *info)
216 {
217         gboolean result;
218         g_assert (mono_thread_info_get_tid (info) == mono_native_thread_id_get ());
219         result = mono_thread_info_remove (info);
220         g_assert (result);
221 }
222
223 MonoThreadInfo*
224 mono_thread_info_current (void)
225 {
226         MonoThreadInfo *info = (MonoThreadInfo*)mono_native_tls_get_value (thread_info_key);
227         if (info)
228                 return info;
229
230         info = mono_thread_info_lookup (mono_native_thread_id_get ()); /*info on HP1*/
231
232         /*
233         We might be called during thread cleanup, but we cannot be called after cleanup as happened.
234         The way to distinguish between before, during and after cleanup is the following:
235
236         -If the TLS key is set, cleanup has not begun;
237         -If the TLS key is clean, but the thread remains registered, cleanup is in progress;
238         -If the thread is nowhere to be found, cleanup has finished.
239
240         We cannot function after cleanup since there's no way to ensure what will happen.
241         */
242         g_assert (info);
243
244         /*We're looking up the current thread which will not be freed until we finish running, so no need to keep it on a HP */
245         mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
246
247         return info;
248 }
249
250 int
251 mono_thread_info_get_small_id (void)
252 {
253         gpointer val = mono_native_tls_get_value (small_id_key);
254         if (!val)
255                 return -1;
256         return GPOINTER_TO_INT (val) - 1;
257 }
258
259 MonoLinkedListSet*
260 mono_thread_info_list_head (void)
261 {
262         return &thread_list;
263 }
264
265 MonoThreadInfo*
266 mono_thread_info_attach (void *baseptr)
267 {
268         MonoThreadInfo *info;
269         if (!mono_threads_inited)
270         {
271                 /* This can happen from DllMain(DLL_THREAD_ATTACH) on Windows, if a
272                  * thread is created before an embedding API user initialized Mono. */
273                 THREADS_DEBUG ("mono_thread_info_attach called before mono_threads_init\n");
274                 return NULL;
275         }
276         info = mono_native_tls_get_value (thread_info_key);
277         if (!info) {
278                 info = g_malloc0 (thread_info_size);
279                 THREADS_DEBUG ("attaching %p\n", info);
280                 if (!register_thread (info, baseptr))
281                         return NULL;
282         } else if (threads_callbacks.thread_attach) {
283                 threads_callbacks.thread_attach (info);
284         }
285         return info;
286 }
287
288 void
289 mono_thread_info_detach (void)
290 {
291         MonoThreadInfo *info;
292         if (!mono_threads_inited)
293         {
294                 /* This can happen from DllMain(THREAD_DETACH) on Windows, if a thread
295                  * is created before an embedding API user initialized Mono. */
296                 THREADS_DEBUG ("mono_thread_info_detach called before mono_threads_init\n");
297                 return;
298         }
299         info = mono_native_tls_get_value (thread_info_key);
300         if (info) {
301                 THREADS_DEBUG ("detaching %p\n", info);
302                 unregister_thread (info);
303                 mono_native_tls_set_value (thread_info_key, NULL);
304         }
305 }
306
307 void
308 mono_threads_init (MonoThreadInfoCallbacks *callbacks, size_t info_size)
309 {
310         gboolean res;
311         threads_callbacks = *callbacks;
312         thread_info_size = info_size;
313 #ifdef HOST_WIN32
314         res = mono_native_tls_alloc (&thread_info_key, NULL);
315 #else
316         res = mono_native_tls_alloc (&thread_info_key, unregister_thread);
317 #endif
318         g_assert (res);
319
320         res = mono_native_tls_alloc (&small_id_key, NULL);
321         g_assert (res);
322
323         MONO_SEM_INIT (&global_suspend_semaphore, 1);
324
325         mono_lls_init (&thread_list, NULL);
326         mono_thread_smr_init ();
327         mono_threads_init_platform ();
328
329 #if defined(__MACH__)
330         mono_mach_init (thread_info_key);
331 #endif
332
333         mono_threads_inited = TRUE;
334
335         g_assert (sizeof (MonoNativeThreadId) <= sizeof (uintptr_t));
336 }
337
338 void
339 mono_threads_runtime_init (MonoThreadInfoRuntimeCallbacks *callbacks)
340 {
341         runtime_callbacks = *callbacks;
342 }
343
344 MonoThreadInfoCallbacks *
345 mono_threads_get_callbacks (void)
346 {
347         return &threads_callbacks;
348 }
349
350 MonoThreadInfoRuntimeCallbacks *
351 mono_threads_get_runtime_callbacks (void)
352 {
353         return &runtime_callbacks;
354 }
355
356 /*
357 The return value is only valid until a matching mono_thread_info_resume is called
358 */
359 static MonoThreadInfo*
360 mono_thread_info_suspend_sync (MonoNativeThreadId tid, gboolean interrupt_kernel)
361 {
362         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();      
363         MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
364         if (!info)
365                 return NULL;
366
367         MONO_SEM_WAIT_UNITERRUPTIBLE (&info->suspend_semaphore);
368
369         /*thread is on the process of detaching*/
370         if (mono_thread_info_run_state (info) > STATE_RUNNING) {
371                 mono_hazard_pointer_clear (hp, 1);
372                 return NULL;
373         }
374
375         THREADS_DEBUG ("suspend %x IN COUNT %d\n", tid, info->suspend_count);
376
377         if (info->suspend_count) {
378                 ++info->suspend_count;
379                 mono_hazard_pointer_clear (hp, 1);
380                 MONO_SEM_POST (&info->suspend_semaphore);
381                 return info;
382         }
383
384         if (!mono_threads_core_suspend (info)) {
385                 MONO_SEM_POST (&info->suspend_semaphore);
386                 mono_hazard_pointer_clear (hp, 1);
387                 return NULL;
388         }
389
390         if (interrupt_kernel) 
391                 mono_threads_core_interrupt (info);
392
393         ++info->suspend_count;
394         info->thread_state |= STATE_SUSPENDED;
395         MONO_SEM_POST (&info->suspend_semaphore);
396
397         return info;
398 }
399
400 void
401 mono_thread_info_self_suspend (void)
402 {
403         gboolean ret;
404         MonoThreadInfo *info = mono_thread_info_current ();
405         if (!info)
406                 return;
407
408         MONO_SEM_WAIT_UNITERRUPTIBLE (&info->suspend_semaphore);
409
410         THREADS_DEBUG ("self suspend IN COUNT %d\n", info->suspend_count);
411
412         g_assert (info->suspend_count == 0);
413         ++info->suspend_count;
414
415         info->thread_state |= STATE_SELF_SUSPENDED;
416
417         ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&info->suspend_state, NULL);
418         g_assert (ret);
419
420         MONO_SEM_POST (&info->suspend_semaphore);
421
422         MONO_SEM_WAIT_UNITERRUPTIBLE (&info->resume_semaphore);
423
424         g_assert (!info->async_target); /*FIXME this should happen normally for suspend. */
425         MONO_SEM_POST (&info->finish_resume_semaphore);
426 }
427
428 static gboolean
429 mono_thread_info_core_resume (MonoThreadInfo *info)
430 {
431         gboolean result;
432         MonoNativeThreadId tid = mono_thread_info_get_tid (info);
433         if (info->create_suspended) {
434                 /* Have to special case this, as the normal suspend/resume pair are racy, they don't work if he resume is received before the suspend */
435                 info->create_suspended = FALSE;
436                 mono_threads_core_resume_created (info, tid);
437                 return TRUE;
438         }
439
440         MONO_SEM_WAIT_UNITERRUPTIBLE (&info->suspend_semaphore);
441
442         THREADS_DEBUG ("resume %x IN COUNT %d\n", tid, info->suspend_count);
443
444         if (info->suspend_count <= 0) {
445                 MONO_SEM_POST (&info->suspend_semaphore);
446                 return FALSE;
447         }
448
449         /*
450          * The theory here is that if we manage to suspend the thread it means it did not
451          * start cleanup since it take the same lock. 
452         */
453         g_assert (mono_thread_info_get_tid (info));
454
455         if (--info->suspend_count == 0) {
456                 if (mono_thread_info_suspend_state (info) == STATE_SELF_SUSPENDED) {
457                         MONO_SEM_POST (&info->resume_semaphore);
458                         MONO_SEM_WAIT_UNITERRUPTIBLE (&info->finish_resume_semaphore);
459                         result = TRUE;
460                 } else {
461                         result = mono_threads_core_resume (info);
462                 }
463                 info->thread_state &= ~SUSPEND_STATE_MASK;
464         } else {
465                 result = TRUE;
466         }
467
468         MONO_SEM_POST (&info->suspend_semaphore);
469         return result;
470 }
471
472 gboolean
473 mono_thread_info_resume (MonoNativeThreadId tid)
474 {
475         gboolean result; /* don't initialize it so the compiler can catch unitilized paths. */
476         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
477         MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
478
479         if (!info) {
480                 result = FALSE;
481                 goto cleanup;
482         }
483         result = mono_thread_info_core_resume (info);
484
485 cleanup:
486         mono_hazard_pointer_clear (hp, 1);
487         return result;
488 }
489
490 void
491 mono_thread_info_finish_suspend (MonoThreadInfo *info)
492 {
493         mono_atomic_store_release (&mono_thread_info_current ()->inside_critical_region, FALSE);
494 }
495
496 void
497 mono_thread_info_finish_suspend_and_resume (MonoThreadInfo *info)
498 {
499         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
500
501         /*Resume can access info after the target has resumed, so we must ensure it won't touch freed memory. */
502         mono_hazard_pointer_set (hp, 1, info);
503         mono_thread_info_core_resume (info);
504         mono_hazard_pointer_clear (hp, 1);
505
506         mono_atomic_store_release (&mono_thread_info_current ()->inside_critical_region, FALSE);
507 }
508
509 /*
510 FIXME fix cardtable WB to be out of line and check with the runtime if the target is not the
511 WB trampoline. Another option is to encode wb ranges in MonoJitInfo, but that is somewhat hard.
512 */
513 static gboolean
514 is_thread_in_critical_region (MonoThreadInfo *info)
515 {
516         MonoMethod *method;
517         MonoJitInfo *ji;
518
519         if (info->inside_critical_region)
520                 return TRUE;
521
522         /* The target thread might be shutting down and the domain might be null, which means no managed code left to run. */
523         if (!info->suspend_state.unwind_data [MONO_UNWIND_DATA_DOMAIN])
524                 return FALSE;
525
526         ji = mono_jit_info_table_find (
527                 info->suspend_state.unwind_data [MONO_UNWIND_DATA_DOMAIN],
528                 MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
529
530         if (!ji)
531                 return FALSE;
532
533         method = mono_jit_info_get_method (ji);
534
535         return threads_callbacks.mono_method_is_critical (method);
536 }
537
538 /*
539 WARNING:
540 If we are trying to suspend a target that is on a critical region
541 and running a syscall we risk looping forever if @interrupt_kernel is FALSE.
542 So, be VERY carefull in calling this with @interrupt_kernel == FALSE.
543
544 Info is not put on a hazard pointer as a suspended thread cannot exit and be freed.
545
546 This function MUST be matched with mono_thread_info_finish_suspend or mono_thread_info_finish_suspend_and_resume
547 */
548 MonoThreadInfo*
549 mono_thread_info_safe_suspend_sync (MonoNativeThreadId id, gboolean interrupt_kernel)
550 {
551         MonoThreadInfo *info = NULL;
552         int sleep_duration = 0;
553
554         /*FIXME: unify this with self-suspend*/
555         g_assert (id != mono_native_thread_id_get ());
556
557         mono_thread_info_suspend_lock ();
558
559         for (;;) {
560                 if (!(info = mono_thread_info_suspend_sync (id, interrupt_kernel))) {
561                         g_warning ("failed to suspend thread %p, hopefully it is dead", (gpointer)id);
562                         mono_thread_info_suspend_unlock ();
563                         return NULL;
564                 }
565                 /*WARNING: We now are in interrupt context until we resume the thread. */
566                 if (!is_thread_in_critical_region (info))
567                         break;
568
569                 if (!mono_thread_info_core_resume (info)) {
570                         g_warning ("failed to result thread %p, hopefully it is dead", (gpointer)id);
571                         mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
572                         mono_thread_info_suspend_unlock ();
573                         return NULL;
574                 }
575                 THREADS_DEBUG ("restarted thread %p\n", (gpointer)id);
576
577                 if (!sleep_duration) {
578 #ifdef HOST_WIN32
579                         SwitchToThread ();
580 #else
581                         sched_yield ();
582 #endif
583                 }
584                 else {
585                         g_usleep (sleep_duration);
586                 }
587                 sleep_duration += 10;
588         }
589
590         /* XXX this clears HP 1, so we restated it again */
591         mono_atomic_store_release (&mono_thread_info_current ()->inside_critical_region, TRUE);
592         mono_thread_info_suspend_unlock ();
593
594         return info;
595 }
596
597 /**
598 Inject an assynchronous call into the target thread. The target thread must be suspended and
599 only a single async call can be setup for a given suspend cycle.
600 This async call must cause stack unwinding as the current implementation doesn't save enough state
601 to resume execution of the top-of-stack function. It's an acceptable limitation since this is
602 currently used only to deliver exceptions.
603 */
604 void
605 mono_thread_info_setup_async_call (MonoThreadInfo *info, void (*target_func)(void*), void *user_data)
606 {
607         g_assert (info->suspend_count);
608         /*FIXME this is a bad assert, we probably should do proper locking and fail if one is already set*/
609         g_assert (!info->async_target);
610         info->async_target = target_func;
611         /* This is not GC tracked */
612         info->user_data = user_data;
613 }
614
615 /*
616 The suspend lock is held during any suspend in progress.
617 A GC that has safepoints must take this lock as part of its
618 STW to make sure no unsafe pending suspend is in progress.   
619 */
620 void
621 mono_thread_info_suspend_lock (void)
622 {
623         MONO_SEM_WAIT_UNITERRUPTIBLE (&global_suspend_semaphore);
624 }
625
626 void
627 mono_thread_info_suspend_unlock (void)
628 {
629         MONO_SEM_POST (&global_suspend_semaphore);
630 }
631
632 void
633 mono_thread_info_disable_new_interrupt (gboolean disable)
634 {
635         disable_new_interrupt = disable;
636 }
637
638 /*
639  * This is a very specific function whose only purpose is to
640  * break a given thread from socket syscalls.
641  *
642  * This only exists because linux won't fail a call to connect
643  * if the underlying is closed.
644  *
645  * TODO We should cleanup and unify this with the other syscall abort
646  * facility.
647  */
648 void
649 mono_thread_info_abort_socket_syscall_for_close (MonoNativeThreadId tid)
650 {
651         MonoThreadHazardPointers *hp;
652         MonoThreadInfo *info;
653         
654         if (tid == mono_native_thread_id_get () || !mono_threads_core_needs_abort_syscall ())
655                 return;
656
657         hp = mono_hazard_pointer_get ();        
658         info = mono_thread_info_lookup (tid); /*info on HP1*/
659         if (!info)
660                 return;
661
662         if (mono_thread_info_run_state (info) > STATE_RUNNING) {
663                 mono_hazard_pointer_clear (hp, 1);
664                 return;
665         }
666
667         mono_thread_info_suspend_lock ();
668
669         mono_threads_core_abort_syscall (info);
670
671         mono_hazard_pointer_clear (hp, 1);
672         mono_thread_info_suspend_unlock ();
673 }
674
675 /*
676 Disabled by default for now.
677 To enable this we need mini to implement the callbacks by MonoThreadInfoRuntimeCallbacks
678 which means mono-context and setup_async_callback, and we need a mono-threads backend.
679 */
680 gboolean
681 mono_thread_info_new_interrupt_enabled (void)
682 {
683         /*We need STW gc events to work correctly*/
684 #if defined (HAVE_BOEHM_GC) && !defined (USE_INCLUDED_LIBGC)
685         return FALSE;
686 #endif
687         /*port not done*/
688 #if defined(HOST_WIN32)
689         return FALSE;
690 #endif
691 #if defined (__i386__)
692         return !disable_new_interrupt;
693 #endif
694         return FALSE;
695 }
696
697 /*
698  * mono_thread_info_set_is_async_context:
699  *
700  *   Set whenever the current thread is in an async context. Some runtime functions might behave
701  * differently while in an async context in order to be async safe.
702  */
703 void
704 mono_thread_info_set_is_async_context (gboolean async_context)
705 {
706         MonoThreadInfo *info = mono_thread_info_current ();
707
708         if (info)
709                 info->is_async_context = async_context;
710 }
711
712 gboolean
713 mono_thread_info_is_async_context (void)
714 {
715         MonoThreadInfo *info = mono_thread_info_current ();
716
717         if (info)
718                 return info->is_async_context;
719         else
720                 return FALSE;
721 }
722
723 /*
724  * mono_threads_create_thread:
725  *
726  *   Create a new thread executing START with argument ARG. Store its id into OUT_TID.
727  * Returns: a windows or io-layer handle for the thread.
728  */
729 HANDLE
730 mono_threads_create_thread (LPTHREAD_START_ROUTINE start, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid)
731 {
732         return mono_threads_core_create_thread (start, arg, stack_size, creation_flags, out_tid);
733 }
734
735 /*
736  * mono_thread_info_get_stack_bounds:
737  *
738  *   Return the address and size of the current threads stack. Return NULL as the 
739  * stack address if the stack address cannot be determined.
740  */
741 void
742 mono_thread_info_get_stack_bounds (guint8 **staddr, size_t *stsize)
743 {
744         mono_threads_core_get_stack_bounds (staddr, stsize);
745 }
746
747 gboolean
748 mono_thread_info_yield (void)
749 {
750         return mono_threads_core_yield ();
751 }
752
753 gpointer
754 mono_thread_info_tls_get (THREAD_INFO_TYPE *info, MonoTlsKey key)
755 {
756         return ((MonoThreadInfo*)info)->tls [key];
757 }
758
759 /*
760  * mono_threads_info_tls_set:
761  *
762  *   Set the TLS key to VALUE in the info structure. This can be used to obtain
763  * values of TLS variables for threads other than the current thread.
764  * This should only be used for infrequently changing TLS variables, and it should
765  * be paired with setting the real TLS variable since this provides no GC tracking.
766  */
767 void
768 mono_thread_info_tls_set (THREAD_INFO_TYPE *info, MonoTlsKey key, gpointer value)
769 {
770         ((MonoThreadInfo*)info)->tls [key] = value;
771 }
772
773 /*
774  * mono_thread_info_exit:
775  *
776  *   Exit the current thread.
777  * This function doesn't return.
778  */
779 void
780 mono_thread_info_exit (void)
781 {
782         mono_threads_core_exit (0);
783 }
784
785 /*
786  * mono_thread_info_open_handle:
787  *
788  *   Return a io-layer/win32 handle for the current thread.
789  * The handle need to be closed by calling CloseHandle () when it is no
790  * longer needed.
791  */
792 HANDLE
793 mono_thread_info_open_handle (void)
794 {
795         return mono_threads_core_open_handle ();
796 }
797
798 /*
799  * mono_thread_info_open_handle:
800  *
801  *   Return a io-layer/win32 handle for the thread identified by HANDLE/TID.
802  * The handle need to be closed by calling CloseHandle () when it is no
803  * longer needed.
804  */
805 HANDLE
806 mono_threads_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
807 {
808         return mono_threads_core_open_thread_handle (handle, tid);
809 }
810
811 void
812 mono_thread_info_set_name (MonoNativeThreadId tid, const char *name)
813 {
814         mono_threads_core_set_name (tid, name);
815 }