Merge pull request #2675 from lambdageek/dev/monoerror-mono_string_intern
[mono.git] / mono / io-layer / mutexes.c
index e4f51eb6280fe41f2b01ee3246a1913c4a727763..730363e6302b1fa16a42da9722eb0690ea970402 100644 (file)
 
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
-#include <mono/io-layer/misc-private.h>
 #include <mono/io-layer/handles-private.h>
-#include <mono/io-layer/mono-mutex.h>
 #include <mono/io-layer/mutex-private.h>
-
-#undef DEBUG
+#include <mono/io-layer/io-trace.h>
+#include <mono/utils/mono-once.h>
+#include <mono/utils/mono-logger-internals.h>
 
 static void mutex_signal(gpointer handle);
 static gboolean mutex_own (gpointer handle);
@@ -88,13 +87,9 @@ static mono_once_t mutex_ops_once=MONO_ONCE_INIT;
 static void mutex_ops_init (void)
 {
        _wapi_handle_register_capabilities (WAPI_HANDLE_MUTEX,
-                                           WAPI_HANDLE_CAP_WAIT |
-                                           WAPI_HANDLE_CAP_SIGNAL |
-                                           WAPI_HANDLE_CAP_OWN);
+               (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL | WAPI_HANDLE_CAP_OWN));
        _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDMUTEX,
-                                           WAPI_HANDLE_CAP_WAIT |
-                                           WAPI_HANDLE_CAP_SIGNAL |
-                                           WAPI_HANDLE_CAP_OWN);
+               (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL | WAPI_HANDLE_CAP_OWN));
 }
 
 static void mutex_signal(gpointer handle)
@@ -117,9 +112,7 @@ static gboolean mutex_own (gpointer handle)
 
        _wapi_thread_own_mutex (handle);
        
-#ifdef DEBUG
-       g_message("%s: owning mutex handle %p", __func__, handle);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning mutex handle %p", __func__, handle);
 
        _wapi_handle_set_signal_state (handle, FALSE, FALSE);
        
@@ -127,11 +120,9 @@ static gboolean mutex_own (gpointer handle)
        mutex_handle->tid = pthread_self ();
        mutex_handle->recursion++;
 
-#ifdef DEBUG
-       g_message ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p locked %d times by %d:%ld", __func__,
                   handle, mutex_handle->recursion, mutex_handle->pid,
                   mutex_handle->tid);
-#endif
 
        return(TRUE);
 }
@@ -149,23 +140,17 @@ static gboolean mutex_is_owned (gpointer handle)
                return(FALSE);
        }
        
-#ifdef DEBUG
-       g_message("%s: testing ownership mutex handle %p", __func__, handle);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: testing ownership mutex handle %p", __func__, handle);
 
        if (mutex_handle->recursion > 0 &&
            mutex_handle->pid == _wapi_getpid () &&
            pthread_equal (mutex_handle->tid, pthread_self ())) {
-#ifdef DEBUG
-               g_message ("%s: mutex handle %p owned by %d:%ld", __func__,
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p owned by %d:%ld", __func__,
                           handle, _wapi_getpid (), pthread_self ());
-#endif
 
                return(TRUE);
        } else {
-#ifdef DEBUG
-               g_message ("%s: mutex handle %p not owned by %d:%ld, but locked %d times by %d:%ld", __func__, handle, _wapi_getpid (), pthread_self (), mutex_handle->recursion, mutex_handle->pid, mutex_handle->tid);
-#endif
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p not owned by %d:%ld, but locked %d times by %d:%ld", __func__, handle, _wapi_getpid (), pthread_self (), mutex_handle->recursion, mutex_handle->pid, mutex_handle->tid);
 
                return(FALSE);
        }
@@ -182,9 +167,7 @@ static gboolean namedmutex_own (gpointer handle)
        struct _WapiHandle_namedmutex *namedmutex_handle;
        gboolean ok;
        
