2 * mutexes.c: Mutex handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Ximian, Inc.
16 #include <mono/io-layer/wapi.h>
17 #include <mono/io-layer/wapi-private.h>
18 #include <mono/io-layer/handles-private.h>
19 #include <mono/io-layer/mutex-private.h>
20 #include <mono/io-layer/io-trace.h>
21 #include <mono/utils/mono-once.h>
22 #include <mono/utils/mono-logger-internals.h>
24 static void mutex_signal(gpointer handle);
25 static gboolean mutex_own (gpointer handle);
26 static gboolean mutex_is_owned (gpointer handle);
28 static void namedmutex_signal (gpointer handle);
29 static gboolean namedmutex_own (gpointer handle);
30 static gboolean namedmutex_is_owned (gpointer handle);
31 static void namedmutex_prewait (gpointer handle);
33 struct _WapiHandleOps _wapi_mutex_ops = {
35 mutex_signal, /* signal */
37 mutex_is_owned, /* is_owned */
38 NULL, /* special_wait */
42 void _wapi_mutex_details (gpointer handle_info)
44 struct _WapiHandle_mutex *mut = (struct _WapiHandle_mutex *)handle_info;
46 #ifdef PTHREAD_POINTER_ID
47 g_print ("own: %5d:%5p, count: %5u", mut->pid, mut->tid,
50 g_print ("own: %5d:%5ld, count: %5u", mut->pid, mut->tid,
55 struct _WapiHandleOps _wapi_namedmutex_ops = {
57 namedmutex_signal, /* signal */
58 namedmutex_own, /* own */
59 namedmutex_is_owned, /* is_owned */
60 NULL, /* special_wait */
61 namedmutex_prewait /* prewait */
64 static gboolean mutex_release (gpointer handle);
65 static gboolean namedmutex_release (gpointer handle);
69 gboolean (*release)(gpointer handle);
70 } mutex_ops[WAPI_HANDLE_COUNT] = {
85 static mono_once_t mutex_ops_once=MONO_ONCE_INIT;
87 static void mutex_ops_init (void)
89 _wapi_handle_register_capabilities (WAPI_HANDLE_MUTEX,
90 (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL | WAPI_HANDLE_CAP_OWN));
91 _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDMUTEX,
92 (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL | WAPI_HANDLE_CAP_OWN));
95 static void mutex_signal(gpointer handle)
100 static gboolean mutex_own (gpointer handle)
102 struct _WapiHandle_mutex *mutex_handle;
105 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
106 (gpointer *)&mutex_handle);
108 g_warning ("%s: error looking up mutex handle %p", __func__,
113 _wapi_thread_own_mutex (handle);
115 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning mutex handle %p", __func__, handle);
117 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
119 mutex_handle->pid = _wapi_getpid ();
120 mutex_handle->tid = pthread_self ();
121 mutex_handle->recursion++;
123 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p locked %d times by %d:%ld", __func__,
124 handle, mutex_handle->recursion, mutex_handle->pid,
130 static gboolean mutex_is_owned (gpointer handle)
132 struct _WapiHandle_mutex *mutex_handle;
135 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
136 (gpointer *)&mutex_handle);
138 g_warning ("%s: error looking up mutex handle %p", __func__,
143 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: testing ownership mutex handle %p", __func__, handle);
145 if (mutex_handle->recursion > 0 &&
146 mutex_handle->pid == _wapi_getpid () &&
147 pthread_equal (mutex_handle->tid, pthread_self ())) {
148 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p owned by %d:%ld", __func__,
149 handle, _wapi_getpid (), pthread_self ());
153 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);
159 static void namedmutex_signal (gpointer handle)
161 ReleaseMutex(handle);
164 /* NB, always called with the shared handle lock held */
165 static gboolean namedmutex_own (gpointer handle)
167 struct _WapiHandle_namedmutex *namedmutex_handle;
170 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning named mutex handle %p", __func__, handle);
172 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
173 (gpointer *)&namedmutex_handle);
175 g_warning ("%s: error looking up named mutex handle %p",
180 _wapi_thread_own_mutex (handle);
182 namedmutex_handle->pid = _wapi_getpid ();
183 namedmutex_handle->tid = pthread_self ();
184 namedmutex_handle->recursion++;
186 _wapi_shared_handle_set_signal_state (handle, FALSE);
188 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p locked %d times by %d:%ld", __func__,
189 handle, namedmutex_handle->recursion,
190 namedmutex_handle->pid, namedmutex_handle->tid);
195 static gboolean namedmutex_is_owned (gpointer handle)
197 struct _WapiHandle_namedmutex *namedmutex_handle;
200 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
201 (gpointer *)&namedmutex_handle);
203 g_warning ("%s: error looking up mutex handle %p", __func__,
208 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: testing ownership mutex handle %p", __func__, handle);
210 if (namedmutex_handle->recursion > 0 &&
211 namedmutex_handle->pid == _wapi_getpid () &&
212 pthread_equal (namedmutex_handle->tid, pthread_self ())) {
213 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: mutex handle %p owned by %d:%ld", __func__,
214 handle, _wapi_getpid (), pthread_self ());
218 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);
224 /* The shared state is not locked when prewait methods are called */
225 static void namedmutex_prewait (gpointer handle)
227 /* If the mutex is not currently owned, do nothing and let the
228 * usual wait carry on. If it is owned, check that the owner
229 * is still alive; if it isn't we override the previous owner
230 * and assume that process exited abnormally and failed to
233 struct _WapiHandle_namedmutex *namedmutex_handle;
236 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
237 (gpointer *)&namedmutex_handle);
239 g_warning ("%s: error looking up named mutex handle %p",
244 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Checking ownership of named mutex handle %p", __func__,
247 if (namedmutex_handle->recursion == 0) {
248 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Named mutex handle %p not owned", __func__,
250 } else if (namedmutex_handle->pid == _wapi_getpid ()) {
251 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Named mutex handle %p owned by this process",
255 gpointer proc_handle;
257 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Named mutex handle %p owned by another process", __func__, handle);
258 proc_handle = OpenProcess (0, 0, namedmutex_handle->pid);
259 if (proc_handle == NULL) {
260 /* Didn't find the process that this handle
261 * was owned by, overriding it
263 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: overriding old owner of named mutex handle %p", __func__, handle);
264 thr_ret = _wapi_handle_lock_shared_handles ();
265 g_assert (thr_ret == 0);
267 namedmutex_handle->pid = 0;
268 namedmutex_handle->tid = 0;
269 namedmutex_handle->recursion = 0;
271 _wapi_shared_handle_set_signal_state (handle, TRUE);
272 _wapi_handle_unlock_shared_handles ();
274 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);
276 if (proc_handle != NULL)
277 CloseProcess (proc_handle);
281 static void mutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
283 struct _WapiHandle_mutex *mutex_handle;
287 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
288 (gpointer *)&mutex_handle);
290 g_warning ("%s: error looking up mutex handle %p", __func__,
295 thr_ret = _wapi_handle_lock_handle (handle);
296 g_assert (thr_ret == 0);
298 if (mutex_handle->pid == pid &&
299 pthread_equal (mutex_handle->tid, tid)) {
300 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Mutex handle %p abandoned!", __func__, handle);
302 mutex_handle->recursion = 0;
303 mutex_handle->pid = 0;
304 mutex_handle->tid = 0;
306 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
309 thr_ret = _wapi_handle_unlock_handle (handle);
310 g_assert (thr_ret == 0);
313 static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
315 struct _WapiHandle_namedmutex *mutex_handle;
319 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
320 (gpointer *)&mutex_handle);
322 g_warning ("%s: error looking up named mutex handle %p",
327 thr_ret = _wapi_handle_lock_shared_handles ();
328 g_assert (thr_ret == 0);
330 if (mutex_handle->pid == pid &&
331 pthread_equal (mutex_handle->tid, tid)) {
332 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Mutex handle %p abandoned!", __func__, handle);
334 mutex_handle->recursion = 0;
335 mutex_handle->pid = 0;
336 mutex_handle->tid = 0;
338 _wapi_shared_handle_set_signal_state (handle, TRUE);
341 _wapi_handle_unlock_shared_handles ();
344 /* When a thread exits, any mutexes it still holds need to be
345 * signalled. This function must not be called with the shared handle
346 * lock held, as namedmutex_abandon () will try to acquire it
348 void _wapi_mutex_abandon (gpointer data, pid_t pid, pthread_t tid)
350 WapiHandleType type = _wapi_handle_type (data);
352 if (type == WAPI_HANDLE_MUTEX) {
353 mutex_abandon (data, pid, tid);
354 } else if (type == WAPI_HANDLE_NAMEDMUTEX) {
355 namedmutex_abandon (data, pid, tid);
357 g_assert_not_reached ();
361 static gpointer mutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
364 struct _WapiHandle_mutex mutex_handle = {0};
368 /* Need to blow away any old errors here, because code tests
369 * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
370 * was freshly created
372 SetLastError (ERROR_SUCCESS);
374 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating unnamed mutex", __func__);
376 handle = _wapi_handle_new (WAPI_HANDLE_MUTEX, &mutex_handle);
377 if (handle == _WAPI_HANDLE_INVALID) {
378 g_warning ("%s: error creating mutex handle", __func__);
379 SetLastError (ERROR_GEN_FAILURE);
383 thr_ret = _wapi_handle_lock_handle (handle);
384 g_assert (thr_ret == 0);
389 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
392 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning mutex handle %p", __func__, handle);
394 thr_ret = _wapi_handle_unlock_handle (handle);
395 g_assert (thr_ret == 0);
400 static gpointer namedmutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
401 const gunichar2 *name)
403 struct _WapiHandle_namedmutex namedmutex_handle = {{{0}}, 0};
411 /* w32 seems to guarantee that opening named objects can't
414 thr_ret = _wapi_namespace_lock ();
415 g_assert (thr_ret == 0);
417 /* Need to blow away any old errors here, because code tests
418 * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
419 * was freshly created
421 SetLastError (ERROR_SUCCESS);
423 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
425 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating named mutex [%s]", __func__, utf8_name);
427 offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
430 /* The name has already been used for a different
433 SetLastError (ERROR_INVALID_HANDLE);
435 } else if (offset != 0) {
436 /* Not an error, but this is how the caller is
437 * informed that the mutex wasn't freshly created
439 SetLastError (ERROR_ALREADY_EXISTS);
441 /* Fall through to create the mutex handle. */
444 /* A new named mutex, so create both the private and
448 if (strlen (utf8_name) < MAX_PATH) {
449 namelen = strlen (utf8_name);
454 memcpy (&namedmutex_handle.sharedns.name, utf8_name, namelen);
456 handle = _wapi_handle_new (WAPI_HANDLE_NAMEDMUTEX,
459 /* A new reference to an existing named mutex, so just
460 * create the private part
462 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX,
466 if (handle == _WAPI_HANDLE_INVALID) {
467 g_warning ("%s: error creating mutex handle", __func__);
468 SetLastError (ERROR_GEN_FAILURE);
474 /* Set the initial state, as this is a completely new
477 thr_ret = _wapi_handle_lock_shared_handles ();
478 g_assert (thr_ret == 0);
481 namedmutex_own (handle);
483 _wapi_shared_handle_set_signal_state (handle, TRUE);
486 _wapi_handle_unlock_shared_handles ();
489 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning mutex handle %p", __func__, handle);
494 _wapi_namespace_unlock (NULL);
501 * @security: Ignored for now.
502 * @owned: If %TRUE, the mutex is created with the calling thread
503 * already owning the mutex.
504 * @name:Pointer to a string specifying the name of this mutex, or
507 * Creates a new mutex handle. A mutex is signalled when no thread
508 * owns it. A thread acquires ownership of the mutex by waiting for
509 * it with WaitForSingleObject() or WaitForMultipleObjects(). A
510 * thread relinquishes ownership with ReleaseMutex().
512 * A thread that owns a mutex can specify the same mutex in repeated
513 * wait function calls without blocking. The thread must call
514 * ReleaseMutex() an equal number of times to release the mutex.
516 * Return value: A new handle, or %NULL on error.
518 gpointer CreateMutex(WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
519 const gunichar2 *name)
521 mono_once (&mutex_ops_once, mutex_ops_init);
524 return(mutex_create (security, owned));
526 return(namedmutex_create (security, owned, name));
530 static gboolean mutex_release (gpointer handle)
532 struct _WapiHandle_mutex *mutex_handle;
534 pthread_t tid = pthread_self ();
535 pid_t pid = _wapi_getpid ();
537 gboolean ret = FALSE;
539 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
540 (gpointer *)&mutex_handle);
542 g_warning ("%s: error looking up mutex handle %p", __func__,
547 thr_ret = _wapi_handle_lock_handle (handle);
548 g_assert (thr_ret == 0);
550 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Releasing mutex handle %p", __func__, handle);
552 if (!pthread_equal (mutex_handle->tid, tid) ||
553 mutex_handle->pid != pid) {
554 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);
560 /* OK, we own this mutex */
561 mutex_handle->recursion--;
563 if(mutex_handle->recursion==0) {
564 _wapi_thread_disown_mutex (handle);
566 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking mutex handle %p", __func__, handle);
570 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
574 thr_ret = _wapi_handle_unlock_handle (handle);
575 g_assert (thr_ret == 0);
580 static gboolean namedmutex_release (gpointer handle)
582 struct _WapiHandle_namedmutex *mutex_handle;
584 pthread_t tid = pthread_self ();
585 pid_t pid = _wapi_getpid ();
587 gboolean ret = FALSE;
589 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
590 (gpointer *)&mutex_handle);
592 g_warning ("%s: error looking up named mutex handle %p",
597 thr_ret = _wapi_handle_lock_shared_handles ();
598 g_assert (thr_ret == 0);
600 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Releasing mutex handle %p", __func__, handle);
602 if (!pthread_equal (mutex_handle->tid, tid) ||
603 mutex_handle->pid != pid) {
604 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);
610 /* OK, we own this mutex */
611 mutex_handle->recursion--;
613 if(mutex_handle->recursion==0) {
614 _wapi_thread_disown_mutex (handle);
616 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking mutex handle %p", __func__, handle);
620 _wapi_shared_handle_set_signal_state (handle, TRUE);
624 _wapi_handle_unlock_shared_handles ();
631 * @handle: The mutex handle.
633 * Releases ownership if the mutex handle @handle.
635 * Return value: %TRUE on success, %FALSE otherwise. This function
636 * fails if the calling thread does not own the mutex @handle.
638 gboolean ReleaseMutex(gpointer handle)
642 if (handle == NULL) {
643 SetLastError (ERROR_INVALID_HANDLE);
647 type = _wapi_handle_type (handle);
649 if (mutex_ops[type].release == NULL) {
650 SetLastError (ERROR_INVALID_HANDLE);
654 return(mutex_ops[type].release (handle));
657 gpointer OpenMutex (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, const gunichar2 *name)
665 mono_once (&mutex_ops_once, mutex_ops_init);
667 /* w32 seems to guarantee that opening named objects can't
670 thr_ret = _wapi_namespace_lock ();
671 g_assert (thr_ret == 0);
673 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
675 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named mutex [%s]", __func__, utf8_name);
677 offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
680 /* The name has already been used for a different
683 SetLastError (ERROR_INVALID_HANDLE);
685 } else if (offset == 0) {
686 /* This name doesn't exist */
687 SetLastError (ERROR_FILE_NOT_FOUND); /* yes, really */
691 /* A new reference to an existing named mutex, so just create
694 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX, offset,
697 if (handle == _WAPI_HANDLE_INVALID) {
698 g_warning ("%s: error opening named mutex handle", __func__);
699 SetLastError (ERROR_GEN_FAILURE);
704 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named mutex handle %p", __func__, handle);
709 _wapi_namespace_unlock (NULL);