Merge pull request #2003 from esdrubal/seq_test_fix2
[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-semaphore.h>
25 #include <mono/utils/mono-threads.h>
26 #include <mono/utils/hazard-pointer.h>
27
28 void
29 mono_threads_init_platform (void)
30 {
31         mono_threads_init_dead_letter ();
32 }
33
34 void
35 mono_threads_core_abort_syscall (MonoThreadInfo *info)
36 {
37         kern_return_t ret;
38
39         ret = thread_suspend (info->native_handle);
40         if (ret != KERN_SUCCESS)
41                 return;
42
43         ret = thread_abort_safely (info->native_handle);
44
45         /*
46          * We are doing thread_abort when thread_abort_safely returns KERN_SUCCESS because
47          * for some reason accept is not interrupted by thread_abort_safely.
48          * The risk of aborting non-atomic operations while calling thread_abort should not
49          * exist because by the time thread_abort_safely returns KERN_SUCCESS the target
50          * thread should have return from the kernel and should be waiting for thread_resume
51          * to resume the user code.
52          */
53         if (ret == KERN_SUCCESS)
54                 ret = thread_abort (info->native_handle);
55
56         g_assert (thread_resume (info->native_handle) == KERN_SUCCESS);
57 }
58
59 gboolean
60 mono_threads_core_needs_abort_syscall (void)
61 {
62         return TRUE;
63 }
64
65 gboolean
66 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
67 {
68         kern_return_t ret;
69         gboolean res;
70
71         g_assert (info);
72
73         ret = thread_suspend (info->native_handle);
74         THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (void*)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                 g_assert (thread_resume (info->native_handle) == KERN_SUCCESS);
82                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/1 %p -> %d\n", (void*)info->native_handle, 0);
83                 //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall
84                 return TRUE;
85         }
86         res = mono_threads_get_runtime_callbacks ()->
87                 thread_state_init_from_handle (&info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], info);
88         THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (void*)info->native_handle, res);
89         if (res) {
90                 if (interrupt_kernel)
91                         thread_abort (info->native_handle);
92         } else {
93                 mono_threads_transition_async_suspend_compensation (info);
94                 g_assert (thread_resume (info->native_handle) == KERN_SUCCESS);
95                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (void*)info->native_handle, 0);
96         }
97         return res;
98 }
99
100 gboolean
101 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
102 {
103         return TRUE;
104 }
105
106 gboolean
107 mono_threads_core_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;
114                 thread_state_t state;
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                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
124
125                 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
126                 if (ret != KERN_SUCCESS)
127                         return FALSE;
128
129                 mono_mach_arch_thread_state_to_mcontext (state, mctx);
130 #ifdef TARGET_ARM64
131                 g_assert_not_reached ();
132 #else
133                 uctx.uc_mcontext = mctx;
134 #endif
135                 mono_monoctx_to_sigctx (&tmp, &uctx);
136
137                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
138
139                 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
140                 if (ret != KERN_SUCCESS)
141                         return FALSE;
142         }
143
144         ret = thread_resume (info->native_handle);
145         THREADS_SUSPEND_DEBUG ("RESUME %p -> %d\n", (void*)info->native_handle, ret);
146
147         return ret == KERN_SUCCESS;
148 }
149
150 void
151 mono_threads_platform_register (MonoThreadInfo *info)
152 {
153         info->native_handle = mach_thread_self ();
154         mono_threads_install_dead_letter ();
155 }
156
157 void
158 mono_threads_platform_free (MonoThreadInfo *info)
159 {
160         mach_port_deallocate (current_task (), info->native_handle);
161 }
162
163 void
164 mono_threads_core_begin_global_suspend (void)
165 {
166 }
167
168 void
169 mono_threads_core_end_global_suspend (void)
170 {
171 }
172
173 #endif /* USE_MACH_BACKEND */
174
175 #ifdef __MACH__
176 void
177 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
178 {
179         *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
180         *stsize = pthread_get_stacksize_np (pthread_self());
181
182 #ifdef TARGET_OSX
183         /*
184          * Mavericks reports stack sizes as 512kb:
185          * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
186          * https://bugs.openjdk.java.net/browse/JDK-8020753
187          */
188         if (pthread_main_np () && *stsize == 512 * 1024)
189                 *stsize = 2048 * mono_pagesize ();
190 #endif
191
192         /* staddr points to the start of the stack, not the end */
193         *staddr -= *stsize;
194 }
195
196 #endif