6270c57f1d6c347e300e3da7d0988ba5326eb91b
[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 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
15 #define _DARWIN_C_SOURCE 1
16
17 #include <mono/utils/mach-support.h>
18 #include <mono/utils/mono-compiler.h>
19 #include <mono/utils/mono-semaphore.h>
20 #include <mono/utils/mono-threads.h>
21 #include <mono/utils/hazard-pointer.h>
22 #include <mono/utils/mono-mmap.h>
23 #include <mono/metadata/gc-internal.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/threads-types.h>
26
27 void
28 mono_threads_init_platform (void)
29 {
30         mono_threads_init_dead_letter ();
31 }
32
33 void
34 mono_threads_core_interrupt (MonoThreadInfo *info)
35 {
36         thread_abort (info->native_handle);
37 }
38
39 void
40 mono_threads_core_abort_syscall (MonoThreadInfo *info)
41 {
42 }
43
44 gboolean
45 mono_threads_core_needs_abort_syscall (void)
46 {
47         return FALSE;
48 }
49
50 gboolean
51 mono_threads_core_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
52 {
53         kern_return_t ret;
54         gboolean res;
55
56         g_assert (info);
57
58         ret = thread_suspend (info->native_handle);
59         if (ret != KERN_SUCCESS)
60                 return FALSE;
61         res = mono_threads_get_runtime_callbacks ()->
62                 thread_state_init_from_handle (&info->suspend_state, info);
63         if (!res)
64                 thread_resume (info->native_handle);
65         return res;
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 #ifdef TARGET_ARM64
92                 g_assert_not_reached ();
93 #else
94                 uctx.uc_mcontext = mctx;
95 #endif
96                 mono_monoctx_to_sigctx (&tmp, &uctx);
97
98                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
99
100                 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
101                 if (ret != KERN_SUCCESS)
102                         return FALSE;
103         }
104
105
106         ret = thread_resume (info->native_handle);
107         return ret == KERN_SUCCESS;
108 }
109
110 void
111 mono_threads_platform_register (MonoThreadInfo *info)
112 {
113         info->native_handle = mach_thread_self ();
114         mono_threads_install_dead_letter ();
115 }
116
117 void
118 mono_threads_platform_free (MonoThreadInfo *info)
119 {
120         mach_port_deallocate (current_task (), info->native_handle);
121 }
122
123 MonoNativeThreadId
124 mono_native_thread_id_get (void)
125 {
126         return pthread_self ();
127 }
128
129 gboolean
130 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
131 {
132         return pthread_equal (id1, id2);
133 }
134
135 /*
136  * mono_native_thread_create:
137  *
138  *   Low level thread creation function without any GC wrappers.
139  */
140 gboolean
141 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
142 {
143         return pthread_create (tid, NULL, func, arg) == 0;
144 }
145
146 void
147 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
148 {
149         /* pthread_setnmae_np() on Mac is not documented and doesn't receive thread id. */
150 }
151
152 void
153 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
154 {
155         *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
156         *stsize = pthread_get_stacksize_np (pthread_self());
157
158 #ifdef TARGET_OSX
159         /*
160          * Mavericks reports stack sizes as 512kb:
161          * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
162          * https://bugs.openjdk.java.net/browse/JDK-8020753
163          */
164         if (pthread_main_np () && *stsize == 512 * 1024)
165                 *stsize = 2048 * mono_pagesize ();
166 #endif
167
168         /* staddr points to the start of the stack, not the end */
169         *staddr -= *stsize;
170 }
171
172 #endif