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