[threading] Rework suspend code to be based on a state machine and eliminate known...
[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_interrupt (MonoThreadInfo *info)
36 {
37         thread_abort (info->native_handle);
38 }
39
40 void
41 mono_threads_core_abort_syscall (MonoThreadInfo *info)
42 {
43         thread_suspend (info->native_handle);
44         thread_abort_safely (info->native_handle);
45         thread_resume (info->native_handle);
46 }
47
48 gboolean
49 mono_threads_core_needs_abort_syscall (void)
50 {
51         return TRUE;
52 }
53
54 gboolean
55 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
56 {
57         kern_return_t ret;
58         gboolean res;
59
60         g_assert (info);
61
62         ret = thread_suspend (info->native_handle);
63         THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (void*)info->native_handle, ret);
64         if (ret != KERN_SUCCESS)
65                 return FALSE;
66
67         /* We're in the middle of a self-suspend, resume and register */
68         if (!mono_threads_transition_finish_async_suspend (info)) {
69                 mono_threads_add_to_pending_operation_set (info);
70                 g_assert (thread_resume (info->native_handle) == KERN_SUCCESS);
71                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/1 %p -> %d\n", (void*)info->native_handle, 0);
72                 //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall
73                 return TRUE;
74         }
75         res = mono_threads_get_runtime_callbacks ()->
76                 thread_state_init_from_handle (&info->suspend_state, info);
77         THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (void*)info->native_handle, res);
78         if (res) {
79                 if (interrupt_kernel)
80                         thread_abort (info->native_handle);
81         } else {
82                 mono_threads_transition_async_suspend_compensation (info);
83                 g_assert (thread_resume (info->native_handle) == KERN_SUCCESS);
84                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (void*)info->native_handle, 0);
85         }
86         return res;
87 }
88
89 gboolean
90 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
91 {
92         return TRUE;
93 }
94
95 gboolean
96 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
97 {
98         kern_return_t ret;
99
100         if (info->async_target) {
101                 MonoContext tmp = info->suspend_state.ctx;
102                 mach_msg_type_number_t num_state;
103                 thread_state_t state;
104                 ucontext_t uctx;
105                 mcontext_t mctx;
106
107                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
108                 info->async_target = info->user_data = NULL;
109
110                 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
111                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
112
113                 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
114                 if (ret != KERN_SUCCESS)
115                         return FALSE;
116
117                 mono_mach_arch_thread_state_to_mcontext (state, mctx);
118 #ifdef TARGET_ARM64
119                 g_assert_not_reached ();
120 #else
121                 uctx.uc_mcontext = mctx;
122 #endif
123                 mono_monoctx_to_sigctx (&tmp, &uctx);
124
125                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
126
127                 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
128                 if (ret != KERN_SUCCESS)
129                         return FALSE;
130         }
131
132         ret = thread_resume (info->native_handle);
133         THREADS_SUSPEND_DEBUG ("RESUME %p -> %d\n", (void*)info->native_handle, ret);
134
135         return ret == KERN_SUCCESS;
136 }
137
138 void
139 mono_threads_platform_register (MonoThreadInfo *info)
140 {
141         info->native_handle = mach_thread_self ();
142         mono_threads_install_dead_letter ();
143 }
144
145 void
146 mono_threads_platform_free (MonoThreadInfo *info)
147 {
148         mach_port_deallocate (current_task (), info->native_handle);
149 }
150
151 MonoNativeThreadId
152 mono_native_thread_id_get (void)
153 {
154         return pthread_self ();
155 }
156
157 gboolean
158 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
159 {
160         return pthread_equal (id1, id2);
161 }
162
163 /*
164  * mono_native_thread_create:
165  *
166  *   Low level thread creation function without any GC wrappers.
167  */
168 gboolean
169 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
170 {
171         return pthread_create (tid, NULL, func, arg) == 0;
172 }
173
174 void
175 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
176 {
177         /* pthread_setnmae_np() on Mac is not documented and doesn't receive thread id. */
178 }
179 #endif /* USE_MACH_BACKEND */
180
181 #ifdef __MACH__
182 void
183 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
184 {
185         *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
186         *stsize = pthread_get_stacksize_np (pthread_self());
187
188 #ifdef TARGET_OSX
189         /*
190          * Mavericks reports stack sizes as 512kb:
191          * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
192          * https://bugs.openjdk.java.net/browse/JDK-8020753
193          */
194         if (pthread_main_np () && *stsize == 512 * 1024)
195                 *stsize = 2048 * mono_pagesize ();
196 #endif
197
198         /* staddr points to the start of the stack, not the end */
199         *staddr -= *stsize;
200 }
201
202 #endif