Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mono / utils / mono-threads-windows.c
1 /*
2  * mono-threads-windows.c: Low-level threading, windows 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(HOST_WIN32)
13
14 #include <mono/utils/mono-threads.h>
15
16
17 void
18 mono_threads_init_platform (void)
19 {
20 }
21
22 void
23 mono_threads_core_interrupt (MonoThreadInfo *info)
24 {
25         g_assert (0);
26 }
27
28 void
29 mono_threads_core_abort_syscall (MonoThreadInfo *info)
30 {
31 }
32
33 gboolean
34 mono_threads_core_needs_abort_syscall (void)
35 {
36         return FALSE;
37 }
38
39 void
40 mono_threads_core_self_suspend (MonoThreadInfo *info)
41 {
42         g_assert (0);
43 }
44
45 gboolean
46 mono_threads_core_suspend (MonoThreadInfo *info)
47 {
48         g_assert (0);
49 }
50
51 gboolean
52 mono_threads_core_resume (MonoThreadInfo *info)
53 {
54         g_assert (0);
55 }
56
57 void
58 mono_threads_platform_register (MonoThreadInfo *info)
59 {
60 }
61
62 void
63 mono_threads_platform_free (MonoThreadInfo *info)
64 {
65 }
66
67 typedef struct {
68         LPTHREAD_START_ROUTINE start_routine;
69         void *arg;
70         MonoSemType registered;
71         gboolean suspend;
72         HANDLE suspend_event;
73 } ThreadStartInfo;
74
75 static DWORD WINAPI
76 inner_start_thread (LPVOID arg)
77 {
78         ThreadStartInfo *start_info = arg;
79         void *t_arg = start_info->arg;
80         int post_result;
81         LPTHREAD_START_ROUTINE start_func = start_info->start_routine;
82         DWORD result;
83         gboolean suspend = start_info->suspend;
84         HANDLE suspend_event = start_info->suspend_event;
85         MonoThreadInfo *info;
86
87         info = mono_thread_info_attach (&result);
88         info->runtime_thread = TRUE;
89         info->create_suspended = suspend;
90
91         post_result = MONO_SEM_POST (&(start_info->registered));
92         g_assert (!post_result);
93
94         if (suspend) {
95                 WaitForSingleObject (suspend_event, INFINITE); /* caller will suspend the thread before setting the event. */
96                 CloseHandle (suspend_event);
97         }
98
99         result = start_func (t_arg);
100
101         g_assert (!mono_domain_get ());
102
103         mono_thread_info_detach ();
104
105         return result;
106 }
107
108 HANDLE
109 mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid)
110 {
111         ThreadStartInfo *start_info;
112         HANDLE result;
113         DWORD thread_id;
114
115         start_info = g_malloc0 (sizeof (ThreadStartInfo));
116         if (!start_info)
117                 return NULL;
118         MONO_SEM_INIT (&(start_info->registered), 0);
119         start_info->arg = arg;
120         start_info->start_routine = start_routine;
121         start_info->suspend = creation_flags & CREATE_SUSPENDED;
122         creation_flags &= ~CREATE_SUSPENDED;
123         if (start_info->suspend) {
124                 start_info->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
125                 if (!start_info->suspend_event)
126                         return NULL;
127         }
128
129         result = CreateThread (NULL, stack_size, inner_start_thread, start_info, creation_flags, &thread_id);
130         if (result) {
131                 while (MONO_SEM_WAIT (&(start_info->registered)) != 0) {
132                         /*if (EINTR != errno) ABORT("sem_wait failed"); */
133                 }
134                 if (start_info->suspend) {
135                         g_assert (SuspendThread (result) != (DWORD)-1);
136                         SetEvent (start_info->suspend_event);
137                 }
138         } else if (start_info->suspend) {
139                 CloseHandle (start_info->suspend_event);
140         }
141         if (out_tid)
142                 *out_tid = thread_id;
143         MONO_SEM_DESTROY (&(start_info->registered));
144         g_free (start_info);
145         return result;
146 }
147
148
149 MonoNativeThreadId
150 mono_native_thread_id_get (void)
151 {
152         return GetCurrentThreadId ();
153 }
154
155 gboolean
156 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
157 {
158         return id1 == id2;
159 }
160
161 gboolean
162 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
163 {
164         return CreateThread (NULL, 0, (func), (arg), 0, (tid)) != NULL;
165 }
166
167 void
168 mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
169 {
170         HANDLE handle;
171
172         handle = OpenThread (THREAD_ALL_ACCESS, TRUE, tid);
173         g_assert (handle);
174         ResumeThread (handle);
175         CloseHandle (handle);
176 }
177
178 #if HAVE_DECL___READFSDWORD==0
179 static __inline__ __attribute__((always_inline))
180 unsigned long long
181 __readfsdword (unsigned long offset)
182 {
183         unsigned long value;
184         //      __asm__("movl %%fs:%a[offset], %k[value]" : [value] "=q" (value) : [offset] "irm" (offset));
185    __asm__ volatile ("movl    %%fs:%1,%0"
186      : "=r" (value) ,"=m" ((*(volatile long *) offset)));
187         return value;
188 }
189 #endif
190
191 void
192 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
193 {
194 #ifdef TARGET_AMD64
195         /* win7 apis */
196         NT_TIB* tib = (NT_TIB*)NtCurrentTeb();
197         guint8 *stackTop = (guint8*)tib->StackBase;
198         guint8 *stackBottom = (guint8*)tib->StackLimit;
199 #else
200         /* http://en.wikipedia.org/wiki/Win32_Thread_Information_Block */
201         void* tib = (void*)__readfsdword(0x18);
202         guint8 *stackTop = (guint8*)*(int*)((char*)tib + 4);
203         guint8 *stackBottom = (guint8*)*(int*)((char*)tib + 8);
204 #endif
205
206         *staddr = stackBottom;
207         *stsize = stackTop - stackBottom;
208 }
209
210 gboolean
211 mono_threads_core_yield (void)
212 {
213         return SwitchToThread ();
214 }
215
216 void
217 mono_threads_core_exit (int exit_code)
218 {
219         ExitThread (exit_code);
220 }
221
222 void
223 mono_threads_core_unregister (MonoThreadInfo *info)
224 {
225 }
226
227 HANDLE
228 mono_threads_core_open_handle (void)
229 {
230         HANDLE thread_handle;
231
232         thread_handle = GetCurrentThread ();
233         g_assert (thread_handle);
234
235         /*
236          * The handle returned by GetCurrentThread () is a pseudo handle, so it can't be used to
237          * refer to the thread from other threads for things like aborting.
238          */
239         DuplicateHandle (GetCurrentProcess (), thread_handle, GetCurrentProcess (), &thread_handle,
240                                          THREAD_ALL_ACCESS, TRUE, 0);
241
242         return thread_handle;
243 }
244
245 #endif