-#ifdef DEBUG
-       g_message ("%s: owning named mutex handle %p", __func__, handle);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning named mutex handle %p", __func__, handle);
        
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
                                  (gpointer *)&namedmutex_handle);
@@ -202,11 +185,9 @@ static gboolean namedmutex_own (gpointer handle)
 
        _wapi_shared_handle_set_signal_state (handle, FALSE);
 
-#ifdef DEBUG
-       g_message ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p locked %d times by %d:%ld", __func__,
                   handle, namedmutex_handle->recursion,
                   namedmutex_handle->pid, namedmutex_handle->tid);
-#endif
        
        return(TRUE);
 }
@@ -224,23 +205,17 @@ static gboolean namedmutex_is_owned (gpointer handle)
                return(FALSE);
        }
        
-#ifdef DEBUG
-       g_message ("%s: testing ownership mutex handle %p", __func__, handle);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: testing ownership mutex handle %p", __func__, handle);
 
        if (namedmutex_handle->recursion > 0 &&
            namedmutex_handle->pid == _wapi_getpid () &&
            pthread_equal (namedmutex_handle->tid, pthread_self ())) {
-#ifdef DEBUG
-               g_message ("%s: mutex handle %p owned by %d:%ld", __func__,
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p owned by %d:%ld", __func__,
                           handle, _wapi_getpid (), pthread_self ());
-#endif
 
                return(TRUE);
        } else {
-#ifdef DEBUG
-               g_message ("%s: mutex handle %p not owned by %d:%ld, but locked %d times by %d:%ld", __func__, handle, _wapi_getpid (), pthread_self (), namedmutex_handle->recursion, namedmutex_handle->pid, namedmutex_handle->tid);
-#endif
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p not owned by %d:%ld, but locked %d times by %d:%ld", __func__, handle, _wapi_getpid (), pthread_self (), namedmutex_handle->recursion, namedmutex_handle->pid, namedmutex_handle->tid);
 
                return(FALSE);
        }
@@ -266,90 +241,40 @@ static void namedmutex_prewait (gpointer handle)
                return;
        }
        
-#ifdef DEBUG
-       g_message ("%s: Checking ownership of named mutex handle %p", __func__,
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Checking ownership of named mutex handle %p", __func__,
                   handle);
