* Author:
* Dick Porter (dick@ximian.com)
*
- * (C) 2002 Ximian, Inc.
+ * (C) 2002-2006 Ximian, Inc.
*/
#include <config.h>
#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>
+#include <mono/io-layer/io-trace.h>
+#include <mono/utils/mono-once.h>
+#include <mono/utils/mono-logger-internals.h>
-#undef DEBUG
-
-static void mutex_close (gpointer handle);
static void mutex_signal(gpointer handle);
static gboolean mutex_own (gpointer handle);
static gboolean mutex_is_owned (gpointer handle);
-static void namedmutex_close (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 = {
- mutex_close, /* close */
+ 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)
}
struct _WapiHandleOps _wapi_namedmutex_ops = {
- namedmutex_close, /* close */
+ NULL, /* close */
namedmutex_signal, /* signal */
namedmutex_own, /* own */
namedmutex_is_owned, /* is_owned */
+ NULL, /* special_wait */
+ namedmutex_prewait /* prewait */
};
static gboolean mutex_release (gpointer handle);
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);
-}
-
-static void mutex_close (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;
- }
-
-#ifdef DEBUG
- g_message("%s: closing mutex handle %p", __func__, handle);
-#endif
-
- /* If its a shared one, clear the name */
+ (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL | WAPI_HANDLE_CAP_OWN));
}
static void mutex_signal(gpointer handle)
struct _WapiHandle_mutex *mutex_handle;
gboolean ok;
- ok=_wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
- (gpointer *)&mutex_handle);
- if(ok==FALSE) {
+ 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);
}
+
+ _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);
- 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__,
+ 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);
}
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 == 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 ())) {
+ 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 {
-#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
+ 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_close (gpointer handle)
-{
- /**/
-}
-
static void namedmutex_signal (gpointer handle)
{
- /**/
+ ReleaseMutex(handle);
}
+/* NB, always called with the shared handle lock held */
static gboolean namedmutex_own (gpointer handle)
{
- struct _WapiHandleShared shared_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_copy_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
- &shared_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);
}
- namedmutex_handle = &shared_handle.u.namedmutex;
- namedmutex_handle->pid = getpid ();
+ _wapi_thread_own_mutex (handle);
+
+ namedmutex_handle->pid = _wapi_getpid ();
namedmutex_handle->tid = pthread_self ();
namedmutex_handle->recursion++;
- ok = _wapi_replace_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
- &shared_handle);
- if (ok == FALSE) {
- SetLastError (ERROR_OUTOFMEMORY);
- return (FALSE);
- }
-
_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);
}
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 == 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 ())) {
+ 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 {
-#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
+ 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);
}
}
-struct mutex_check_data
+/* The shared state is not locked when prewait methods are called */
+static void namedmutex_prewait (gpointer handle)
{
- pid_t pid;
- pthread_t tid;
-};
+ /* 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;
+ }
+
+ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Checking ownership of named mutex handle %p", __func__,
+ handle);
-static gboolean mutex_check (gpointer handle, gpointer user_data)
+ 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;
+
+ 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 {
+ 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;
- struct mutex_check_data *data = (struct mutex_check_data *)user_data;
int thr_ret;
ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
if (ok == FALSE) {
g_warning ("%s: error looking up mutex handle %p", __func__,
handle);
- return(FALSE);
+ 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 == data->pid &&
- mutex_handle->tid == data->tid) {
-#ifdef DEBUG
- g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
-#endif
+ 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;
thr_ret = _wapi_handle_unlock_handle (handle);
g_assert (thr_ret == 0);
- pthread_cleanup_pop (0);
-
- /* Return false to keep searching */
- return(FALSE);
}
-static gboolean namedmutex_check (gpointer handle, gpointer user_data)
+static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
{
struct _WapiHandle_namedmutex *mutex_handle;
gboolean ok;
- struct mutex_check_data *data = (struct mutex_check_data *)user_data;
int thr_ret;
- guint32 now;
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);
+ return;
}
- thr_ret = _wapi_handle_shared_lock_handle (handle, &now);
+ thr_ret = _wapi_handle_lock_shared_handles ();
g_assert (thr_ret == 0);
- if (mutex_handle->pid == data->pid &&
- mutex_handle->tid == data->tid) {
-#ifdef DEBUG
- g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
-#endif
+ 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;
_wapi_shared_handle_set_signal_state (handle, TRUE);
}
- _wapi_handle_shared_unlock_handle (handle, now);
-
- /* Return false to keep searching */
- return(FALSE);
+ _wapi_handle_unlock_shared_handles ();
}
-/* When a thread exits, any mutexes it still holds need to be signalled */
-void _wapi_mutex_check_abandoned (pid_t pid, pthread_t tid)
+/* 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)
{
- struct mutex_check_data data;
+ WapiHandleType type = _wapi_handle_type (data);
- data.pid = pid;
- data.tid = tid;
-
- _wapi_search_handle (WAPI_HANDLE_MUTEX, mutex_check, &data, NULL);
- _wapi_search_handle (WAPI_HANDLE_NAMEDMUTEX, namedmutex_check, &data,
- NULL);
+ 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 ();
+ }
}
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) {
return(NULL);
}
- pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
- handle);
thr_ret = _wapi_handle_lock_handle (handle);
g_assert (thr_ret == 0);
_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);
}
gchar *utf8_name;
int thr_ret;
gpointer ret = NULL;
- guint32 now = (guint32)(time(NULL) & 0xFFFFFFFF), locknow;
guint32 namelen;
gint32 offset;
- /* w32 seems to guarantee that opening named mutexes can't
+ /* w32 seems to guarantee that opening named objects can't
* race each other
*/
- pthread_cleanup_push ((void(*)(void *))_wapi_namespace_timestamp_release, GUINT_TO_POINTER(now));
- thr_ret = _wapi_namespace_timestamp (now);
+ thr_ret = _wapi_namespace_lock ();
g_assert (thr_ret == 0);
/* Need to blow away any old errors here, because code tests
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);
SetLastError (ERROR_ALREADY_EXISTS);
}
/* Fall through to create the mutex handle. */
-
- if (strlen (utf8_name) < MAX_PATH) {
- namelen = strlen (utf8_name);
- } else {
- namelen = MAX_PATH;
- }
-
- memcpy (&namedmutex_handle.sharedns.name, utf8_name, namelen);
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;
+ }
+
+ memcpy (&namedmutex_handle.sharedns.name, utf8_name, namelen);
+
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_for_existing_ns (WAPI_HANDLE_NAMEDMUTEX, &namedmutex_handle, offset);
+ handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX,
+ offset, TRUE);
}
if (handle == _WAPI_HANDLE_INVALID) {
/* Set the initial state, as this is a completely new
* handle
*/
- thr_ret = _wapi_handle_shared_lock_handle (handle, &locknow);
+ thr_ret = _wapi_handle_lock_shared_handles ();
g_assert (thr_ret == 0);
if (owned == TRUE) {
_wapi_shared_handle_set_signal_state (handle, TRUE);
}
- _wapi_handle_shared_unlock_handle (handle, locknow);
+ _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);
- /* Releases the timestamp */
- pthread_cleanup_pop (1);
+
+ _wapi_namespace_unlock (NULL);
return(ret);
}
{
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;
- ok=_wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
- (gpointer *)&mutex_handle);
- if(ok==FALSE) {
+ 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_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(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) {
+ 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;
}
mutex_handle->recursion--;
if(mutex_handle->recursion==0) {
-#ifdef DEBUG
- g_message("%s: Unlocking mutex handle %p", __func__, handle);
-#endif
+ _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;
cleanup:
thr_ret = _wapi_handle_unlock_handle (handle);
g_assert (thr_ret == 0);
- pthread_cleanup_pop (0);
return(ret);
}
{
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;
- guint32 now;
ok=_wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
(gpointer *)&mutex_handle);
return(FALSE);
}
- thr_ret = _wapi_handle_shared_lock_handle (handle, &now);
+ 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(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) {
+ 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;
}
mutex_handle->recursion--;
if(mutex_handle->recursion==0) {
-#ifdef DEBUG
- g_message("%s: Unlocking mutex handle %p", __func__, handle);
-#endif
+ _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;
}
cleanup:
- _wapi_handle_shared_unlock_handle (handle, now);
+ _wapi_handle_unlock_shared_handles ();
return(ret);
}
return(mutex_ops[type].release (handle));
}
+
+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);
+
+ 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;
+ }
+
+ /* 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(ret);
+}