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