-#endif
 
        if (namedmutex_handle->recursion == 0) {
-#ifdef DEBUG
-               g_message ("%s: Named mutex handle %p not owned", __func__,
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Named mutex handle %p not owned", __func__,
                           handle);
-#endif
        } else if (namedmutex_handle->pid == _wapi_getpid ()) {
-#ifdef DEBUG
-               g_message ("%s: Named mutex handle %p owned by this process",
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Named mutex handle %p owned by this process",
                           __func__, handle);
-#endif
        } else {
-               guint32 *pids = g_new0 (guint32, 32);
-               guint32 count = 32, needed_bytes, i;
-               gboolean ret;
                int thr_ret;
+               gpointer proc_handle;
                
-#ifdef DEBUG
-               g_message ("%s: Named mutex handle %p owned by another process", __func__, handle);
-#endif
-               
-               ret = EnumProcesses (pids, count * sizeof(guint32),
-                                    &needed_bytes);
-               if (ret == FALSE) {
-                       do {
-                               count = needed_bytes / sizeof(guint32);
-#ifdef DEBUG
-                               g_message ("%s: Retrying pid lookup with %d slots", __func__, count);
-#endif
-                               pids = g_renew (guint32, pids, count);
-                               ret = EnumProcesses (pids, needed_bytes,
-                                                    &needed_bytes);
-                       } while (ret == FALSE);
-               }
-
-               count = needed_bytes / sizeof(guint32);
-
-#ifdef DEBUG
-               g_message ("%s: Need to look at %d pids for named mutex handle %p", __func__, count, handle);
-#endif
-
-               thr_ret = _wapi_handle_lock_shared_handles ();
-               g_assert (thr_ret == 0);
-
-               for (i = 0; i < count; i++) {
-#ifdef DEBUG
-                       g_message ("%s: Checking pid %d for named mutex handle %p", __func__, pids[i], handle);
-#endif
-
-                       if (pids[i] == namedmutex_handle->pid) {
-                               /* Must be still alive, because
-                                * EnumProcesses() checks for us
-                                */
-#ifdef DEBUG
-                               g_message ("%s: Found active pid %d for named mutex handle %p", __func__, pids[i], handle);
-#endif
-
-                               break;
-                       }
-               }
-               
-               g_free (pids);
-
-               if (i == count) {
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Named mutex handle %p owned by another process", __func__, handle);
+               proc_handle = OpenProcess (0, 0, namedmutex_handle->pid);
+               if (proc_handle == NULL) {
                        /* Didn't find the process that this handle
                         * was owned by, overriding it
                         */
-
-#ifdef DEBUG
-                       g_message ("%s: overriding old owner of named mutex handle %p", __func__, handle);
-#endif
+                       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: overriding old owner of named mutex handle %p", __func__, handle);
+                       thr_ret = _wapi_handle_lock_shared_handles ();
+                       g_assert (thr_ret == 0);
 
                        namedmutex_handle->pid = 0;
                        namedmutex_handle->tid = 0;
                        namedmutex_handle->recursion = 0;
 
                        _wapi_shared_handle_set_signal_state (handle, TRUE);
+                       _wapi_handle_unlock_shared_handles ();
+               } else {
+                       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Found active pid %d for named mutex handle %p", __func__, namedmutex_handle->pid, handle);
                }
-
-               _wapi_handle_unlock_shared_handles ();
+               if (proc_handle != NULL)
+                       CloseProcess (proc_handle);
        }
 }
 
@@ -367,16 +292,12 @@ static void mutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
                return;
        }
 
-       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
-                             handle);
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
        
        if (mutex_handle->pid == pid &&
            pthread_equal (mutex_handle->tid, tid)) {
-#ifdef DEBUG
-               g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
-#endif
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Mutex handle %p abandoned!", __func__, handle);
 
                mutex_handle->recursion = 0;
                mutex_handle->pid = 0;
@@ -387,7 +308,6 @@ static void mutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
 
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 }
 
 static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
@@ -409,9 +329,7 @@ static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
        
        if (mutex_handle->pid == pid &&
            pthread_equal (mutex_handle->tid, tid)) {
-#ifdef DEBUG
-               g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
-#endif
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Mutex handle %p abandoned!", __func__, handle);
 
                mutex_handle->recursion = 0;
                mutex_handle->pid = 0;
@@ -453,9 +371,7 @@ static gpointer mutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
         */
        SetLastError (ERROR_SUCCESS);
        
-#ifdef DEBUG
-       g_message ("%s: Creating unnamed mutex", __func__);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating unnamed mutex", __func__);
        
        handle = _wapi_handle_new (WAPI_HANDLE_MUTEX, &mutex_handle);
        if (handle == _WAPI_HANDLE_INVALID) {
@@ -464,8 +380,6 @@ static gpointer mutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
                return(NULL);
        }
 
-       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
-                             handle);
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
        
@@ -475,13 +389,10 @@ static gpointer mutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
                _wapi_handle_set_signal_state (handle, TRUE, FALSE);
        }
        
-#ifdef DEBUG
-       g_message ("%s: returning mutex handle %p", __func__, handle);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning mutex handle %p", __func__, handle);
 
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
        
        return(handle);
 }
@@ -511,9 +422,7 @@ static gpointer namedmutex_create (WapiSecurityAttributes *security G_GNUC_UNUSE
        
        utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
        
-#ifdef DEBUG
-       g_message ("%s: Creating named mutex [%s]", __func__, utf8_name);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating named mutex [%s]", __func__, utf8_name);
        
        offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
                                                utf8_name);
