Merge pull request #3769 from evincarofautumn/fix-verify-before-allocs
[mono.git] / mono / utils / mono-threads-windows.c
index ca6159b07f52e277b41d35a7b756a37201f81c87..9a8a93b96948afc523515992b7737553166263b3 100644 (file)
@@ -12,6 +12,7 @@
 #if defined(USE_WINDOWS_BACKEND)
 
 #include <mono/utils/mono-compiler.h>
+#include <mono/utils/mono-threads-debug.h>
 #include <limits.h>
 
 
@@ -63,7 +64,7 @@ mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interru
        }
 
        CloseHandle (handle);
-       return info->suspend_can_continue;
+       return TRUE;
 }
 
 gboolean
@@ -72,6 +73,33 @@ mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
        return info->suspend_can_continue;
 }
 
+static void CALLBACK
+abort_apc (ULONG_PTR param)
+{
+       THREADS_INTERRUPT_DEBUG ("%06d - abort_apc () called", GetCurrentThreadId ());
+}
+
+void
+mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
+{
+       DWORD id = mono_thread_info_get_tid (info);
+       HANDLE handle;
+
+       handle = OpenThread (THREAD_ALL_ACCESS, FALSE, id);
+       g_assert (handle);
+
+       THREADS_INTERRUPT_DEBUG ("%06d - Aborting syscall in thread %06d", GetCurrentThreadId (), id);
+       QueueUserAPC ((PAPCFUNC)abort_apc, handle, (ULONG_PTR)NULL);
+
+       CloseHandle (handle);
+}
+
+gboolean
+mono_threads_suspend_needs_abort_syscall (void)
+{
+       return TRUE;
+}
+
 gboolean
 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
 {
@@ -128,6 +156,35 @@ mono_threads_suspend_free (MonoThreadInfo *info)
 {
 }
 
+void
+mono_threads_suspend_init_signals (void)
+{
+}
+
+gint
+mono_threads_suspend_search_alternative_signal (void)
+{
+       g_assert_not_reached ();
+}
+
+gint
+mono_threads_suspend_get_suspend_signal (void)
+{
+       return -1;
+}
+
+gint
+mono_threads_suspend_get_restart_signal (void)
+{
+       return -1;
+}
+
+gint
+mono_threads_suspend_get_abort_signal (void)
+{
+       return -1;
+}
+
 #endif
 
 #if defined (HOST_WIN32)
@@ -148,84 +205,30 @@ mono_threads_platform_register (MonoThreadInfo *info)
        info->handle = thread_handle;
 }
 
-typedef struct {
-       LPTHREAD_START_ROUTINE start_routine;
-       void *arg;
-       gint32 priority;
-       MonoCoopSem registered;
-       gboolean suspend;
-       HANDLE handle;
-} ThreadStartInfo;
-
-static DWORD WINAPI
-inner_start_thread (LPVOID arg)
+int
+mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *out_tid)
 {
-       ThreadStartInfo *start_info = arg;
-       void *t_arg = start_info->arg;
-       LPTHREAD_START_ROUTINE start_func = start_info->start_routine;
-       DWORD result;
-       gboolean suspend = start_info->suspend;
-       MonoThreadInfo *info;
-       int res;
-
-       info = mono_thread_info_attach (&result);
-       info->runtime_thread = TRUE;
-
-       start_info->handle = info->handle;
-
-       mono_threads_platform_set_priority(info, start_info->priority);
+       HANDLE result;
+       DWORD thread_id;
 
-       if (suspend) {
-               info->create_suspended = TRUE;
-               mono_coop_sem_init (&info->create_suspended_sem, 0);
-       }
+       result = CreateThread (NULL, stack_size ? *stack_size : 0, (LPTHREAD_START_ROUTINE) thread_fn, thread_data, 0, &thread_id);
+       if (!result)
+               return -1;
 
-       mono_coop_sem_post (&(start_info->registered));
+       /* A new handle is open when attaching
+        * the thread, so we don't need this one */
+       CloseHandle (result);
 
-       if (suspend) {
-               res = mono_coop_sem_wait (&info->create_suspended_sem, MONO_SEM_FLAGS_NONE);
-               g_assert (res != -1);
+       if (out_tid)
+               *out_tid = thread_id;
 
-               mono_coop_sem_destroy (&info->create_suspended_sem);
+       if (stack_size) {
+               // TOOD: Use VirtualQuery to get correct value 
+               // http://stackoverflow.com/questions/2480095/thread-stack-size-on-windows-visual-c
+               *stack_size = 2 * 1024 * 1024;
        }
 
-       result = start_func (t_arg);
-
-       mono_thread_info_detach ();
-
-       return result;
-}
-
-HANDLE
-mono_threads_platform_create_thread (MonoThreadStart start_routine, gpointer arg, MonoThreadParm *tp, MonoNativeThreadId *out_tid)
-{
-       ThreadStartInfo start_info;
-       HANDLE result;
-       DWORD thread_id;
-       guint32 creation_flags = tp->creation_flags;
-       int res;
-
-       memset (&start_info, 0, sizeof (start_info));
-       mono_coop_sem_init (&(start_info.registered), 0);
-       start_info.arg = arg;
-       start_info.start_routine = start_routine;
-       start_info.suspend = creation_flags & CREATE_SUSPENDED;
-       start_info.priority = tp->priority;
-       creation_flags &= ~CREATE_SUSPENDED;
-
-       result = CreateThread (NULL, tp->stack_size, inner_start_thread, &start_info, creation_flags, &thread_id);
-       if (result) {
-               res = mono_coop_sem_wait (&(start_info.registered), MONO_SEM_FLAGS_NONE);
-               g_assert (res != -1);
-
-               /* A new handle has been opened when attaching
-                * the thread, so we don't need this one */
-               CloseHandle (result);
-       }
-       if (out_tid)
-               *out_tid = thread_id;
-       mono_coop_sem_destroy (&(start_info.registered));
-       return start_info.handle;
+       return 0;
 }
 
 
@@ -247,6 +250,21 @@ mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
        return CreateThread (NULL, 0, (func), (arg), 0, (tid)) != NULL;
 }
 
