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