2 * mono-threads-windows.c: Low-level threading, windows version
5 * Rodrigo Kumpera (kumpera@gmail.com)
12 #if defined(HOST_WIN32)
14 #include <mono/utils/mono-threads.h>
18 mono_threads_init_platform (void)
23 mono_threads_core_interrupt (MonoThreadInfo *info)
29 mono_threads_core_abort_syscall (MonoThreadInfo *info)
34 mono_threads_core_needs_abort_syscall (void)
40 mono_threads_core_self_suspend (MonoThreadInfo *info)
46 mono_threads_core_suspend (MonoThreadInfo *info)
52 mono_threads_core_resume (MonoThreadInfo *info)
58 mono_threads_platform_register (MonoThreadInfo *info)
63 mono_threads_platform_free (MonoThreadInfo *info)
68 LPTHREAD_START_ROUTINE start_routine;
70 MonoSemType registered;
76 inner_start_thread (LPVOID arg)
78 ThreadStartInfo *start_info = arg;
79 void *t_arg = start_info->arg;
81 LPTHREAD_START_ROUTINE start_func = start_info->start_routine;
83 gboolean suspend = start_info->suspend;
84 HANDLE suspend_event = start_info->suspend_event;
86 mono_thread_info_attach (&result);
88 post_result = MONO_SEM_POST (&(start_info->registered));
89 g_assert (!post_result);
93 WaitForSingleObject (suspend_event, INFINITE); /* caller will suspend the thread before setting the event. */
94 CloseHandle (suspend_event);
97 result = start_func (t_arg);
99 g_assert (!mono_domain_get ());
105 mono_threads_CreateThread (LPSECURITY_ATTRIBUTES attributes, SIZE_T stack_size, LPTHREAD_START_ROUTINE start_routine,
106 LPVOID arg, DWORD creation_flags, LPDWORD thread_id)
108 ThreadStartInfo *start_info;
111 start_info = g_malloc0 (sizeof (ThreadStartInfo));
114 MONO_SEM_INIT (&(start_info->registered), 0);
115 start_info->arg = arg;
116 start_info->start_routine = start_routine;
117 start_info->suspend = creation_flags & CREATE_SUSPENDED;
118 creation_flags &= ~CREATE_SUSPENDED;
119 if (start_info->suspend)
121 start_info->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
122 if (!start_info->suspend_event)
126 result = CreateThread (attributes, stack_size, inner_start_thread, start_info, creation_flags, thread_id);
129 while (MONO_SEM_WAIT (&(start_info->registered)) != 0) {
130 /*if (EINTR != errno) ABORT("sem_wait failed"); */
132 if (start_info->suspend)
134 g_assert (SuspendThread (result) != (DWORD)-1);
135 SetEvent (start_info->suspend_event);
138 else if (start_info->suspend)
139 CloseHandle (start_info->suspend_event);
140 MONO_SEM_DESTROY (&(start_info->registered));
147 mono_native_thread_id_get (void)
149 return GetCurrentThreadId ();
153 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
159 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
161 return CreateThread (NULL, 0, (func), (arg), 0, (tid)) != NULL;