[threads] Remove ThreadState_StopRequested (#4462)
[mono.git] / mono / metadata / monitor.c
index fe09d0c24b881a1c10ee7046c94cb5c5aba395e8..676400c152e8d414dbf72f87b4d74e86037e0fb2 100644 (file)
@@ -18,7 +18,6 @@
 #include <mono/metadata/threads-types.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/threads.h>
-#include <mono/io-layer/io-layer.h>
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/class-internals.h>
 #include <mono/metadata/gc-internals.h>
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/marshal.h>
+#include <mono/metadata/w32event.h>
 #include <mono/utils/mono-threads.h>
 #include <mono/metadata/profiler-private.h>
 #include <mono/utils/mono-time.h>
 #include <mono/utils/atomic.h>
+#include <mono/utils/w32api.h>
 
 /*
  * Pull the list of opcodes
@@ -395,7 +396,7 @@ mon_new (gsize id)
                                                /* Orphaned events left by aborted threads */
                                                while (new_->wait_list) {
                                                        LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d): Closing orphaned event %d", mono_thread_info_get_small_id (), new_->wait_list->data));
-                                                       CloseHandle (new_->wait_list->data);
+                                                       mono_w32event_close (new_->wait_list->data);
                                                        new_->wait_list = g_slist_remove (new_->wait_list, new_->wait_list->data);
                                                }
                                        }
@@ -630,18 +631,19 @@ mono_object_hash (MonoObject* obj)
 #endif
 }
 
-static void
+static gboolean
 mono_monitor_ensure_owned (LockWord lw, guint32 id)
 {
        if (lock_word_is_flat (lw)) {
                if (lock_word_get_owner (lw) == id)
-                       return;
+                       return TRUE;
        } else if (lock_word_is_inflated (lw)) {
                if (mon_status_get_owner (lock_word_get_inflated_lock (lw)->status) == id)
-                       return;
+                       return TRUE;
        }
 
        mono_set_pending_exception (mono_get_exception_synchronization_lock ("Object synchronization method was called from an unsynchronized block of code."));
+       return FALSE;
 }
 
 /*
@@ -866,7 +868,7 @@ retry_contended:
                }
        }
 
-       if (ms != INFINITE) {
+       if (ms != MONO_INFINITE_WAIT) {
                then = mono_msec_ticks ();
        }
        waitms = ms;
@@ -877,7 +879,25 @@ retry_contended:
 #endif
        thread = mono_thread_internal_current ();
 
-       mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
+       /*
+        * If we allow interruption, we check the test state for an abort request before going into sleep.
+        * This is a workaround to the fact that Thread.Abort does non-sticky interruption of semaphores.
+        *
+        * Semaphores don't support the sticky interruption with mono_thread_info_install_interrupt.
+        *
+        * A better fix would be to switch to wait with something that allows sticky interrupts together
+        * with wrapping it with abort_protected_block_count for the non-alertable cases.
+        * And somehow make this whole dance atomic and not crazy expensive. Good luck.
+        *
+        */
+       if (allow_interruption) {
+               if (!mono_thread_test_and_set_state (thread, ThreadState_AbortRequested, ThreadState_WaitSleepJoin)) {
+                       wait_ret = MONO_SEM_TIMEDWAIT_RET_ALERTED;
+                       goto done_waiting;
+               }
+       } else {
+               mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
+       }
 
        /*
         * We pass ALERTABLE instead of allow_interruption since we have to check for the
@@ -886,7 +906,8 @@ retry_contended:
        wait_ret = mono_coop_sem_timedwait (mon->entry_sem, waitms, MONO_SEM_FLAGS_ALERTABLE);
 
        mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
-       
+
+done_waiting:
 #ifndef DISABLE_PERFCOUNTERS
        mono_perfcounters->thread_queue_len--;
 #endif
@@ -897,8 +918,8 @@ retry_contended:
                 * We have to obey a stop/suspend request even if 
                 * allow_interruption is FALSE to avoid hangs at shutdown.
                 */
