Merge pull request #760 from mayerwin/patch-2
[mono.git] / mono / io-layer / mutexes.c
index 1204723defafe483429a4cb9f5934768ce692f7d..7ef9c43e24b7df29ba52e572aceb9acf1fa0e07f 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
+ * (C) 2002-2006 Ximian, Inc.
  */
 
 #include <config.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>
+#include <mono/utils/mono-mutex.h>
 
-#undef DEBUG
+#if 0
+#define DEBUG(...) g_message(__VA_ARGS__)
+#else
+#define DEBUG(...)
+#endif
 
 static void mutex_signal(gpointer handle);
 static gboolean mutex_own (gpointer handle);
@@ -29,12 +33,15 @@ static gboolean mutex_is_owned (gpointer handle);
 static void namedmutex_signal (gpointer handle);
 static gboolean namedmutex_own (gpointer handle);
 static gboolean namedmutex_is_owned (gpointer handle);
+static void namedmutex_prewait (gpointer handle);
 
 struct _WapiHandleOps _wapi_mutex_ops = {
        NULL,                   /* close */
        mutex_signal,           /* signal */
        mutex_own,              /* own */
        mutex_is_owned,         /* is_owned */
+       NULL,                   /* special_wait */
+       NULL                    /* prewait */
 };
 
 void _wapi_mutex_details (gpointer handle_info)
@@ -55,6 +62,8 @@ struct _WapiHandleOps _wapi_namedmutex_ops = {
        namedmutex_signal,      /* signal */
        namedmutex_own,         /* own */
        namedmutex_is_owned,    /* is_owned */
+       NULL,                   /* special_wait */
+       namedmutex_prewait      /* prewait */
 };
 
 static gboolean mutex_release (gpointer handle);
@@ -112,21 +121,17 @@ static gboolean mutex_own (gpointer handle)
 
        _wapi_thread_own_mutex (handle);
        
-#ifdef DEBUG
-       g_message("%s: owning mutex handle %p", __func__, handle);
-#endif
+       DEBUG("%s: owning mutex handle %p", __func__, handle);
 
        _wapi_handle_set_signal_state (handle, FALSE, FALSE);
        
-       mutex_handle->pid = getpid ();
+       mutex_handle->pid = _wapi_getpid ();
        mutex_handle->tid = pthread_self ();
        mutex_handle->recursion++;
 
-#ifdef DEBUG
-       g_message ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
+       DEBUG ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
                   handle, mutex_handle->recursion, mutex_handle->pid,
                   mutex_handle->tid);
-#endif
 
        return(TRUE);
 }
@@ -144,23 +149,17 @@ static gboolean mutex_is_owned (gpointer handle)
                return(FALSE);
        }
        
-#ifdef DEBUG
-       g_message("%s: testing ownership mutex handle %p", __func__, handle);
-#endif
+       DEBUG("%s: testing ownership mutex handle %p", __func__, handle);
 
        if (mutex_handle->recursion > 0 &&
-           mutex_handle->pid == getpid () &&
-           mutex_handle->tid == pthread_self ()) {
-#ifdef DEBUG
-               g_message ("%s: mutex handle %p owned by %d:%ld", __func__,
-                          handle, getpid (), pthread_self ());
-#endif
+           mutex_handle->pid == _wapi_getpid () &&
+           pthread_equal (mutex_handle->tid, pthread_self ())) {
+               DEBUG ("%s: mutex handle %p owned by %d:%ld", __func__,
+                          handle, _wapi_getpid (), pthread_self ());
 
                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, getpid (), pthread_self (), mutex_handle->recursion, mutex_handle->pid, mutex_handle->tid);
-#endif
+               DEBUG ("%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);
        }
@@ -177,9 +176,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
+       DEBUG ("%s: owning named mutex handle %p", __func__, handle);
        
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
                                  (gpointer *)&namedmutex_handle);
@@ -191,17 +188,15 @@ static gboolean namedmutex_own (gpointer handle)
 
        _wapi_thread_own_mutex (handle);
 
-       namedmutex_handle->pid = getpid ();
+       namedmutex_handle->pid = _wapi_getpid ();
        namedmutex_handle->tid = pthread_self ();
        namedmutex_handle->recursion++;
 
        _wapi_shared_handle_set_signal_state (handle, FALSE);
 
-#ifdef DEBUG
-       g_message ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
+       DEBUG ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
                   handle, namedmutex_handle->recursion,
                   namedmutex_handle->pid, namedmutex_handle->tid);
-#endif
        
        return(TRUE);
 }
@@ -219,28 +214,79 @@ static gboolean namedmutex_is_owned (gpointer handle)
                return(FALSE);
        }
        
-#ifdef DEBUG
-       g_message ("%s: testing ownership mutex handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: testing ownership mutex handle %p", __func__, handle);
 
        if (namedmutex_handle->recursion > 0 &&
-           namedmutex_handle->pid == getpid () &&
-           namedmutex_handle->tid == pthread_self ()) {
-#ifdef DEBUG
-               g_message ("%s: mutex handle %p owned by %d:%ld", __func__,
-                          handle, getpid (), pthread_self ());
-#endif
+           namedmutex_handle->pid == _wapi_getpid () &&
+           pthread_equal (namedmutex_handle->tid, pthread_self ())) {
+               DEBUG ("%s: mutex handle %p owned by %d:%ld", __func__,
+                          handle, _wapi_getpid (), pthread_self ());
 
                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, getpid (), pthread_self (), namedmutex_handle->recursion, namedmutex_handle->pid, namedmutex_handle->tid);
-#endif
+               DEBUG ("%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);
        }
 }
 
