Updated with review feedback.
authorlateralusX <lateralusx.github@gmail.com>
Tue, 19 Sep 2017 15:17:23 +0000 (17:17 +0200)
committerlateralusX <lateralusx.github@gmail.com>
Mon, 25 Sep 2017 13:41:32 +0000 (15:41 +0200)
mono/metadata/threads.c
mono/utils/mono-os-wait-win32.c

index cc4705c813336a8dfb7d1936d43f1324267aba4c..7133185e36cc984c7741442b72b5e48b672c43a6 100644 (file)
@@ -4455,7 +4455,7 @@ mono_thread_execute_interruption (void)
        }
 
        /* this will consume pending APC calls */
-#ifdef USE_WINDOWS_BACKEND
+#ifdef HOST_WIN32
        mono_win32_wait_for_single_object_ex (GetCurrentThread (), 0, TRUE);
 #endif
 
@@ -4526,7 +4526,7 @@ mono_thread_request_interruption (gboolean running_managed)
 
                /* this will awake the thread if it is in WaitForSingleObject 
                   or similar */
-#ifdef USE_WINDOWS_BACKEND
+#ifdef HOST_WIN32
                mono_win32_interrupt_wait (thread->thread_info, thread->native_handle, (DWORD)thread->tid);
 #else
                mono_thread_info_self_interrupt ();
index df0fd06b3a34a5a21c138d336ffa7a498e9b683b..a302d973295d80d5b3cd95cfbc81b8daced08a8a 100644 (file)
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/mono-threads-debug.h>
 
-#define THREAD_WAIT_INFO_CLEARED 0
-#define THREAD_WAIT_INFO_ALERTABLE_WAIT_SLOT 1
-#define THREAD_WAIT_INFO_PENDING_INTERRUPT_APC_SLOT 2
-#define THREAD_WAIT_INFO_PENDING_ABORT_APC_SLOT 4
+enum ThreadWaitInfo {
+       THREAD_WAIT_INFO_CLEARED = 0,
+       THREAD_WAIT_INFO_ALERTABLE_WAIT_SLOT = 1 << 0,
+       THREAD_WAIT_INFO_PENDING_INTERRUPT_APC_SLOT = 1 << 1,
+       THREAD_WAIT_INFO_PENDING_ABORT_APC_SLOT = 1 << 2
+};
 
 static inline void
-reqeust_interrupt (gpointer thread_info, HANDLE native_thread_handle, gint32 pending_apc_slot, PAPCFUNC apc_callback, DWORD tid)
+request_interrupt (gpointer thread_info, HANDLE native_thread_handle, gint32 pending_apc_slot, PAPCFUNC apc_callback, DWORD tid)
 {
        /*
        * On Windows platforms, an async interrupt/abort request queues an APC
@@ -34,23 +36,18 @@ reqeust_interrupt (gpointer thread_info, HANDLE native_thread_handle, gint32 pen
        * (like service the interrupt/abort request).
        */
        MonoThreadInfo *info = (MonoThreadInfo *)thread_info;
-       gboolean queue_apc = FALSE;
+       gint32 old_wait_info, new_wait_info;
 
-       while (!queue_apc) {
-               gint32 old_wait_info = InterlockedRead (&info->thread_wait_info);
+       do {
+               old_wait_info = InterlockedRead (&info->thread_wait_info);
                if (old_wait_info & pending_apc_slot)
-                       break;
+                       return;
 
-               gint32 new_wait_info = old_wait_info | pending_apc_slot;
-               if (InterlockedCompareExchange (&info->thread_wait_info, new_wait_info, old_wait_info) == old_wait_info) {
-                       queue_apc = TRUE;
-               }
-       }
+               new_wait_info = old_wait_info | pending_apc_slot;
+       } while (InterlockedCompareExchange (&info->thread_wait_info, new_wait_info, old_wait_info) != old_wait_info);
 
-       if (queue_apc == TRUE) {
-               THREADS_INTERRUPT_DEBUG ("%06d - Interrupting/Aborting syscall in thread %06d", GetCurrentThreadId (), tid);
-               QueueUserAPC (apc_callback, native_thread_handle, (ULONG_PTR)NULL);
-       }
+       THREADS_INTERRUPT_DEBUG ("%06d - Interrupting/Aborting syscall in thread %06d", GetCurrentThreadId (), tid);
+       QueueUserAPC (apc_callback, native_thread_handle, (ULONG_PTR)NULL);
 }
 
 static void CALLBACK
@@ -62,7 +59,7 @@ interrupt_apc (ULONG_PTR param)
 void
 mono_win32_interrupt_wait (PVOID thread_info, HANDLE native_thread_handle, DWORD tid)
 {
-       reqeust_interrupt (thread_info, native_thread_handle, THREAD_WAIT_INFO_PENDING_INTERRUPT_APC_SLOT, interrupt_apc, tid);
+       request_interrupt (thread_info, native_thread_handle, THREAD_WAIT_INFO_PENDING_INTERRUPT_APC_SLOT, interrupt_apc, tid);
 }
 
 static void CALLBACK
@@ -74,7 +71,7 @@ abort_apc (ULONG_PTR param)
 void
 mono_win32_abort_wait (PVOID thread_info, HANDLE native_thread_handle, DWORD tid)
 {
-       reqeust_interrupt (thread_info, native_thread_handle, THREAD_WAIT_INFO_PENDING_ABORT_APC_SLOT, abort_apc, tid);
+       request_interrupt (thread_info, native_thread_handle, THREAD_WAIT_INFO_PENDING_ABORT_APC_SLOT, abort_apc, tid);
 }
 
 static inline void