+/*
+ * mutexes.c: Mutex handles
+ *
+ * Author:
+ * Dick Porter (dick@ximian.com)
+ *
+ * (C) 2002-2006 Ximian, Inc.
+ */
+
#include <config.h>
#include <glib.h>
#include <pthread.h>
#include <string.h>
+#include <unistd.h>
+
+#include <mono/io-layer/wapi.h>
+#include <mono/io-layer/wapi-private.h>
+#include <mono/io-layer/handles-private.h>
+#include <mono/io-layer/mutex-private.h>
+#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);
+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 */
+};
-#include "mono/io-layer/wapi.h"
-#include "wapi-private.h"
-#include "wait-private.h"
-#include "misc-private.h"
-#include "handles-private.h"
+void _wapi_mutex_details (gpointer handle_info)
+{
+ struct _WapiHandle_mutex *mut = (struct _WapiHandle_mutex *)handle_info;
+
+#ifdef PTHREAD_POINTER_ID
+ g_print ("own: %5d:%5p, count: %5u", mut->pid, mut->tid,
+ mut->recursion);
+#else
+ g_print ("own: %5d:%5ld, count: %5u", mut->pid, mut->tid,
+ mut->recursion);
+#endif
+}
+
+struct _WapiHandleOps _wapi_namedmutex_ops = {
+ NULL, /* close */
+ namedmutex_signal, /* signal */
+ namedmutex_own, /* own */
+ namedmutex_is_owned, /* is_owned */
+ NULL, /* special_wait */
+ namedmutex_prewait /* prewait */
+};
-#undef DEBUG
+static gboolean mutex_release (gpointer handle);
+static gboolean namedmutex_release (gpointer handle);
-struct _WapiHandle_mutex
+static struct
{
- WapiHandle handle;
- pthread_mutex_t mutex;
- pthread_t tid;
- guint32 recursion;
+ gboolean (*release)(gpointer handle);
+} mutex_ops[WAPI_HANDLE_COUNT] = {
+ {NULL},
+ {NULL},
+ {NULL},
+ {NULL},
+ {NULL},
+ {mutex_release},
+ {NULL},
+ {NULL},
+ {NULL},
+ {NULL},
+ {NULL},
+ {namedmutex_release},
};
-static void mutex_close(WapiHandle *handle);
-static gboolean mutex_wait(WapiHandle *handle, WapiHandle *signal, guint32 ms);
-static guint32 mutex_wait_multiple(gpointer data);
-static void mutex_signal(WapiHandle *handle);
-
-static struct _WapiHandleOps mutex_ops = {
- mutex_close, /* close */
- NULL, /* getfiletype */
- NULL, /* readfile */
- NULL, /* writefile */
- NULL, /* seek */
- NULL, /* setendoffile */
- NULL, /* getfilesize */
- mutex_wait, /* wait */
- mutex_wait_multiple, /* wait_multiple */
- mutex_signal, /* signal */
-};
+static mono_once_t mutex_ops_once=MONO_ONCE_INIT;
-static void mutex_close(WapiHandle *handle)
+static void mutex_ops_init (void)
{
- struct _WapiHandle_mutex *mutex_handle=(struct _WapiHandle_mutex *)handle;
-
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": closing mutex handle %p",
- mutex_handle);
-#endif
+ _wapi_handle_register_capabilities (WAPI_HANDLE_MUTEX,
+ (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL | WAPI_HANDLE_CAP_OWN));
+ _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDMUTEX,
+ (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL | WAPI_HANDLE_CAP_OWN));
}
-static gboolean mutex_wait(WapiHandle *handle, WapiHandle *signal, guint32 ms)
+static void mutex_signal(gpointer handle)
{
- struct _WapiHandle_mutex *mutex_handle=(struct _WapiHandle_mutex *)handle;
- pthread_t tid=pthread_self();
- int ret;
-
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": waiting for mutex handle %p",
- mutex_handle);
-#endif
+ ReleaseMutex(handle);
+}
- /* Signal this handle now. It really doesn't matter if some
- * other thread grabs the mutex before we can
- */
- if(signal!=NULL) {
- signal->ops->signal(signal);
+static gboolean mutex_own (gpointer handle)
+{
+ struct _WapiHandle_mutex *mutex_handle;
+ gboolean ok;
+
+ ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
+ (gpointer *)&mutex_handle);
+ if (ok == FALSE) {
+ g_warning ("%s: error looking up mutex handle %p", __func__,
+ handle);
+ return(FALSE);
}
- if(mutex_handle->tid==tid) {
- /* We already own this mutex, so just increase the count and
- * return TRUE
- */
+ _wapi_thread_own_mutex (handle);
+
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning mutex handle %p", __func__, handle);
- mutex_handle->recursion++;
+ _wapi_handle_set_signal_state (handle, FALSE, FALSE);
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": Already own mutex handle %p (recursion %d)",
- mutex_handle, mutex_handle->recursion);
-#endif
+ mutex_handle->pid = _wapi_getpid ();
+ mutex_handle->tid = pthread_self ();
+ mutex_handle->recursion++;
- return(TRUE);
+ 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);
+
+ return(TRUE);
+}
+
+static gboolean mutex_is_owned (gpointer handle)
+{
+ struct _WapiHandle_mutex *mutex_handle;
+ gboolean ok;
+
+ ok=_wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
+ (gpointer *)&mutex_handle);
+ if(ok==FALSE) {
+ g_warning ("%s: error looking up mutex handle %p", __func__,
+ handle);
+ return(FALSE);
}
- if(ms==INFINITE) {
- ret=pthread_mutex_lock(&mutex_handle->mutex);
+ 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 ())) {
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p owned by %d:%ld", __func__,
+ handle, _wapi_getpid (), pthread_self ());
+
+ return(TRUE);
} else {
- struct timespec timeout;
-
- _wapi_calc_timeout(&timeout, ms);
-
- ret=pthread_mutex_timedlock(&mutex_handle->mutex, &timeout);
+ 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);
+ }
+}
+
+static void namedmutex_signal (gpointer handle)
+{
+ ReleaseMutex(handle);
+}
+
+/* NB, always called with the shared handle lock held */
+static gboolean namedmutex_own (gpointer handle)
+{
+ struct _WapiHandle_namedmutex *namedmutex_handle;
+ gboolean ok;
+
+ 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);
+ if (ok == FALSE) {
+ g_warning ("%s: error looking up named mutex handle %p",
+ __func__, handle);
+ return(FALSE);
}
- if(ret==0) {
- /* Mutex locked */
+ _wapi_thread_own_mutex (handle);
+
+ namedmutex_handle->pid = _wapi_getpid ();
+ namedmutex_handle->tid = pthread_self ();
+ namedmutex_handle->recursion++;
+
+ _wapi_shared_handle_set_signal_state (handle, FALSE);
+
+ 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);
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": Locking mutex handle %p",
- mutex_handle);
-#endif
+ return(TRUE);
+}
+
+static gboolean namedmutex_is_owned (gpointer handle)
+{
+ 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 mutex handle %p", __func__,
+ handle);
+ return(FALSE);
+ }
+
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: testing ownership mutex handle %p", __func__, handle);
- mutex_handle->tid=tid;
- mutex_handle->recursion=1;
+ if (namedmutex_handle->recursion > 0 &&
+ namedmutex_handle->pid == _wapi_getpid () &&
+ pthread_equal (namedmutex_handle->tid, pthread_self ())) {
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p owned by %d:%ld", __func__,
+ handle, _wapi_getpid (), pthread_self ());
return(TRUE);
} else {
- /* ret might be ETIMEDOUT for timeout, or other for error */
-
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": Failed to lock mutex handle %p: %s", mutex_handle,
- strerror(ret));
-#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);
}
}
-static guint32 mutex_wait_multiple(gpointer data G_GNUC_UNUSED)
+/* The shared state is not locked when prewait methods are called */
+static void namedmutex_prewait (gpointer handle)
{
- WaitQueueItem *item=(WaitQueueItem *)data;
- GPtrArray *needed;
- int ret;
- guint32 numhandles;
- struct timespec timeout;
- pthread_t tid=pthread_self();
- guint32 i, iterations;
-
- numhandles=item->handles[WAPI_HANDLE_MUTEX]->len;
-
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": waiting on %d mutex handles for %d ms", numhandles,
- item->timeout);
-#endif
-
- /*
- * See which ones we need to lock
+ /* 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.
*/
- needed=g_ptr_array_new();
- for(i=0; i<numhandles; i++) {
- struct _WapiHandle_mutex *mutex_handle;
-
- mutex_handle=g_ptr_array_index(
- item->handles[WAPI_HANDLE_MUTEX], i);
-
- if(mutex_handle->tid!=tid) {
- /* We don't have this one, so add it to the list */
- g_ptr_array_add(needed, mutex_handle);
- }
+ 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;
}
-
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": need to lock %d mutex handles",
- needed->len);
-#endif
- iterations=0;
- do {
- iterations++;
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Checking ownership of named mutex handle %p", __func__,
+ handle);
+
+ if (namedmutex_handle->recursion == 0) {
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Named mutex handle %p not owned", __func__,
+ handle);
+ } else if (namedmutex_handle->pid == _wapi_getpid ()) {
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Named mutex handle %p owned by this process",
+ __func__, handle);
+ } else {
+ int thr_ret;
+ gpointer proc_handle;
- /* If the timeout isnt INFINITE but greater than 1s,
- * split the timeout into 1s chunks
- */
- if((item->timeout!=INFINITE) &&
- (item->timeout < (iterations*1000))) {
- _wapi_calc_timeout(
- &timeout, item->timeout-((iterations-1)*1000));
+ 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
+ */
+ 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 {
- _wapi_calc_timeout(&timeout, 1000);
+ 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);
}
+ if (proc_handle != NULL)
+ CloseProcess (proc_handle);
+ }
+}
+
+static void mutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
+{
+ struct _WapiHandle_mutex *mutex_handle;
+ gboolean ok;
+ int thr_ret;
- /* Try and lock as many mutexes as we can until we run
- * out of time, but to avoid deadlocks back off if we
- * fail to lock one
- */
- for(i=0; i<needed->len; i++) {
- struct _WapiHandle_mutex *mutex_handle;
+ ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
+ (gpointer *)&mutex_handle);
+ if (ok == FALSE) {
+ g_warning ("%s: error looking up mutex handle %p", __func__,
+ handle);
+ return;
+ }
- mutex_handle=g_ptr_array_index(needed, i);
+ thr_ret = _wapi_handle_lock_handle (handle);
+ g_assert (thr_ret == 0);
+
+ if (mutex_handle->pid == pid &&
+ pthread_equal (mutex_handle->tid, tid)) {
+ 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;
+ mutex_handle->tid = 0;
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": Locking %d mutex %p (owner %ld, me %ld)",
- i, mutex_handle, mutex_handle->tid, tid);
-#endif
+ _wapi_handle_set_signal_state (handle, TRUE, FALSE);
+ }
- ret=pthread_mutex_timedlock(&mutex_handle->mutex,
- &timeout);
+ thr_ret = _wapi_handle_unlock_handle (handle);
+ g_assert (thr_ret == 0);
+}
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": timedlock ret %s",
- strerror(ret));
-#endif
+static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
+{
+ struct _WapiHandle_namedmutex *mutex_handle;
+ gboolean ok;
+ int thr_ret;
+
+ ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
+ (gpointer *)&mutex_handle);
+ if (ok == FALSE) {
+ g_warning ("%s: error looking up named mutex handle %p",
+ __func__, handle);
+ return;
+ }
- if(ret!=0) {
- /* ETIMEDOUT is the most likely, but
- * fail on other error too
- */
+ thr_ret = _wapi_handle_lock_shared_handles ();
+ g_assert (thr_ret == 0);
+
+ if (mutex_handle->pid == pid &&
+ pthread_equal (mutex_handle->tid, tid)) {
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Mutex handle %p abandoned!", __func__, handle);
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": Lock %d mutex failed: %s", i,
- strerror(ret));
-#endif
+ mutex_handle->recursion = 0;
+ mutex_handle->pid = 0;
+ mutex_handle->tid = 0;
+
+ _wapi_shared_handle_set_signal_state (handle, TRUE);
+ }
- while(i--) {
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": Releasing %d mutex", i);
-#endif
- mutex_handle=g_ptr_array_index(needed,
- i);
- pthread_mutex_unlock(
- &mutex_handle->mutex);
- }
+ _wapi_handle_unlock_shared_handles ();
+}
- break;
- }
+/* When a thread exits, any mutexes it still holds need to be
+ * signalled. This function must not be called with the shared handle
+ * lock held, as namedmutex_abandon () will try to acquire it
+ */
+void _wapi_mutex_abandon (gpointer data, pid_t pid, pthread_t tid)
+{
+ WapiHandleType type = _wapi_handle_type (data);
- /* OK, got that one. Don't record it as ours
- * though until we get them all
- */
- }
+ if (type == WAPI_HANDLE_MUTEX) {
+ mutex_abandon (data, pid, tid);
+ } else if (type == WAPI_HANDLE_NAMEDMUTEX) {
+ namedmutex_abandon (data, pid, tid);
+ } else {
+ g_assert_not_reached ();
+ }
+}
- if(i==needed->len) {
- /* We've locked all the mutexes. Update the
- * ones we already had, and record that the
- * new ones belong to us
- */
- for(i=0; i<numhandles; i++) {
- struct _WapiHandle_mutex *mutex_handle;
- guint32 idx;
+static gpointer mutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
+ gboolean owned)
+{
+ struct _WapiHandle_mutex mutex_handle = {0};
+ gpointer handle;
+ int thr_ret;
+
+ /* Need to blow away any old errors here, because code tests
+ * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
+ * was freshly created
+ */
+ SetLastError (ERROR_SUCCESS);
+
+ 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) {
+ g_warning ("%s: error creating mutex handle", __func__);
+ SetLastError (ERROR_GEN_FAILURE);
+ return(NULL);
+ }
- mutex_handle=g_ptr_array_index(
- item->handles[WAPI_HANDLE_MUTEX], i);
-
- idx=g_array_index(
- item->waitindex[WAPI_HANDLE_MUTEX],
- guint32, i);
- _wapi_handle_set_lowest(item, idx);
-
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": Updating mutex %p", mutex_handle);
-#endif
-
- if(mutex_handle->tid==tid) {
- /* We already own this mutex,
- * so just increase the count
- */
- mutex_handle->recursion++;
- } else {
- mutex_handle->tid=tid;
- mutex_handle->recursion=1;
- }
- }
-
- g_ptr_array_free(needed, FALSE);
+ thr_ret = _wapi_handle_lock_handle (handle);
+ g_assert (thr_ret == 0);
+
+ if(owned==TRUE) {
+ mutex_own (handle);
+ } else {
+ _wapi_handle_set_signal_state (handle, TRUE, FALSE);
+ }
+
+ 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);
+
+ return(handle);
+}
+
+static gpointer namedmutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
+ const gunichar2 *name)
+{
+ struct _WapiHandle_namedmutex namedmutex_handle = {{{0}}, 0};
+ gpointer handle;
+ gchar *utf8_name;
+ int thr_ret;
+ gpointer ret = NULL;
+ guint32 namelen;
+ gint32 offset;
+
+ /* w32 seems to guarantee that opening named objects can't
+ * race each other
+ */
+ thr_ret = _wapi_namespace_lock ();
+ g_assert (thr_ret == 0);
+
+ /* Need to blow away any old errors here, because code tests
+ * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
+ * was freshly created
+ */
+ SetLastError (ERROR_SUCCESS);
+
+ utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
+
+ 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);
+ if (offset == -1) {
+ /* The name has already been used for a different
+ * object.
+ */
+ SetLastError (ERROR_INVALID_HANDLE);
+ goto cleanup;
+ } else if (offset != 0) {
+ /* Not an error, but this is how the caller is
+ * informed that the mutex wasn't freshly created
+ */
+ SetLastError (ERROR_ALREADY_EXISTS);
+ }
+ /* Fall through to create the mutex handle. */
- item->waited[WAPI_HANDLE_MUTEX]=TRUE;
- item->waitcount[WAPI_HANDLE_MUTEX]=numhandles;
-
- return(numhandles);
+ if (offset == 0) {
+ /* A new named mutex, so create both the private and
+ * shared parts
+ */
+
+ if (strlen (utf8_name) < MAX_PATH) {
+ namelen = strlen (utf8_name);
+ } else {
+ namelen = MAX_PATH;
}
- } while((item->timeout==INFINITE) ||
- (item->timeout > (iterations * 1000)));
+
+ memcpy (&namedmutex_handle.sharedns.name, utf8_name, namelen);
- /* Didn't get all the locks, and timeout isn't INFINITE */
-
- g_ptr_array_free(needed, FALSE);
+ handle = _wapi_handle_new (WAPI_HANDLE_NAMEDMUTEX,
+ &namedmutex_handle);
+ } else {
+ /* A new reference to an existing named mutex, so just
+ * create the private part
+ */
+ handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX,
+ offset, TRUE);
+ }
+
+ if (handle == _WAPI_HANDLE_INVALID) {
+ g_warning ("%s: error creating mutex handle", __func__);
+ SetLastError (ERROR_GEN_FAILURE);
+ goto cleanup;
+ }
+ ret = handle;
- item->waited[WAPI_HANDLE_MUTEX]=TRUE;
- item->waitcount[WAPI_HANDLE_MUTEX]=0;
+ if (offset == 0) {
+ /* Set the initial state, as this is a completely new
+ * handle
+ */
+ thr_ret = _wapi_handle_lock_shared_handles ();
+ g_assert (thr_ret == 0);
- return(0);
-}
+ if (owned == TRUE) {
+ namedmutex_own (handle);
+ } else {
+ _wapi_shared_handle_set_signal_state (handle, TRUE);
+ }
-static void mutex_signal(WapiHandle *handle)
-{
- ReleaseMutex(handle);
+ _wapi_handle_unlock_shared_handles ();
+ }
+
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning mutex handle %p", __func__, handle);
+
+cleanup:
+ g_free (utf8_name);
+
+ _wapi_namespace_unlock (NULL);
+
+ return(ret);
}
/**
* @owned: If %TRUE, the mutex is created with the calling thread
* already owning the mutex.
* @name:Pointer to a string specifying the name of this mutex, or
- * %NULL. Currently ignored.
+ * %NULL.
*
* Creates a new mutex handle. A mutex is signalled when no thread
* owns it. A thread acquires ownership of the mutex by waiting for
*
* Return value: A new handle, or %NULL on error.
*/
-WapiHandle *CreateMutex(WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned G_GNUC_UNUSED,
- const guchar *name G_GNUC_UNUSED)
+gpointer CreateMutex(WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
+ const gunichar2 *name)
+{
+ mono_once (&mutex_ops_once, mutex_ops_init);
+
+ if (name == NULL) {
+ return(mutex_create (security, owned));
+ } else {
+ return(namedmutex_create (security, owned, name));
+ }
+}
+
+static gboolean mutex_release (gpointer handle)
{
struct _WapiHandle_mutex *mutex_handle;
- WapiHandle *handle;
+ gboolean ok;
+ pthread_t tid = pthread_self ();
+ pid_t pid = _wapi_getpid ();
+ int thr_ret;
+ gboolean ret = FALSE;
- mutex_handle=(struct _WapiHandle_mutex *)g_new0(struct _WapiHandle_mutex, 1);
- handle=(WapiHandle *)mutex_handle;
- _WAPI_HANDLE_INIT(handle, WAPI_HANDLE_MUTEX, mutex_ops);
+ ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
+ (gpointer *)&mutex_handle);
+ if (ok == FALSE) {
+ g_warning ("%s: error looking up mutex handle %p", __func__,
+ handle);
+ return(FALSE);
+ }
- pthread_mutex_init(&mutex_handle->mutex, NULL);
- if(owned==TRUE) {
- pthread_t tid=pthread_self();
-
- pthread_mutex_lock(&mutex_handle->mutex);
-
- mutex_handle->tid=tid;
- mutex_handle->recursion=1;
+ thr_ret = _wapi_handle_lock_handle (handle);
+ g_assert (thr_ret == 0);
+
+ 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) {
+ 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;
}
+ ret = TRUE;
- return(handle);
+ /* OK, we own this mutex */
+ mutex_handle->recursion--;
+
+ if(mutex_handle->recursion==0) {
+ _wapi_thread_disown_mutex (handle);
+
+ 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;
+ _wapi_handle_set_signal_state (handle, TRUE, FALSE);
+ }
+
+cleanup:
+ thr_ret = _wapi_handle_unlock_handle (handle);
+ g_assert (thr_ret == 0);
+
+ return(ret);
+}
+
+static gboolean namedmutex_release (gpointer handle)
+{
+ struct _WapiHandle_namedmutex *mutex_handle;
+ gboolean ok;
+ pthread_t tid = pthread_self ();
+ pid_t pid = _wapi_getpid ();
+ int thr_ret;
+ gboolean ret = FALSE;
+
+ ok=_wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
+ (gpointer *)&mutex_handle);
+ if(ok==FALSE) {
+ g_warning ("%s: error looking up named mutex handle %p",
+ __func__, handle);
+ return(FALSE);
+ }
+
+ thr_ret = _wapi_handle_lock_shared_handles ();
+ g_assert (thr_ret == 0);
+
+ 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) {
+ 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;
+ }
+ ret = TRUE;
+
+ /* OK, we own this mutex */
+ mutex_handle->recursion--;
+
+ if(mutex_handle->recursion==0) {
+ _wapi_thread_disown_mutex (handle);
+
+ 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;
+ _wapi_shared_handle_set_signal_state (handle, TRUE);
+ }
+
+cleanup:
+ _wapi_handle_unlock_shared_handles ();
+
+ return(ret);
}
/**
* Return value: %TRUE on success, %FALSE otherwise. This function
* fails if the calling thread does not own the mutex @handle.
*/
-gboolean ReleaseMutex(WapiHandle *handle)
+gboolean ReleaseMutex(gpointer handle)
{
- struct _WapiHandle_mutex *mutex_handle=(struct _WapiHandle_mutex *)handle;
- pthread_t tid=pthread_self();
-
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": Releasing mutex handle %p",
- mutex_handle);
-#endif
-
- if(mutex_handle->tid!=tid) {
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": We don't own mutex handle %p (owned by %ld, me %ld)", mutex_handle, mutex_handle->tid, tid);
-#endif
+ WapiHandleType type;
+ if (handle == NULL) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(FALSE);
+ }
+
+ type = _wapi_handle_type (handle);
+
+ if (mutex_ops[type].release == NULL) {
+ SetLastError (ERROR_INVALID_HANDLE);
return(FALSE);
}
+
+ return(mutex_ops[type].release (handle));
+}
- /* OK, we own this mutex */
- mutex_handle->recursion--;
+gpointer OpenMutex (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, const gunichar2 *name)
+{
+ gpointer handle;
+ gchar *utf8_name;
+ int thr_ret;
+ gpointer ret = NULL;
+ gint32 offset;
+
+ mono_once (&mutex_ops_once, mutex_ops_init);
+
+ /* w32 seems to guarantee that opening named objects can't
+ * race each other
+ */
+ thr_ret = _wapi_namespace_lock ();
+ g_assert (thr_ret == 0);
+
+ utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
- if(mutex_handle->recursion==0) {
-#ifdef DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": Unlocking mutex handle %p",
- mutex_handle);
-#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);
+ if (offset == -1) {
+ /* The name has already been used for a different
+ * object.
+ */
+ SetLastError (ERROR_INVALID_HANDLE);
+ goto cleanup;
+ } else if (offset == 0) {
+ /* This name doesn't exist */
+ SetLastError (ERROR_FILE_NOT_FOUND); /* yes, really */
+ goto cleanup;
+ }
- mutex_handle->tid=0;
- pthread_mutex_unlock(&mutex_handle->mutex);
+ /* A new reference to an existing named mutex, so just create
+ * the private part
+ */
+ handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX, offset,
+ TRUE);
+
+ if (handle == _WAPI_HANDLE_INVALID) {
+ g_warning ("%s: error opening named mutex handle", __func__);
+ SetLastError (ERROR_GEN_FAILURE);
+ goto cleanup;
}
+ ret = handle;
+
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named mutex handle %p", __func__, handle);
+
+cleanup:
+ g_free (utf8_name);
+
+ _wapi_namespace_unlock (NULL);
- return(TRUE);
+ return(ret);
}