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