-               if (!mono_thread_test_state (mono_thread_internal_current (), (MonoThreadState)(ThreadState_StopRequested | ThreadState_SuspendRequested | ThreadState_AbortRequested))) {
-                       if (ms != INFINITE) {
+               if (!mono_thread_test_state (mono_thread_internal_current (), ThreadState_SuspendRequested | ThreadState_AbortRequested)) {
+                       if (ms != MONO_INFINITE_WAIT) {
                                now = mono_msec_ticks ();
 
                                /* it should not overflow before ~30k years */
@@ -930,7 +951,7 @@ retry_contended:
        if (wait_ret == MONO_SEM_TIMEDWAIT_RET_ALERTED) {
                LOCK_DEBUG (g_message ("%s: (%d) interrupted waiting, returning -1", __func__, id));
                return -1;
-       } else if (wait_ret == MONO_SEM_TIMEDWAIT_RET_ALERTED) {
+       } else if (wait_ret == MONO_SEM_TIMEDWAIT_RET_TIMEDOUT) {
                LOCK_DEBUG (g_message ("%s: (%d) timed out waiting, returning FALSE", __func__, id));
                return 0;
        } else {
@@ -993,17 +1014,49 @@ mono_monitor_try_enter_internal (MonoObject *obj, guint32 ms, gboolean allow_int
        return -1;
 }
 
-gboolean 
-mono_monitor_enter (MonoObject *obj)
+/* This is an icall */
+MonoBoolean
+mono_monitor_enter_internal (MonoObject *obj)
 {
+       gint32 res;
+       gboolean allow_interruption = TRUE;
        if (G_UNLIKELY (!obj)) {
                mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
                return FALSE;
        }
-       return mono_monitor_try_enter_internal (obj, INFINITE, FALSE) == 1;
+
+       /*
+        * An inquisitive mind could ask what's the deal with this loop.
+        * It exists to deal with interrupting a monitor enter that happened within an abort-protected block, like a .cctor.
+        *
+        * The thread will be set with a pending abort and the wait might even be interrupted. Either way, once we call mono_thread_interruption_checkpoint,
+        * it will return NULL meaning we can't be aborted right now. Once that happens we switch to non-alertable.
+        */
+       do {
+               res = mono_monitor_try_enter_internal (obj, MONO_INFINITE_WAIT, allow_interruption);
+               /*This means we got interrupted during the wait and didn't got the monitor.*/
+               if (res == -1) {
+                       MonoException *exc = mono_thread_interruption_checkpoint ();
+                       if (exc) {
+                               mono_set_pending_exception (exc);
+                               return FALSE;
+                       } else {
+                               //we detected a pending interruption but it turned out to be a false positive, we ignore it from now on (this feels like a hack, right?, threads.c should give us less confusing directions)
+                               allow_interruption = FALSE;
+                       }
+               }
+       } while (res == -1);
+       return TRUE;
 }
 
-gboolean 
+gboolean
+mono_monitor_enter (MonoObject *obj)
+{
+       return mono_monitor_enter_internal (obj);
+}
+
+/* Called from JITted code so we return guint32 instead of gboolean */
+guint32
 mono_monitor_enter_fast (MonoObject *obj)
 {
        if (G_UNLIKELY (!obj)) {
@@ -1038,7 +1091,8 @@ mono_monitor_exit (MonoObject *obj)
 
        lw.sync = obj->synchronisation;
 
-       mono_monitor_ensure_owned (lw, mono_thread_info_get_small_id ());
+       if (!mono_monitor_ensure_owned (lw, mono_thread_info_get_small_id ()))
+               return;
 
        if (G_UNLIKELY (lock_word_is_inflated (lw)))
                mono_monitor_exit_inflated (obj);
@@ -1080,21 +1134,25 @@ mono_monitor_threads_sync_members_offset (int *status_offset, int *nest_offset)
 }
 
 void
-ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (MonoObject *obj, guint32 ms, char *lockTaken)
+ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (MonoObject *obj, guint32 ms, MonoBoolean *lockTaken)
 {
        gint32 res;
+       gboolean allow_interruption = TRUE;
        if (G_UNLIKELY (!obj)) {
                mono_set_pending_exception (mono_get_exception_argument_null ("obj"));
                return;
        }
        do {
-               res = mono_monitor_try_enter_internal (obj, ms, TRUE);
+               res = mono_monitor_try_enter_internal (obj, ms, allow_interruption);
                /*This means we got interrupted during the wait and didn't got the monitor.*/
                if (res == -1) {
                        MonoException *exc = mono_thread_interruption_checkpoint ();
                        if (exc) {
                                mono_set_pending_exception (exc);
                                return;
+                       } else {
+                               //we detected a pending interruption but it turned out to be a false positive, we ignore it from now on (this feels like a hack, right?, threads.c should give us less confusing directions)
+                               allow_interruption = FALSE;
                        }
                }
        } while (res == -1);
@@ -1110,7 +1168,22 @@ mono_monitor_enter_v4 (MonoObject *obj, char *lock_taken)
                return;
        }
 
-       ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (obj, INFINITE, lock_taken);
+       MonoBoolean taken;
+
+       ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (obj, MONO_INFINITE_WAIT, &taken);
+       *lock_taken = taken;
+}
+
+/* Called from JITted code */
+void
+mono_monitor_enter_v4_internal (MonoObject *obj, MonoBoolean *lock_taken)
+{
+       if (*lock_taken == 1) {
+               mono_set_pending_exception (mono_get_exception_argument ("lockTaken", "lockTaken is already true"));
+               return;
+       }
+
+       ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (obj, MONO_INFINITE_WAIT, lock_taken);
 }
 
 /*
@@ -1119,9 +1192,10 @@ mono_monitor_enter_v4 (MonoObject *obj, char *lock_taken)
  *   Same as mono_monitor_enter_v4, but return immediately if the
  * monitor cannot be acquired.
  * Returns TRUE if the lock was acquired, FALSE otherwise.
+ * Called from JITted code so we return guint32 instead of gboolean.
  */
-gboolean
-mono_monitor_enter_v4_fast (MonoObject *obj, char *lock_taken)
+guint32
+mono_monitor_enter_v4_fast (MonoObject *obj, MonoBoolean *lock_taken)
 {
        if (*lock_taken == 1)
                return FALSE;
@@ -1132,7 +1206,7 @@ mono_monitor_enter_v4_fast (MonoObject *obj, char *lock_taken)
        return res == 1;
 }
 
-gboolean 
+MonoBoolean
 ves_icall_System_Threading_Monitor_Monitor_test_owner (MonoObject *obj)
 {
        LockWord lw;
@@ -1150,7 +1224,7 @@ ves_icall_System_Threading_Monitor_Monitor_test_owner (MonoObject *obj)
        return(FALSE);
 }
 
-gboolean 
+MonoBoolean
 ves_icall_System_Threading_Monitor_Monitor_test_synchronised (MonoObject *obj)
 {
        LockWord lw;
@@ -1185,7 +1259,8 @@ ves_icall_System_Threading_Monitor_Monitor_pulse (MonoObject *obj)
        id = mono_thread_info_get_small_id ();
        lw.sync = obj->synchronisation;
 
-       mono_monitor_ensure_owned (lw, id);
+       if (!mono_monitor_ensure_owned (lw, id))
+               return;
 
        if (!lock_word_is_inflated (lw)) {
                /* No threads waiting. A wait would have inflated the lock */
@@ -1199,7 +1274,7 @@ ves_icall_System_Threading_Monitor_Monitor_pulse (MonoObject *obj)
        if (mon->wait_list != NULL) {
                LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, mono_thread_info_get_small_id (), mon->wait_list->data));
        
-               SetEvent (mon->wait_list->data);
+               mono_w32event_set (mon->wait_list->data);
                mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
        }
 }
@@ -1216,7 +1291,8 @@ ves_icall_System_Threading_Monitor_Monitor_pulse_all (MonoObject *obj)
        id = mono_thread_info_get_small_id ();
        lw.sync = obj->synchronisation;
 
-       mono_monitor_ensure_owned (lw, id);
+       if (!mono_monitor_ensure_owned (lw, id))
+               return;
 
        if (!lock_word_is_inflated (lw)) {
                /* No threads waiting. A wait would have inflated the lock */
@@ -1230,19 +1306,19 @@ ves_icall_System_Threading_Monitor_Monitor_pulse_all (MonoObject *obj)
        while (mon->wait_list != NULL) {
                LOCK_DEBUG (g_message ("%s: (%d) signalling and dequeuing handle %p", __func__, mono_thread_info_get_small_id (), mon->wait_list->data));
        
-               SetEvent (mon->wait_list->data);
+               mono_w32event_set (mon->wait_list->data);
                mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
        }
 }
 
-gboolean
+MonoBoolean
 ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
 {
        LockWord lw;
        MonoThreadsSync *mon;
        HANDLE event;
        guint32 nest;
-       guint32 ret;
+       MonoW32HandleWaitRet ret;
        gboolean success = FALSE;
        gint32 regain;
        MonoInternalThread *thread = mono_thread_internal_current ();
@@ -1252,7 +1328,8 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
 
        lw.sync = obj->synchronisation;
 
-       mono_monitor_ensure_owned (lw, id);
+       if (!mono_monitor_ensure_owned (lw, id))
+               return FALSE;
 
        if (!lock_word_is_inflated (lw)) {
                mono_monitor_inflate_owned (obj, id);
@@ -1265,7 +1342,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
        if (mono_thread_current_check_pending_interrupt ())
                return FALSE;
        
-       event = CreateEvent (NULL, FALSE, FALSE, NULL);
+       event = mono_w32event_create (FALSE, FALSE);
        if (event == NULL) {
                mono_set_pending_exception (mono_get_exception_synchronization_lock ("Failed to set up wait event"));
                return FALSE;
@@ -1275,7 +1352,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
 
        /* This looks superfluous */
        if (mono_thread_current_check_pending_interrupt ()) {
-               CloseHandle (event);
+               mono_w32event_close (event);
                return FALSE;
        }
        
@@ -1297,7 +1374,11 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
         * signalled before we wait, we still succeed.
         */
        MONO_ENTER_GC_SAFE;
-       ret = WaitForSingleObjectEx (event, ms, TRUE);
+#ifdef HOST_WIN32
+       ret = mono_w32handle_convert_wait_ret (WaitForSingleObjectEx (event, ms, TRUE), 1);
+#else
+       ret = mono_w32handle_wait_one (event, ms, TRUE);
+#endif /* HOST_WIN32 */
        MONO_EXIT_GC_SAFE;
 
        /* Reset the thread state fairly early, so we don't have to worry
@@ -1307,7 +1388,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
 
        /* Regain the lock with the previous nest count */
        do {
-               regain = mono_monitor_try_enter_inflated (obj, INFINITE, TRUE, id);
+               regain = mono_monitor_try_enter_inflated (obj, MONO_INFINITE_WAIT, TRUE, id);
                /* We must regain the lock before handling interruption requests */
        } while (regain == -1);
 
@@ -1317,12 +1398,16 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
 
        LOCK_DEBUG (g_message ("%s: (%d) Regained %p lock %p", __func__, mono_thread_info_get_small_id (), obj, mon));
 
-       if (ret == WAIT_TIMEOUT) {
+       if (ret == MONO_W32HANDLE_WAIT_RET_TIMEOUT) {
                /* Poll the event again, just in case it was signalled
                 * while we were trying to regain the monitor lock
                 */
                MONO_ENTER_GC_SAFE;
-               ret = WaitForSingleObjectEx (event, 0, FALSE);
+#ifdef HOST_WIN32
+               ret = mono_w32handle_convert_wait_ret (WaitForSingleObjectEx (event, 0, FALSE), 1);
+#else
+               ret = mono_w32handle_wait_one (event, 0, FALSE);
+#endif /* HOST_WIN32 */
                MONO_EXIT_GC_SAFE;
        }
 
@@ -1336,7 +1421,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
         * thread.
         */
        
-       if (ret == WAIT_OBJECT_0) {
+       if (ret == MONO_W32HANDLE_WAIT_RET_SUCCESS_0) {
                LOCK_DEBUG (g_message ("%s: (%d) Success", __func__, mono_thread_info_get_small_id ()));
                success = TRUE;
        } else {
@@ -1346,7 +1431,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
                 */
                mon->wait_list = g_slist_remove (mon->wait_list, event);
        }
-       CloseHandle (event);
+       mono_w32event_close (event);
        
        return success;
 }