+/* The shared state is not locked when prewait methods are called */
+static void namedmutex_prewait (gpointer handle)
+{
+       /* If the mutex is not currently owned, do nothing and let the
+        * usual wait carry on.  If it is owned, check that the owner
+        * is still alive; if it isn't we override the previous owner
+        * and assume that process exited abnormally and failed to
+        * clean up.
+        */
+       struct _WapiHandle_namedmutex *namedmutex_handle;
+       gboolean ok;
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
+                                 (gpointer *)&namedmutex_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up named mutex handle %p",
+                          __func__, handle);
+               return;
+       }
+       
+       DEBUG ("%s: Checking ownership of named mutex handle %p", __func__,
+                  handle);
+
+       if (namedmutex_handle->recursion == 0) {
+               DEBUG ("%s: Named mutex handle %p not owned", __func__,
+                          handle);
+       } else if (namedmutex_handle->pid == _wapi_getpid ()) {
+               DEBUG ("%s: Named mutex handle %p owned by this process",
+                          __func__, handle);
+       } else {
+               int thr_ret;
+               gpointer proc_handle;
+               
+               DEBUG ("%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
+                        */
+                       DEBUG ("%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 {
+                       DEBUG ("%s: Found active pid %d for named mutex handle %p", __func__, namedmutex_handle->pid, handle);
+               }
+               if (proc_handle != NULL)
+                       CloseProcess (proc_handle);
+       }
+}
+
 static void mutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
 {
        struct _WapiHandle_mutex *mutex_handle;
@@ -261,10 +307,8 @@ static void mutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
        g_assert (thr_ret == 0);
        
        if (mutex_handle->pid == pid &&
-           mutex_handle->tid == tid) {
-#ifdef DEBUG
-               g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
-#endif
+           pthread_equal (mutex_handle->tid, tid)) {
+               DEBUG ("%s: Mutex handle %p abandoned!", __func__, handle);
 
                mutex_handle->recursion = 0;
                mutex_handle->pid = 0;
@@ -296,10 +340,8 @@ static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
        g_assert (thr_ret == 0);
        
        if (mutex_handle->pid == pid &&
-           mutex_handle->tid == tid) {
-#ifdef DEBUG
-               g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
-#endif
+           pthread_equal (mutex_handle->tid, tid)) {
+               DEBUG ("%s: Mutex handle %p abandoned!", __func__, handle);
 
                mutex_handle->recursion = 0;
                mutex_handle->pid = 0;
@@ -341,9 +383,7 @@ static gpointer mutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
         */
        SetLastError (ERROR_SUCCESS);
        
-#ifdef DEBUG
-       g_message ("%s: Creating unnamed mutex", __func__);
-#endif
+       DEBUG ("%s: Creating unnamed mutex", __func__);
        
        handle = _wapi_handle_new (WAPI_HANDLE_MUTEX, &mutex_handle);
        if (handle == _WAPI_HANDLE_INVALID) {
@@ -363,9 +403,7 @@ 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
+       DEBUG ("%s: returning mutex handle %p", __func__, handle);
 
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
@@ -399,9 +437,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
+       DEBUG ("%s: Creating named mutex [%s]", __func__, utf8_name);
        
        offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
                                                utf8_name);
@@ -465,9 +501,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
+       DEBUG ("%s: returning mutex handle %p", __func__, handle);
 
 cleanup:
        g_free (utf8_name);
@@ -512,8 +546,8 @@ static gboolean mutex_release (gpointer handle)
 {
        struct _WapiHandle_mutex *mutex_handle;
        gboolean ok;
-       pthread_t tid=pthread_self();
-       pid_t pid=getpid ();
+       pthread_t tid = pthread_self ();
+       pid_t pid = _wapi_getpid ();
        int thr_ret;
        gboolean ret = FALSE;
        
@@ -530,14 +564,11 @@ static gboolean mutex_release (gpointer 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
+       DEBUG("%s: Releasing mutex handle %p", __func__, handle);
 
-       if(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, pid, tid);
-#endif
+       if (!pthread_equal (mutex_handle->tid, tid) ||
+           mutex_handle->pid != pid) {
+               DEBUG("%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;
        }
@@ -549,9 +580,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
+               DEBUG("%s: Unlocking mutex handle %p", __func__, handle);
 
                mutex_handle->pid=0;
                mutex_handle->tid=0;
@@ -570,8 +599,8 @@ static gboolean namedmutex_release (gpointer handle)
 {
        struct _WapiHandle_namedmutex *mutex_handle;
        gboolean ok;
-       pthread_t tid=pthread_self();
-       pid_t pid=getpid ();
+       pthread_t tid = pthread_self ();
+       pid_t pid = _wapi_getpid ();
        int thr_ret;
        gboolean ret = FALSE;
        
@@ -586,14 +615,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
+       DEBUG("%s: Releasing mutex handle %p", __func__, handle);
 
-       if(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, pid, tid);
-#endif
+       if (!pthread_equal (mutex_handle->tid, tid) ||
+           mutex_handle->pid != pid) {
+               DEBUG("%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;
        }
@@ -605,9 +631,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
+               DEBUG("%s: Unlocking mutex handle %p", __func__, handle);
 
                mutex_handle->pid=0;
                mutex_handle->tid=0;
@@ -666,9 +690,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
+       DEBUG ("%s: Opening named mutex [%s]", __func__, utf8_name);
        
        offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
                                                utf8_name);
@@ -697,9 +719,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
+       DEBUG ("%s: returning named mutex handle %p", __func__, handle);
 
 cleanup:
        g_free (utf8_name);