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