@@ -577,9 +486,7 @@ static gpointer namedmutex_create (WapiSecurityAttributes *security G_GNUC_UNUSE
                _wapi_handle_unlock_shared_handles ();
        }
        
-#ifdef DEBUG
-       g_message ("%s: returning mutex handle %p", __func__, handle);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning mutex handle %p", __func__, handle);
 
 cleanup:
        g_free (utf8_name);
@@ -637,20 +544,14 @@ static gboolean mutex_release (gpointer handle)
                return(FALSE);
        }
 
-       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
-                             handle);
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
        
-#ifdef DEBUG
-       g_message("%s: Releasing mutex handle %p", __func__, handle);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Releasing mutex handle %p", __func__, handle);
 
        if (!pthread_equal (mutex_handle->tid, tid) ||
            mutex_handle->pid != pid) {
-#ifdef DEBUG
-               g_message("%s: We don't own mutex handle %p (owned by %d:%ld, me %d:%ld)", __func__, handle, mutex_handle->pid, mutex_handle->tid, _wapi_getpid (), tid);
-#endif
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: We don't own mutex handle %p (owned by %d:%ld, me %d:%ld)", __func__, handle, mutex_handle->pid, mutex_handle->tid, _wapi_getpid (), tid);
 
                goto cleanup;
        }
@@ -662,9 +563,7 @@ static gboolean mutex_release (gpointer handle)
        if(mutex_handle->recursion==0) {
                _wapi_thread_disown_mutex (handle);
 
-#ifdef DEBUG
-               g_message("%s: Unlocking mutex handle %p", __func__, handle);
-#endif
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking mutex handle %p", __func__, handle);
 
                mutex_handle->pid=0;
                mutex_handle->tid=0;
@@ -674,7 +573,6 @@ static gboolean mutex_release (gpointer handle)
 cleanup:
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
        
        return(ret);
 }
@@ -699,15 +597,11 @@ static gboolean namedmutex_release (gpointer handle)
        thr_ret = _wapi_handle_lock_shared_handles ();
        g_assert (thr_ret == 0);
        
-#ifdef DEBUG
-       g_message("%s: Releasing mutex handle %p", __func__, handle);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Releasing mutex handle %p", __func__, handle);
 
        if (!pthread_equal (mutex_handle->tid, tid) ||
            mutex_handle->pid != pid) {
-#ifdef DEBUG
-               g_message("%s: We don't own mutex handle %p (owned by %d:%ld, me %d:%ld)", __func__, handle, mutex_handle->pid, mutex_handle->tid, _wapi_getpid (), tid);
-#endif
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: We don't own mutex handle %p (owned by %d:%ld, me %d:%ld)", __func__, handle, mutex_handle->pid, mutex_handle->tid, _wapi_getpid (), tid);
 
                goto cleanup;
        }
@@ -719,9 +613,7 @@ static gboolean namedmutex_release (gpointer handle)
        if(mutex_handle->recursion==0) {
                _wapi_thread_disown_mutex (handle);
 
-#ifdef DEBUG
-               g_message("%s: Unlocking mutex handle %p", __func__, handle);
-#endif
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking mutex handle %p", __func__, handle);
 
                mutex_handle->pid=0;
                mutex_handle->tid=0;
@@ -780,9 +672,7 @@ gpointer OpenMutex (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED
 
        utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
        
-#ifdef DEBUG
-       g_message ("%s: Opening named mutex [%s]", __func__, utf8_name);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named mutex [%s]", __func__, utf8_name);
        
        offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
                                                utf8_name);
@@ -811,9 +701,7 @@ gpointer OpenMutex (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED
        }
        ret = handle;
 
-#ifdef DEBUG
-       g_message ("%s: returning named mutex handle %p", __func__, handle);
-#endif
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named mutex handle %p", __func__, handle);
 
 cleanup:
        g_free (utf8_name);