Revert "Revert "Merge branch 'master' of https://github.com/mono/mono""
[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 #if defined(__MACH__)
13
14 #include <mono/utils/mach-support.h>
15 #include <mono/utils/mono-compiler.h>
16 #include <mono/utils/mono-semaphore.h>
17 #include <mono/utils/mono-threads.h>
18 #include <mono/utils/hazard-pointer.h>
19 #include <mono/metadata/gc-internal.h>
20 #include <mono/metadata/appdomain.h>
21 #include <mono/metadata/threads-types.h>
22
23 #include <pthread.h>
24 #include <errno.h>
25
26 void
27 mono_threads_init_platform (void)
28 {
29         mono_threads_init_dead_letter ();
30 }
31
32 void
33 mono_threads_core_interrupt (MonoThreadInfo *info)
34 {
35         thread_abort (info->native_handle);
36 }
37
38 void
39 mono_threads_core_abort_syscall (MonoThreadInfo *info)
40 {
41 }
42
43 gboolean
44 mono_threads_core_needs_abort_syscall (void)
45 {
46         return FALSE;
47 }
48
49 gboolean
50 mono_threads_core_suspend (MonoThreadInfo *info)
51 {
52         kern_return_t ret;
53         gboolean res;
54
55         g_assert (info);
56
57         ret = thread_suspend (info->native_handle);
58         if (ret != KERN_SUCCESS)
59                 return FALSE;
60         res = mono_threads_get_runtime_callbacks ()->
61                 thread_state_init_from_handle (&info->suspend_state, mono_thread_info_get_tid (info), info->native_handle);
62         if (!res)
63                 thread_resume (info->native_handle);
64         return res;
65 }
66
67 gboolean
68 mono_threads_core_resume (MonoThreadInfo *info)
69 {
70         kern_return_t ret;
71
72         if (info->async_target) {
73                 MonoContext tmp = info->suspend_state.ctx;
74                 mach_msg_type_number_t num_state;
75                 thread_state_t state;
76                 ucontext_t uctx;
77                 mcontext_t mctx;
78
79                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
80                 info->async_target = info->user_data = NULL;
81
82                 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
83                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
84
85                 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
86                 if (ret != KERN_SUCCESS)
87                         return FALSE;
88
89                 mono_mach_arch_thread_state_to_mcontext (state, mctx);
90 #ifdef TARGET_ARM64
91                 g_assert_not_reached ();
92 #else
93                 uctx.uc_mcontext = mctx;
94 #endif
95                 mono_monoctx_to_sigctx (&tmp, &uctx);
96
97                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
98
99                 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
100                 if (ret != KERN_SUCCESS)
101                         return FALSE;
102         }
103
104
105         ret = thread_resume (info->native_handle);
106         return ret == KERN_SUCCESS;
107 }
108
109 void
110 mono_threads_platform_register (MonoThreadInfo *info)
111 {
112         info->native_handle = mach_thread_self ();
113         mono_threads_install_dead_letter ();
114 }
115
116 void
117 mono_threads_platform_free (MonoThreadInfo *info)
118 {
119         mach_port_deallocate (current_task (), info->native_handle);
120 }
121
122 MonoNativeThreadId
123 mono_native_thread_id_get (void)
124 {
125         return pthread_self ();
126 }
127
128 gboolean
129 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
130 {
131         return pthread_equal (id1, id2);
132 }
133
134 /*
135  * mono_native_thread_create:
136  *
137  *   Low level thread creation function without any GC wrappers.
138  */
139 gboolean
140 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
141 {
142         return pthread_create (tid, NULL, func, arg) == 0;
143 }
144
145 void
146 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
147 {
148         /* pthread_setnmae_np() on Mac is not documented and doesn't receive thread id. */
149 }
150
151 #endif