[mono-threads] Split platform and suspend specific functions
[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
27 void
28 mono_threads_suspend_init (void)
29 {
30         mono_threads_init_dead_letter ();
31 }
32
33 #if defined(HOST_WATCHOS) || defined(HOST_TVOS)
34
35 gboolean
36 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
37 {
38         g_assert_not_reached ();
39 }
40
41 gboolean
42 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
43 {
44         g_assert_not_reached ();
45 }
46
47 gboolean
48 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
49 {
50         g_assert_not_reached ();
51 }
52
53 #else /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
54
55 gboolean
56 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
57 {
58         kern_return_t ret;
59
60         g_assert (info);
61
62
63         do {
64                 ret = thread_suspend (info->native_handle);
65         } while (ret == KERN_ABORTED);
66
67         THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
68         if (ret != KERN_SUCCESS)
69                 return FALSE;
70
71         /* We're in the middle of a self-suspend, resume and register */
72         if (!mono_threads_transition_finish_async_suspend (info)) {
73                 mono_threads_add_to_pending_operation_set (info);
74                 do {
75                         ret = thread_resume (info->native_handle);
76                 } while (ret == KERN_ABORTED);
77                 g_assert (ret == KERN_SUCCESS);
78                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/1 %p -> %d\n", (gpointer)(gsize)info->native_handle, 0);
79                 //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall
80                 return TRUE;
81         }
82         info->suspend_can_continue = mono_threads_get_runtime_callbacks ()->
83                 thread_state_init_from_handle (&info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], info);
84         THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (gpointer)(gsize)info->native_handle, res);
85         if (info->suspend_can_continue) {
86                 if (interrupt_kernel)
87                         thread_abort (info->native_handle);
88         } else {
89                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (gpointer)(gsize)info->native_handle, 0);
90         }
91         return info->suspend_can_continue;
92 }
93
94 gboolean
95 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
96 {
97         return info->suspend_can_continue;
98 }
99
100 gboolean
101 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
102 {
103         kern_return_t ret;
104
105         if (info->async_target) {
106                 MonoContext tmp = info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx;
107                 mach_msg_type_number_t num_state;
108                 thread_state_t state;
109                 ucontext_t uctx;
110                 mcontext_t mctx;
111
112                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
113                 info->user_data = NULL;
114                 info->async_target = (void (*)(void *)) info->user_data;
115
116                 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
117                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
118
119                 do {
120                         ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
121                 } while (ret == KERN_ABORTED);
122
123                 if (ret != KERN_SUCCESS)
124                         return FALSE;
125
126                 mono_mach_arch_thread_state_to_mcontext (state, mctx);
127                 uctx.uc_mcontext = mctx;
128                 mono_monoctx_to_sigctx (&tmp, &uctx);
129
130                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
131
132                 do {
133                         ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
134                 } while (ret == KERN_ABORTED);
135
136                 if (ret != KERN_SUCCESS)
137                         return FALSE;
138         }
139
140         do {
141                 ret = thread_resume (info->native_handle);
142         } while (ret == KERN_ABORTED);
143         THREADS_SUSPEND_DEBUG ("RESUME %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
144
145         return ret == KERN_SUCCESS;
146 }
147
148 #endif /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
149
150 void
151 mono_threads_suspend_register (MonoThreadInfo *info)
152 {
153         char thread_name [64];
154
155         info->native_handle = mach_thread_self ();
156
157         snprintf (thread_name, sizeof (thread_name), "tid_%x", (int) info->native_handle);
158         pthread_setname_np (thread_name);
159
160         mono_threads_install_dead_letter ();
161 }
162
163 void
164 mono_threads_suspend_free (MonoThreadInfo *info)
165 {
166         mach_port_deallocate (current_task (), info->native_handle);
167 }
168
169 #endif /* USE_MACH_BACKEND */
170
171 #ifdef __MACH__
172 void
173 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
174 {
175         *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
176         *stsize = pthread_get_stacksize_np (pthread_self());
177
178 #ifdef TARGET_OSX
179         /*
180          * Mavericks reports stack sizes as 512kb:
181          * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
182          * https://bugs.openjdk.java.net/browse/JDK-8020753
183          */
184         if (pthread_main_np () && *stsize == 512 * 1024)
185                 *stsize = 2048 * mono_pagesize ();
186 #endif
187
188         /* staddr points to the start of the stack, not the end */
189         *staddr -= *stsize;
190 }
191
192 #endif