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