Properly support cross compiling for the mach support code.
[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 }
30
31 void
32 mono_threads_core_interrupt (MonoThreadInfo *info)
33 {
34         thread_abort (info->native_handle);
35 }
36
37 void
38 mono_threads_core_abort_syscall (MonoThreadInfo *info)
39 {
40 }
41
42 gboolean
43 mono_threads_core_needs_abort_syscall (void)
44 {
45         return FALSE;
46 }
47
48 gboolean
49 mono_threads_core_suspend (MonoThreadInfo *info)
50 {
51         kern_return_t ret;
52         g_assert (info);
53
54         ret = thread_suspend (info->native_handle);
55         if (ret != KERN_SUCCESS)
56                 return FALSE;
57         return mono_threads_get_runtime_callbacks ()->
58                 thread_state_init_from_handle (&info->suspend_state, mono_thread_info_get_tid (info), info->native_handle);
59 }
60
61 gboolean
62 mono_threads_core_resume (MonoThreadInfo *info)
63 {
64         kern_return_t ret;
65
66         if (info->async_target) {
67                 MonoContext tmp = info->suspend_state.ctx;
68                 mach_msg_type_number_t num_state;
69                 thread_state_t state;
70                 ucontext_t uctx;
71                 mcontext_t mctx;
72
73                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
74                 info->async_target = info->user_data = NULL;
75
76                 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
77                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
78
79                 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
80                 if (ret != KERN_SUCCESS)
81                         return FALSE;
82
83                 mono_mach_arch_thread_state_to_mcontext (state, mctx);
84                 uctx.uc_mcontext = mctx;
85                 mono_monoctx_to_sigctx (&tmp, &uctx);
86
87                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
88
89                 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
90                 if (ret != KERN_SUCCESS)
91                         return FALSE;
92         }
93
94
95         ret = thread_resume (info->native_handle);
96         return ret == KERN_SUCCESS;
97 }
98
99 void
100 mono_threads_platform_register (MonoThreadInfo *info)
101 {
102         info->native_handle = mach_thread_self ();
103 }
104
105 void
106 mono_threads_platform_free (MonoThreadInfo *info)
107 {
108         mach_port_deallocate (current_task (), info->native_handle);
109 }
110
111 MonoNativeThreadId
112 mono_native_thread_id_get (void)
113 {
114         return pthread_self ();
115 }
116
117 gboolean
118 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
119 {
120         return pthread_equal (id1, id2);
121 }
122
123 /*
124  * mono_native_thread_create:
125  *
126  *   Low level thread creation function without any GC wrappers.
127  */
128 gboolean
129 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
130 {
131         return pthread_create (tid, NULL, func, arg) == 0;
132 }
133
134 #endif