[amd64] Remove the callee saved registers from MonoLMF, save/restore them normally...
[mono-project.git] / mono / utils / mono-threads-mach.c
blobf4ad2637e57e51c2f04688c586e67870e05041c4
1 /*
2 * mono-threads-mach.c: Low-level threading, mach version
4 * Author:
5 * Rodrigo Kumpera (kumpera@gmail.com)
7 * (C) 2011 Novell, Inc
8 */
10 #include "config.h"
12 #if defined(__MACH__)
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>
23 #include <pthread.h>
24 #include <errno.h>
26 void
27 mono_threads_init_platform (void)
29 mono_threads_init_dead_letter ();
32 void
33 mono_threads_core_interrupt (MonoThreadInfo *info)
35 thread_abort (info->native_handle);
38 void
39 mono_threads_core_abort_syscall (MonoThreadInfo *info)
43 gboolean
44 mono_threads_core_needs_abort_syscall (void)
46 return FALSE;
49 gboolean
50 mono_threads_core_suspend (MonoThreadInfo *info)
52 kern_return_t ret;
53 gboolean res;
55 g_assert (info);
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, info);
62 if (!res)
63 thread_resume (info->native_handle);
64 return res;
67 gboolean
68 mono_threads_core_resume (MonoThreadInfo *info)
70 kern_return_t ret;
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;
79 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
80 info->async_target = info->user_data = NULL;
82 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
83 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
85 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
86 if (ret != KERN_SUCCESS)
87 return FALSE;
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);
97 mono_mach_arch_mcontext_to_thread_state (mctx, state);
99 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
100 if (ret != KERN_SUCCESS)
101 return FALSE;
105 ret = thread_resume (info->native_handle);
106 return ret == KERN_SUCCESS;
109 void
110 mono_threads_platform_register (MonoThreadInfo *info)
112 info->native_handle = mach_thread_self ();
113 mono_threads_install_dead_letter ();
116 void
117 mono_threads_platform_free (MonoThreadInfo *info)
119 mach_port_deallocate (current_task (), info->native_handle);
122 MonoNativeThreadId
123 mono_native_thread_id_get (void)
125 return pthread_self ();
128 gboolean
129 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
131 return pthread_equal (id1, id2);
135 * mono_native_thread_create:
137 * Low level thread creation function without any GC wrappers.
139 gboolean
140 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
142 return pthread_create (tid, NULL, func, arg) == 0;
145 void
146 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
148 /* pthread_setnmae_np() on Mac is not documented and doesn't receive thread id. */
151 #endif