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