Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mono / utils / mono-threads-windows.c
old mode 100644 (file)
new mode 100755 (executable)
index bf061dd..04430fb
@@ -12,6 +12,7 @@
 #if defined(HOST_WIN32)
 
 #include <mono/utils/mono-threads.h>
+#include <mono/utils/mono-compiler.h>
 #include <limits.h>
 
 
@@ -20,10 +21,23 @@ mono_threads_init_platform (void)
 {
 }
 
+static void CALLBACK
+interrupt_apc (ULONG_PTR param)
+{
+}
+
 void
 mono_threads_core_interrupt (MonoThreadInfo *info)
 {
-       g_assert (0);
+       DWORD id = mono_thread_info_get_tid (info);
+       HANDLE handle;
+
+       handle = OpenThread (THREAD_ALL_ACCESS, FALSE, id);
+       g_assert (handle);
+
+       QueueUserAPC ((PAPCFUNC)interrupt_apc, handle, (ULONG_PTR)NULL);
+
+       CloseHandle (handle);
 }
 
 void
@@ -40,19 +54,79 @@ mono_threads_core_needs_abort_syscall (void)
 void
 mono_threads_core_self_suspend (MonoThreadInfo *info)
 {
-       g_assert (0);
+       g_assert_not_reached ();
 }
 
 gboolean
 mono_threads_core_suspend (MonoThreadInfo *info)
 {
-       g_assert (0);
+       DWORD id = mono_thread_info_get_tid (info);
+       HANDLE handle;
+       DWORD result;
+       gboolean res;
+
+       g_assert (id != GetCurrentThreadId ());
+
+       handle = OpenThread (THREAD_ALL_ACCESS, FALSE, id);
+       g_assert (handle);
+
+       result = SuspendThread (handle);
+       if (result == (DWORD)-1) {
+               fprintf (stderr, "could not suspend thread %x (handle %p): %d\n", id, handle, GetLastError ()); fflush (stderr);
+               CloseHandle (handle);
+               return FALSE;
+       }
+
+       CloseHandle (handle);
+
+       res = mono_threads_get_runtime_callbacks ()->thread_state_init_from_handle (&info->suspend_state, info);
+       g_assert (res);
+
+       return TRUE;
 }
 
 gboolean
 mono_threads_core_resume (MonoThreadInfo *info)
 {
-       g_assert (0);
+       DWORD id = mono_thread_info_get_tid (info);
+       HANDLE handle;
+       DWORD result;
+
+       handle = OpenThread (THREAD_ALL_ACCESS, FALSE, id);
+       g_assert (handle);
+
+       if (info->async_target) {
+               MonoContext ctx;
+               CONTEXT context;
+               gboolean res;
+
+               ctx = info->suspend_state.ctx;
+               mono_threads_get_runtime_callbacks ()->setup_async_callback (&ctx, info->async_target, info->user_data);
+               info->async_target = info->user_data = NULL;
+
+               context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
+
+               if (!GetThreadContext (handle, &context)) {
+                       CloseHandle (handle);
+                       return FALSE;
+               }
+
+               g_assert (context.ContextFlags & CONTEXT_INTEGER);
+               g_assert (context.ContextFlags & CONTEXT_CONTROL);
+
+               mono_monoctx_to_sigctx (&ctx, &context);
+
+               context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
+               res = SetThreadContext (handle, &context);
+               g_assert (res);
+       }
+
+       result = ResumeThread (handle);
+       g_assert (result != (DWORD)-1);
+
+       CloseHandle (handle);
+
+       return result != (DWORD)-1;
 }
 
 void
@@ -99,8 +173,6 @@ inner_start_thread (LPVOID arg)
 
        result = start_func (t_arg);
 
-       g_assert (!mono_domain_get ());
-
        mono_thread_info_detach ();
 
        return result;
@@ -177,8 +249,7 @@ mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
 }
 
 #if HAVE_DECL___READFSDWORD==0
-static __inline__ __attribute__((always_inline))
-unsigned long long
+static MONO_ALWAYS_INLINE unsigned long long
 __readfsdword (unsigned long offset)
 {
        unsigned long value;
@@ -192,6 +263,7 @@ __readfsdword (unsigned long offset)
 void
 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
 {
+       MEMORY_BASIC_INFORMATION meminfo;
 #ifdef TARGET_AMD64
        /* win7 apis */
        NT_TIB* tib = (NT_TIB*)NtCurrentTeb();
@@ -203,9 +275,17 @@ mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
        guint8 *stackTop = (guint8*)*(int*)((char*)tib + 4);
        guint8 *stackBottom = (guint8*)*(int*)((char*)tib + 8);
 #endif
+       /*
+       Windows stacks are expanded on demand, one page at time. The TIB reports
+       only the currently allocated amount.
+       VirtualQuery will return the actual limit for the bottom, which is what we want.
+       */
+       if (VirtualQuery (&meminfo, &meminfo, sizeof (meminfo)) == sizeof (meminfo))
+               stackBottom = MIN (stackBottom, (guint8*)meminfo.AllocationBase);
 
        *staddr = stackBottom;
        *stsize = stackTop - stackBottom;
+
 }
 
 gboolean
@@ -256,4 +336,58 @@ mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
        return OpenThread (THREAD_ALL_ACCESS, TRUE, tid);
 }
 
+#if !defined(__GNUC__)
+const DWORD MS_VC_EXCEPTION=0x406D1388;
+#pragma pack(push,8)
+typedef struct tagTHREADNAME_INFO
+{
+   DWORD dwType; // Must be 0x1000.
+   LPCSTR szName; // Pointer to name (in user addr space).
+   DWORD dwThreadID; // Thread ID (-1=caller thread).
+  DWORD dwFlags; // Reserved for future use, must be zero.
+} THREADNAME_INFO;
+#pragma pack(pop)
+#endif
+
+void
+mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
+{
+#if !defined(__GNUC__)
+       /* http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx */
+       THREADNAME_INFO info;
+       info.dwType = 0x1000;
+       info.szName = name;
+       info.dwThreadID = tid;
+       info.dwFlags = 0;
+
+       __try {
+               RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR),       (ULONG_PTR*)&info );
+       }
+       __except(EXCEPTION_EXECUTE_HANDLER) {
+       }
+#endif
+}
+
+
+gpointer
+mono_threads_core_prepare_interrupt (HANDLE thread_handle)
+{
+       return NULL;
+}
+
+void
+mono_threads_core_finish_interrupt (gpointer wait_handle)
+{
+}
+
+void
+mono_threads_core_self_interrupt (void)
+{
+}
+
+void
+mono_threads_core_clear_interruption (void)
+{
+}
+
 #endif