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)
29 mono_threads_init_dead_letter ();
33 mono_threads_core_interrupt (MonoThreadInfo
*info
)
35 thread_abort (info
->native_handle
);
39 mono_threads_core_abort_syscall (MonoThreadInfo
*info
)
44 mono_threads_core_needs_abort_syscall (void)
50 mono_threads_core_suspend (MonoThreadInfo
*info
)
57 ret
= thread_suspend (info
->native_handle
);
58 if (ret
!= KERN_SUCCESS
)
60 res
= mono_threads_get_runtime_callbacks ()->
61 thread_state_init_from_handle (&info
->suspend_state
, info
);
63 thread_resume (info
->native_handle
);
68 mono_threads_core_resume (MonoThreadInfo
*info
)
72 if (info
->async_target
) {
73 MonoContext tmp
= info
->suspend_state
.ctx
;
74 mach_msg_type_number_t num_state
;
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
)
89 mono_mach_arch_thread_state_to_mcontext (state
, mctx
);
91 g_assert_not_reached ();
93 uctx
.uc_mcontext
= mctx
;
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
)
105 ret
= thread_resume (info
->native_handle
);
106 return ret
== KERN_SUCCESS
;
110 mono_threads_platform_register (MonoThreadInfo
*info
)
112 info
->native_handle
= mach_thread_self ();
113 mono_threads_install_dead_letter ();
117 mono_threads_platform_free (MonoThreadInfo
*info
)
119 mach_port_deallocate (current_task (), info
->native_handle
);
123 mono_native_thread_id_get (void)
125 return pthread_self ();
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.
140 mono_native_thread_create (MonoNativeThreadId
*tid
, gpointer func
, gpointer arg
)
142 return pthread_create (tid
, NULL
, func
, arg
) == 0;
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. */