Merge branch 'master' of https://github.com/mono/mono
[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         gboolean res;
53
54         g_assert (info);
55
56         ret = thread_suspend (info->native_handle);
57         if (ret != KERN_SUCCESS)
58                 return FALSE;
59         res = mono_threads_get_runtime_callbacks ()->
60                 thread_state_init_from_handle (&info->suspend_state, mono_thread_info_get_tid (info), info->native_handle);
61         if (!res)
62                 thread_resume (info->native_handle);
63         return res;
64 }
65
66 gboolean
67 mono_threads_core_resume (MonoThreadInfo *info)
68 {
69         kern_return_t ret;
70
71         if (info->async_target) {
72                 MonoContext tmp = info->suspend_state.ctx;
73                 mach_msg_type_number_t num_state;
74                 thread_state_t state;
75                 ucontext_t uctx;
76                 mcontext_t mctx;
77
78                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
79                 info->async_target = info->user_data = NULL;
80
81                 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
82                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
83
84                 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
85                 if (ret != KERN_SUCCESS)
86                         return FALSE;
87
88                 mono_mach_arch_thread_state_to_mcontext (state, mctx);
89                 uctx.uc_mcontext = mctx;
90                 mono_monoctx_to_sigctx (&tmp, &uctx);
91
92                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
93
94                 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
95                 if (ret != KERN_SUCCESS)
96                         return FALSE;
97         }
98
99
100         ret = thread_resume (info->native_handle);
101         return ret == KERN_SUCCESS;
102 }
103
104 void
105 mono_threads_platform_register (MonoThreadInfo *info)
106 {
107         info->native_handle = mach_thread_self ();
108 }
109
110 void
111 mono_threads_platform_free (MonoThreadInfo *info)
112 {
113         mach_port_deallocate (current_task (), info->native_handle);
114 }
115
116 MonoNativeThreadId
117 mono_native_thread_id_get (void)
118 {
119         return pthread_self ();
120 }
121
122 gboolean
123 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
124 {
125         return pthread_equal (id1, id2);
126 }
127
128 /*
129  * mono_native_thread_create:
130  *
131  *   Low level thread creation function without any GC wrappers.
132  */
133 gboolean
134 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
135 {
136         return pthread_create (tid, NULL, func, arg) == 0;
137 }
138
139 #endif