Merge pull request #980 from StephenMcConnel/bug-18638
[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
24 void
25 mono_threads_init_platform (void)
26 {
27         mono_threads_init_dead_letter ();
28 }
29
30 void
31 mono_threads_core_interrupt (MonoThreadInfo *info)
32 {
33         thread_abort (info->native_handle);
34 }
35
36 void
37 mono_threads_core_abort_syscall (MonoThreadInfo *info)
38 {
39 }
40
41 gboolean
42 mono_threads_core_needs_abort_syscall (void)
43 {
44         return FALSE;
45 }
46
47 gboolean
48 mono_threads_core_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
49 {
50         kern_return_t ret;
51         gboolean res;
52
53         g_assert (info);
54
55         ret = thread_suspend (info->native_handle);
56         if (ret != KERN_SUCCESS)
57                 return FALSE;
58         res = mono_threads_get_runtime_callbacks ()->
59                 thread_state_init_from_handle (&info->suspend_state, info);
60         if (!res)
61                 thread_resume (info->native_handle);
62         return res;
63 }
64
65 gboolean
66 mono_threads_core_resume (MonoThreadInfo *info)
67 {
68         kern_return_t ret;
69
70         if (info->async_target) {
71                 MonoContext tmp = info->suspend_state.ctx;
72                 mach_msg_type_number_t num_state;
73                 thread_state_t state;
74                 ucontext_t uctx;
75                 mcontext_t mctx;
76
77                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
78                 info->async_target = info->user_data = NULL;
79
80                 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
81                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
82
83                 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
84                 if (ret != KERN_SUCCESS)
85                         return FALSE;
86
87                 mono_mach_arch_thread_state_to_mcontext (state, mctx);
88 #ifdef TARGET_ARM64
89                 g_assert_not_reached ();
90 #else
91                 uctx.uc_mcontext = mctx;
92 #endif
93                 mono_monoctx_to_sigctx (&tmp, &uctx);
94
95                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
96
97                 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
98                 if (ret != KERN_SUCCESS)
99                         return FALSE;
100         }
101
102
103         ret = thread_resume (info->native_handle);
104         return ret == KERN_SUCCESS;
105 }
106
107 void
108 mono_threads_platform_register (MonoThreadInfo *info)
109 {
110         info->native_handle = mach_thread_self ();
111         mono_threads_install_dead_letter ();
112 }
113
114 void
115 mono_threads_platform_free (MonoThreadInfo *info)
116 {
117         mach_port_deallocate (current_task (), info->native_handle);
118 }
119
120 MonoNativeThreadId
121 mono_native_thread_id_get (void)
122 {
123         return pthread_self ();
124 }
125
126 gboolean
127 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
128 {
129         return pthread_equal (id1, id2);
130 }
131
132 /*
133  * mono_native_thread_create:
134  *
135  *   Low level thread creation function without any GC wrappers.
136  */
137 gboolean
138 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
139 {
140         return pthread_create (tid, NULL, func, arg) == 0;
141 }
142
143 void
144 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
145 {
146         /* pthread_setnmae_np() on Mac is not documented and doesn't receive thread id. */
147 }
148
149 void
150 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
151 {
152         *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
153         *stsize = pthread_get_stacksize_np (pthread_self());
154
155 #ifdef TARGET_OSX
156         /*
157          * Mavericks reports stack sizes as 512kb:
158          * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
159          * https://bugs.openjdk.java.net/browse/JDK-8020753
160          */
161         if (pthread_main_np () && *stsize == 512 * 1024)
162                 *stsize = 2048 * mono_pagesize ();
163 #endif
164
165         /* staddr points to the start of the stack, not the end */
166         *staddr -= *stsize;
167 }
168
169 #endif