Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / metadata / monitor.c
index dd2cc0eb9a7186b6a6d77471adeb1a914570bef9..d3b51abfc80247f536fdb2410f28edf528411200 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * monitor.c:  Monitor locking functions
+/**
+ * \file
+ * Monitor locking functions
  *
  * Author:
  *     Dick Porter (dick@ximian.com)
@@ -31,6 +32,7 @@
 #include <mono/utils/mono-time.h>
 #include <mono/utils/atomic.h>
 #include <mono/utils/w32api.h>
+#include <mono/utils/mono-os-wait.h>
 
 /*
  * Pull the list of opcodes
@@ -307,10 +309,9 @@ monitor_is_on_freelist (MonoThreadsSync *mon)
 
 /**
  * mono_locks_dump:
- * @include_untaken:
- *
+ * \param include_untaken Whether to list unheld inflated locks.
  * Print a report on stdout of the managed locks currently held by
- * threads. If @include_untaken is specified, list also inflated locks
+ * threads. If \p include_untaken is specified, list also inflated locks
  * which are unheld.
  * This is supposed to be used in debuggers like gdb.
  */
@@ -373,7 +374,7 @@ mon_finalize (MonoThreadsSync *mon)
        mon->data = monitor_freelist;
        monitor_freelist = mon;
 #ifndef DISABLE_PERFCOUNTERS
-       mono_perfcounters->gc_sync_blocks--;
+       InterlockedDecrement (&mono_perfcounters->gc_sync_blocks);
 #endif
 }
 
@@ -445,7 +446,7 @@ mon_new (gsize id)
        new_->data = NULL;
        
 #ifndef DISABLE_PERFCOUNTERS
-       mono_perfcounters->gc_sync_blocks++;
+       InterlockedIncrement (&mono_perfcounters->gc_sync_blocks);
 #endif
        return new_;
 }
@@ -792,7 +793,7 @@ retry:
 
        /* The object must be locked by someone else... */
 #ifndef DISABLE_PERFCOUNTERS
-       mono_perfcounters->thread_contentions++;
+       InterlockedIncrement (&mono_perfcounters->thread_contentions);
 #endif
 
        /* If ms is 0 we don't block, but just fail straight away */
@@ -801,7 +802,7 @@ retry:
                return 0;
        }
 
-       mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_CONTENTION);
+       MONO_PROFILER_RAISE (monitor_contention, (obj));
 
        /* The slow path begins here. */
 retry_contended:
@@ -824,7 +825,7 @@ retry_contended:
                if (G_LIKELY (tmp_status == old_status)) {
                        /* Success */
                        g_assert (mon->nest == 1);
-                       mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_DONE);
+                       MONO_PROFILER_RAISE (monitor_acquired, (obj));
                        return 1;
                }
        }
@@ -832,7 +833,7 @@ retry_contended:
        /* If the object is currently locked by this thread... */
        if (mon_status_get_owner (old_status) == id) {
                mon->nest++;
-               mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_DONE);
+               MONO_PROFILER_RAISE (monitor_acquired, (obj));
                return 1;
        }
 
@@ -874,8 +875,8 @@ retry_contended:
        waitms = ms;
        
 #ifndef DISABLE_PERFCOUNTERS
-       mono_perfcounters->thread_queue_len++;
-       mono_perfcounters->thread_queue_max++;
+       InterlockedIncrement (&mono_perfcounters->thread_queue_len);
+       InterlockedIncrement (&mono_perfcounters->thread_queue_max);
 #endif
        thread = mono_thread_internal_current ();
 
@@ -891,7 +892,7 @@ retry_contended:
         *
         */
        if (allow_interruption) {
-               if (!mono_thread_test_and_set_state (thread, (MonoThreadState)(ThreadState_StopRequested | ThreadState_AbortRequested), ThreadState_WaitSleepJoin)) {
+               if (!mono_thread_test_and_set_state (thread, ThreadState_AbortRequested, ThreadState_WaitSleepJoin)) {
                        wait_ret = MONO_SEM_TIMEDWAIT_RET_ALERTED;
                        goto done_waiting;
                }
@@ -909,7 +910,7 @@ retry_contended:
 
 done_waiting:
 #ifndef DISABLE_PERFCOUNTERS
-       mono_perfcounters->thread_queue_len--;
+       InterlockedDecrement (&mono_perfcounters->thread_queue_len);
 #endif
 
        if (wait_ret == MONO_SEM_TIMEDWAIT_RET_ALERTED && !allow_interruption) {
@@ -918,7 +919,7 @@ done_waiting:
                 * 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 (!mono_thread_test_state (mono_thread_internal_current (), ThreadState_SuspendRequested | ThreadState_AbortRequested)) {
                        if (ms != MONO_INFINITE_WAIT) {
                                now = mono_msec_ticks ();
 
@@ -946,7 +947,7 @@ done_waiting:
        /* Timed out or interrupted */
        mon_decrement_entry_count (mon);
 
-       mono_profiler_monitor_event (obj, MONO_PROFILER_MONITOR_FAIL);
+       MONO_PROFILER_RAISE (monitor_failed, (obj));
 
        if (wait_ret == MONO_SEM_TIMEDWAIT_RET_ALERTED) {
                LOCK_DEBUG (g_message ("%s: (%d) interrupted waiting, returning -1", __func__, id));
@@ -1049,6 +1050,9 @@ mono_monitor_enter_internal (MonoObject *obj)
        return TRUE;
 }
 
+/**
+ * mono_monitor_enter:
+ */
 gboolean
 mono_monitor_enter (MonoObject *obj)
 {
@@ -1067,6 +1071,9 @@ mono_monitor_enter_fast (MonoObject *obj)
        return mono_monitor_try_enter_internal (obj, 0, FALSE) == 1;
 }
 
+/**
+ * mono_monitor_try_enter:
+ */
 gboolean
 mono_monitor_try_enter (MonoObject *obj, guint32 ms)
 {
@@ -1077,6 +1084,9 @@ mono_monitor_try_enter (MonoObject *obj, guint32 ms)
        return mono_monitor_try_enter_internal (obj, ms, FALSE) == 1;
 }
 
+/**
+ * mono_monitor_exit:
+ */
 void
 mono_monitor_exit (MonoObject *obj)
 {
@@ -1160,6 +1170,9 @@ ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (MonoObject
        *lockTaken = res == 1;
 }
 
+/**
+ * mono_monitor_enter_v4:
+ */
 void
 mono_monitor_enter_v4 (MonoObject *obj, char *lock_taken)
 {
@@ -1375,7 +1388,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
         */
        MONO_ENTER_GC_SAFE;
 #ifdef HOST_WIN32
-       ret = mono_w32handle_convert_wait_ret (WaitForSingleObjectEx (event, ms, TRUE), 1);
+       ret = mono_w32handle_convert_wait_ret (mono_win32_wait_for_single_object_ex (event, ms, TRUE), 1);
 #else
        ret = mono_w32handle_wait_one (event, ms, TRUE);
 #endif /* HOST_WIN32 */
@@ -1404,7 +1417,7 @@ ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
                 */
                MONO_ENTER_GC_SAFE;
 #ifdef HOST_WIN32
-               ret = mono_w32handle_convert_wait_ret (WaitForSingleObjectEx (event, 0, FALSE), 1);
+               ret = mono_w32handle_convert_wait_ret (mono_win32_wait_for_single_object_ex (event, 0, FALSE), 1);
 #else
                ret = mono_w32handle_wait_one (event, 0, FALSE);
 #endif /* HOST_WIN32 */