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