+gboolean
+mono_native_thread_join (MonoNativeThreadId tid)
+{
+       HANDLE handle;
+
+       if (!(handle = OpenThread (THREAD_ALL_ACCESS, TRUE, tid)))
+               return FALSE;
+
+       DWORD res = WaitForSingleObject (handle, INFINITE);
+
+       CloseHandle (handle);
+
+       return res != WAIT_FAILED;
+}
+
 #if HAVE_DECL___READFSDWORD==0
 static MONO_ALWAYS_INLINE unsigned long long
 __readfsdword (unsigned long offset)
@@ -303,7 +321,10 @@ mono_threads_platform_exit (int exit_code)
 void
 mono_threads_platform_unregister (MonoThreadInfo *info)
 {
-       mono_threads_platform_set_exited (info);
+       g_assert (info->handle);
+
+       CloseHandle (info->handle);
+       info->handle = NULL;
 }
 
 int
@@ -313,6 +334,17 @@ mono_threads_get_max_stack_size (void)
        return INT_MAX;
 }
 
+gpointer
+mono_threads_platform_duplicate_handle (MonoThreadInfo *info)
+{
+       HANDLE thread_handle;
+
+       g_assert (info->handle);
+       DuplicateHandle (GetCurrentProcess (), info->handle, GetCurrentProcess (), &thread_handle, THREAD_ALL_ACCESS, TRUE, 0);
+
+       return thread_handle;
+}
+
 HANDLE
 mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
 {
@@ -358,44 +390,8 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
 }
 
 void
-mono_threads_platform_set_exited (MonoThreadInfo *info)
-{
-       g_assert (info->handle);
-       // No need to call CloseHandle() here since the InternalThread
-       // destructor will close the handle when the finalizer thread calls it
-       info->handle = NULL;
-}
-
-void
-mono_threads_platform_describe (MonoThreadInfo *info, GString *text)
+mono_threads_platform_set_exited (gpointer handle)
 {
-       /* TODO */
-}
-
-void
-mono_threads_platform_own_mutex (MonoThreadInfo *info, gpointer mutex_handle)
-{
-       g_assert_not_reached ();
-}
-
-void
-mono_threads_platform_disown_mutex (MonoThreadInfo *info, gpointer mutex_handle)
-{
-       g_assert_not_reached ();
-}
-
-MonoThreadPriority
-mono_threads_platform_get_priority (MonoThreadInfo *info)
-{
-       g_assert (info->handle);
-       return GetThreadPriority (info->handle) + 2;
-}
-
-gboolean
-mono_threads_platform_set_priority (MonoThreadInfo *info, MonoThreadPriority priority)
-{
-       g_assert (info->handle);
-       return SetThreadPriority (info->handle, priority - 2);
 }
 
 void