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)->runtime_thread = TRUE;
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 ());
101 mono_thread_info_dettach ();
107 mono_threads_CreateThread (LPSECURITY_ATTRIBUTES attributes, SIZE_T stack_size, LPTHREAD_START_ROUTINE start_routine,
108 LPVOID arg, DWORD creation_flags, LPDWORD thread_id)
110 ThreadStartInfo *start_info;
113 start_info = g_malloc0 (sizeof (ThreadStartInfo));
116 MONO_SEM_INIT (&(start_info->registered), 0);
117 start_info->arg = arg;
118 start_info->start_routine = start_routine;
119 start_info->suspend = creation_flags & CREATE_SUSPENDED;
120 creation_flags &= ~CREATE_SUSPENDED;
121 if (start_info->suspend)
123 start_info->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
124 if (!start_info->suspend_event)
128 result = CreateThread (attributes, stack_size, inner_start_thread, start_info, creation_flags, thread_id);
131 while (MONO_SEM_WAIT (&(start_info->registered)) != 0) {
132 /*if (EINTR != errno) ABORT("sem_wait failed"); */
134 if (start_info->suspend)
136 g_assert (SuspendThread (result) != (DWORD)-1);
137 SetEvent (start_info->suspend_event);
140 else if (start_info->suspend)
141 CloseHandle (start_info->suspend_event);
142 MONO_SEM_DESTROY (&(start_info->registered));
149 mono_native_thread_id_get (void)
151 return GetCurrentThreadId ();
155 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
161 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
163 return CreateThread (NULL, 0, (func), (arg), 0, (tid)) != NULL;