2 * mono-threads-mach.c: Low-level threading, mach version
5 * Rodrigo Kumpera (kumpera@gmail.com)
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>
27 mono_threads_init_platform (void)
32 mono_threads_core_interrupt (MonoThreadInfo *info)
34 thread_abort_safely (info->native_handle);
38 mono_threads_core_self_suspend (MonoThreadInfo *info)
40 kern_return_t kern_ret;
45 ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&info->suspend_state, NULL);
48 /* we must unlock only after context is captured. */
49 LeaveCriticalSection (&info->suspend_lock);
51 kern_ret = thread_suspend (info->native_handle);
52 g_assert (kern_ret == KERN_SUCCESS);
56 mono_threads_core_suspend (MonoThreadInfo *info)
61 ret = thread_suspend (info->native_handle);
62 if (ret != KERN_SUCCESS)
64 return mono_threads_get_runtime_callbacks ()->
65 thread_state_init_from_handle (&info->suspend_state, mono_thread_info_get_tid (info), info->native_handle);
69 mono_threads_core_resume (MonoThreadInfo *info)
73 if (info->async_target) {
74 MonoContext tmp = info->suspend_state.ctx;
75 mach_msg_type_number_t num_state;
80 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
81 info->async_target = info->user_data = NULL;
83 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
84 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
86 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
87 if (ret != KERN_SUCCESS)
90 mono_mach_arch_thread_state_to_mcontext (state, mctx);
91 uctx.uc_mcontext = mctx;
92 mono_monoctx_to_sigctx (&tmp, &uctx);
94 mono_mach_arch_mcontext_to_thread_state (mctx, state);
96 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
97 if (ret != KERN_SUCCESS)
102 ret = thread_resume (info->native_handle);
103 return ret == KERN_SUCCESS;
107 mono_threads_platform_register (MonoThreadInfo *info)
109 info->native_handle = mach_thread_self ();
113 mono_threads_platform_free (MonoThreadInfo *info)
115 mach_port_deallocate (current_task (), info->native_handle);