[runtime] Fix DISABLE_REFLECTION_EMIT build.
[mono.git] / mono / utils / mono-threads-mach.c
1 /**
2  * \file
3  * Low-level threading, mach version
4  *
5  * Author:
6  *      Rodrigo Kumpera (kumpera@gmail.com)
7  *
8  * (C) 2011 Novell, Inc
9  */
10
11 #include "config.h"
12
13 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
14 #if defined (__MACH__)
15 #define _DARWIN_C_SOURCE 1
16 #endif
17
18 #include <mono/utils/mono-threads.h>
19 #include <mono/utils/mono-mmap.h>
20
21 #if defined (USE_MACH_BACKEND)
22
23 #include <mono/utils/mach-support.h>
24 #include <mono/utils/mono-compiler.h>
25 #include <mono/utils/mono-threads.h>
26 #include <mono/utils/hazard-pointer.h>
27 #include <mono/utils/mono-threads-debug.h>
28
29 void
30 mono_threads_suspend_init (void)
31 {
32         mono_threads_init_dead_letter ();
33 }
34
35 #if defined(HOST_WATCHOS) || defined(HOST_TVOS)
36
37 gboolean
38 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
39 {
40         g_assert_not_reached ();
41 }
42
43 gboolean
44 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
45 {
46         g_assert_not_reached ();
47 }
48
49 gboolean
50 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
51 {
52         g_assert_not_reached ();
53 }
54
55 void
56 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
57 {
58 }
59
60 #else /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
61
62 gboolean
63 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
64 {
65         kern_return_t ret;
66
67         g_assert (info);
68
69
70         do {
71                 ret = thread_suspend (info->native_handle);
72         } while (ret == KERN_ABORTED);
73
74         THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
75         if (ret != KERN_SUCCESS)
76                 return FALSE;
77
78         /* We're in the middle of a self-suspend, resume and register */
79         if (!mono_threads_transition_finish_async_suspend (info)) {
80                 mono_threads_add_to_pending_operation_set (info);
81                 do {
82                         ret = thread_resume (info->native_handle);
83                 } while (ret == KERN_ABORTED);
84                 g_assert (ret == KERN_SUCCESS);
85                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/1 %p -> %d\n", (gpointer)(gsize)info->native_handle, 0);
86                 //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall
87                 return TRUE;
88         }
89         info->suspend_can_continue = mono_threads_get_runtime_callbacks ()->
90                 thread_state_init_from_handle (&info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], info);
91         THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
92         if (info->suspend_can_continue) {
93                 if (interrupt_kernel)
94                         thread_abort (info->native_handle);
95         } else {
96                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (gpointer)(gsize)info->native_handle, 0);
97         }
98         return TRUE;
99 }
100
101 gboolean
102 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
103 {
104         return info->suspend_can_continue;
105 }
106
107 gboolean
108 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
109 {
110         kern_return_t ret;
111
112         if (info->async_target) {
113                 MonoContext tmp = info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx;
114                 mach_msg_type_number_t num_state, num_fpstate;
115                 thread_state_t state, fpstate;
116                 ucontext_t uctx;
117                 mcontext_t mctx;
118
119                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
120                 info->user_data = NULL;
121                 info->async_target = (void (*)(void *)) info->user_data;
122
123                 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
124                 fpstate = (thread_state_t) alloca (mono_mach_arch_get_thread_fpstate_size ());
125                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
126
127                 do {
128                         ret = mono_mach_arch_get_thread_states (info->native_handle, state, &num_state, fpstate, &num_fpstate);
129                 } while (ret == KERN_ABORTED);
130
131                 if (ret != KERN_SUCCESS)
132                         return FALSE;
133
134                 mono_mach_arch_thread_states_to_mcontext (state, fpstate, mctx);
135                 uctx.uc_mcontext = mctx;
136                 mono_monoctx_to_sigctx (&tmp, &uctx);
137
138                 mono_mach_arch_mcontext_to_thread_states (mctx, state, fpstate);
139
140                 do {
141                         ret = mono_mach_arch_set_thread_states (info->native_handle, state, num_state, fpstate, num_fpstate);
142                 } while (ret == KERN_ABORTED);
143
144                 if (ret != KERN_SUCCESS)
145                         return FALSE;
146         }
147
148         do {
149                 ret = thread_resume (info->native_handle);
150         } while (ret == KERN_ABORTED);
151         THREADS_SUSPEND_DEBUG ("RESUME %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
152
153         return ret == KERN_SUCCESS;
154 }
155
156 void
157 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
158 {
159         kern_return_t ret;
160
161         do {
162                 ret = thread_suspend (info->native_handle);
163         } while (ret == KERN_ABORTED);
164
165         if (ret != KERN_SUCCESS)
166                 return;
167
168         do {
169                 ret = thread_abort_safely (info->native_handle);
170         } while (ret == KERN_ABORTED);
171
172         /*
173          * We are doing thread_abort when thread_abort_safely returns KERN_SUCCESS because
174          * for some reason accept is not interrupted by thread_abort_safely.
175          * The risk of aborting non-atomic operations while calling thread_abort should not
176          * exist because by the time thread_abort_safely returns KERN_SUCCESS the target
177          * thread should have return from the kernel and should be waiting for thread_resume
178          * to resume the user code.
179          */
180         if (ret == KERN_SUCCESS)
181                 ret = thread_abort (info->native_handle);
182
183         do {
184                 ret = thread_resume (info->native_handle);
185         } while (ret == KERN_ABORTED);
186
187         g_assert (ret == KERN_SUCCESS);
188 }
189
190 #endif /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
191
192 void
193 mono_threads_suspend_register (MonoThreadInfo *info)
194 {
195         char thread_name [64];
196
197         info->native_handle = mach_thread_self ();
198
199         snprintf (thread_name, sizeof (thread_name), "tid_%x", (int) info->native_handle);
200         pthread_setname_np (thread_name);
201
202         mono_threads_install_dead_letter ();
203 }
204
205 void
206 mono_threads_suspend_free (MonoThreadInfo *info)
207 {
208         mach_port_deallocate (current_task (), info->native_handle);
209 }
210
211 void
212 mono_threads_suspend_init_signals (void)
213 {
214 }
215
216 gint
217 mono_threads_suspend_search_alternative_signal (void)
218 {
219         g_assert_not_reached ();
220 }
221
222 gint
223 mono_threads_suspend_get_suspend_signal (void)
224 {
225         return -1;
226 }
227
228 gint
229 mono_threads_suspend_get_restart_signal (void)
230 {
231         return -1;
232 }
233
234 gint
235 mono_threads_suspend_get_abort_signal (void)
236 {
237         return -1;
238 }
239
240 #endif /* USE_MACH_BACKEND */
241
242 #ifdef __MACH__
243 void
244 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
245 {
246         *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
247         *stsize = pthread_get_stacksize_np (pthread_self());
248
249 #ifdef TARGET_OSX
250         /*
251          * Mavericks reports stack sizes as 512kb:
252          * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
253          * https://bugs.openjdk.java.net/browse/JDK-8020753
254          */
255         if (pthread_main_np () && *stsize == 512 * 1024)
256                 *stsize = 2048 * mono_pagesize ();
257 #endif
258
259         /* staddr points to the start of the stack, not the end */
260         *staddr -= *stsize;
261 }
262
263 #endif