Finish spliting sgen-nursery-allocator into a separate compilation unit
[mono.git] / mono / utils / mono-threads-mach.c
1 /*
2  * mono-threads-mach.c: Low-level threading, mach version
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * (C) 2011 Novell, Inc
8  */
9
10 #include "config.h"
11
12 #if defined(__MACH__)
13
14 #include <mono/utils/mach-support.h>
15 #include <mono/utils/mono-compiler.h>
16 #include <mono/utils/mono-semaphore.h>
17 #include <mono/utils/mono-threads.h>
18 #include <mono/utils/hazard-pointer.h>
19 #include <mono/metadata/gc-internal.h>
20 #include <mono/metadata/appdomain.h>
21 #include <mono/metadata/threads-types.h>
22
23 #include <pthread.h>
24 #include <errno.h>
25
26 void
27 mono_threads_init_platform (void)
28 {       
29 }
30
31 void
32 mono_threads_core_interrupt (MonoThreadInfo *info)
33 {
34         thread_abort_safely (info->native_handle);
35 }
36
37 void
38 mono_threads_core_self_suspend (MonoThreadInfo *info)
39 {
40         kern_return_t kern_ret;
41         gboolean ret;
42
43         g_assert (info);
44
45         ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&info->suspend_state, NULL);
46         g_assert (ret);
47
48         /* we must unlock only after context is captured. */
49         LeaveCriticalSection (&info->suspend_lock);
50
51         kern_ret = thread_suspend (info->native_handle);
52         g_assert (kern_ret == KERN_SUCCESS);
53 }
54
55 gboolean
56 mono_threads_core_suspend (MonoThreadInfo *info)
57 {
58         kern_return_t ret;
59         g_assert (info);
60
61         ret = thread_suspend (info->native_handle);
62         if (ret != KERN_SUCCESS)
63                 return FALSE;
64         return mono_threads_get_runtime_callbacks ()->
65                 thread_state_init_from_handle (&info->suspend_state, mono_thread_info_get_tid (info), info->native_handle);
66 }
67
68 gboolean
69 mono_threads_core_resume (MonoThreadInfo *info)
70 {
71         kern_return_t ret;
72
73         if (info->async_target) {
74                 MonoContext tmp = info->suspend_state.ctx;
75                 mach_msg_type_number_t num_state;
76                 thread_state_t state;
77                 ucontext_t uctx;
78                 mcontext_t mctx;
79
80                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
81                 info->async_target = info->user_data = NULL;
82
83                 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
84                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
85
86                 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
87                 if (ret != KERN_SUCCESS)
88                         return FALSE;
89
90                 mono_mach_arch_thread_state_to_mcontext (state, mctx);
91                 uctx.uc_mcontext = mctx;
92                 mono_monoctx_to_sigctx (&tmp, &uctx);
93
94                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
95
96                 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
97                 if (ret != KERN_SUCCESS)
98                         return FALSE;
99         }
100
101
102         ret = thread_resume (info->native_handle);
103         return ret == KERN_SUCCESS;
104 }
105
106 void
107 mono_threads_platform_register (MonoThreadInfo *info)
108 {
109         info->native_handle = mach_thread_self ();
110 }
111
112 void
113 mono_threads_platform_free (MonoThreadInfo *info)
114 {
115         mach_port_deallocate (current_task (), info->native_handle);
116 }
117
118 #endif