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_self_suspend (MonoThreadInfo *info)
35 mono_threads_core_suspend (MonoThreadInfo *info)
41 mono_threads_core_resume (MonoThreadInfo *info)
47 mono_threads_platform_register (MonoThreadInfo *info)
52 mono_threads_platform_free (MonoThreadInfo *info)
57 LPTHREAD_START_ROUTINE start_routine;
59 MonoSemType registered;
65 inner_start_thread (LPVOID arg)
67 ThreadStartInfo *start_info = arg;
68 void *t_arg = start_info->arg;
70 LPTHREAD_START_ROUTINE start_func = start_info->start_routine;
72 gboolean suspend = start_info->suspend;
73 HANDLE suspend_event = start_info->suspend_event;
75 mono_thread_info_attach (&result);
77 post_result = MONO_SEM_POST (&(start_info->registered));
78 g_assert (!post_result);
82 WaitForSingleObject (suspend_event, INFINITE); /* caller will suspend the thread before setting the event. */
83 CloseHandle (suspend_event);
86 result = start_func (t_arg);
88 g_assert (!mono_domain_get ());
94 mono_threads_CreateThread (LPSECURITY_ATTRIBUTES attributes, SIZE_T stack_size, LPTHREAD_START_ROUTINE start_routine,
95 LPVOID arg, DWORD creation_flags, LPDWORD thread_id)
97 ThreadStartInfo *start_info;
100 start_info = g_malloc0 (sizeof (ThreadStartInfo));
103 MONO_SEM_INIT (&(start_info->registered), 0);
104 start_info->arg = arg;
105 start_info->start_routine = start_routine;
106 start_info->suspend = creation_flags & CREATE_SUSPENDED;
107 creation_flags &= ~CREATE_SUSPENDED;
108 if (start_info->suspend)
110 start_info->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
111 if (!start_info->suspend_event)
115 result = CreateThread (attributes, stack_size, inner_start_thread, start_info, creation_flags, thread_id);
118 while (MONO_SEM_WAIT (&(start_info->registered)) != 0) {
119 /*if (EINTR != errno) ABORT("sem_wait failed"); */
121 if (start_info->suspend)
123 g_assert (SuspendThread (result) != (DWORD)-1);
124 SetEvent (start_info->suspend_event);
127 else if (start_info->suspend)
128 CloseHandle (start_info->suspend_event);
129 MONO_SEM_DESTROY (&(start_info->registered));
136 mono_native_thread_id_get (void)
138 return GetCurrentThreadId ();
142 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
148 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
150 return CreateThread (NULL, 0, (func), (arg), 0, (tid)) != NULL;