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/misc-private.h>
19 #include <mono/io-layer/handles-private.h>
20 #include <mono/io-layer/mutex-private.h>
21 #include <mono/utils/mono-mutex.h>
24 #define DEBUG(...) g_message(__VA_ARGS__)
29 static void mutex_signal(gpointer handle);
30 static gboolean mutex_own (gpointer handle);
31 static gboolean mutex_is_owned (gpointer handle);
33 static void namedmutex_signal (gpointer handle);
34 static gboolean namedmutex_own (gpointer handle);
35 static gboolean namedmutex_is_owned (gpointer handle);
36 static void namedmutex_prewait (gpointer handle);
38 struct _WapiHandleOps _wapi_mutex_ops = {
40 mutex_signal, /* signal */
42 mutex_is_owned, /* is_owned */
43 NULL, /* special_wait */
47 void _wapi_mutex_details (gpointer handle_info)
49 struct _WapiHandle_mutex *mut = (struct _WapiHandle_mutex *)handle_info;
51 #ifdef PTHREAD_POINTER_ID
52 g_print ("own: %5d:%5p, count: %5u", mut->pid, mut->tid,
55 g_print ("own: %5d:%5ld, count: %5u", mut->pid, mut->tid,
60 struct _WapiHandleOps _wapi_namedmutex_ops = {
62 namedmutex_signal, /* signal */
63 namedmutex_own, /* own */
64 namedmutex_is_owned, /* is_owned */
65 NULL, /* special_wait */
66 namedmutex_prewait /* prewait */
69 static gboolean mutex_release (gpointer handle);
70 static gboolean namedmutex_release (gpointer handle);
74 gboolean (*release)(gpointer handle);
75 } mutex_ops[WAPI_HANDLE_COUNT] = {
90 static mono_once_t mutex_ops_once=MONO_ONCE_INIT;
92 static void mutex_ops_init (void)
94 _wapi_handle_register_capabilities (WAPI_HANDLE_MUTEX,
95 WAPI_HANDLE_CAP_WAIT |
96 WAPI_HANDLE_CAP_SIGNAL |
98 _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDMUTEX,
99 WAPI_HANDLE_CAP_WAIT |
100 WAPI_HANDLE_CAP_SIGNAL |
101 WAPI_HANDLE_CAP_OWN);
104 static void mutex_signal(gpointer handle)
106 ReleaseMutex(handle);
109 static gboolean mutex_own (gpointer handle)
111 struct _WapiHandle_mutex *mutex_handle;
114 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
115 (gpointer *)&mutex_handle);
117 g_warning ("%s: error looking up mutex handle %p", __func__,
122 _wapi_thread_own_mutex (handle);
124 DEBUG("%s: owning mutex handle %p", __func__, handle);
126 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
128 mutex_handle->pid = _wapi_getpid ();
129 mutex_handle->tid = pthread_self ();
130 mutex_handle->recursion++;
132 DEBUG ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
133 handle, mutex_handle->recursion, mutex_handle->pid,
139 static gboolean mutex_is_owned (gpointer handle)
141 struct _WapiHandle_mutex *mutex_handle;
144 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
145 (gpointer *)&mutex_handle);
147 g_warning ("%s: error looking up mutex handle %p", __func__,
152 DEBUG("%s: testing ownership mutex handle %p", __func__, handle);
154 if (mutex_handle->recursion > 0 &&
155 mutex_handle->pid == _wapi_getpid () &&
156 pthread_equal (mutex_handle->tid, pthread_self ())) {
157 DEBUG ("%s: mutex handle %p owned by %d:%ld", __func__,
158 handle, _wapi_getpid (), pthread_self ());
162 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);
168 static void namedmutex_signal (gpointer handle)
170 ReleaseMutex(handle);
173 /* NB, always called with the shared handle lock held */
174 static gboolean namedmutex_own (gpointer handle)
176 struct _WapiHandle_namedmutex *namedmutex_handle;
179 DEBUG ("%s: owning named mutex handle %p", __func__, handle);
181 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
182 (gpointer *)&namedmutex_handle);
184 g_warning ("%s: error looking up named mutex handle %p",
189 _wapi_thread_own_mutex (handle);
191 namedmutex_handle->pid = _wapi_getpid ();
192 namedmutex_handle->tid = pthread_self ();
193 namedmutex_handle->recursion++;
195 _wapi_shared_handle_set_signal_state (handle, FALSE);
197 DEBUG ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
198 handle, namedmutex_handle->recursion,
199 namedmutex_handle->pid, namedmutex_handle->tid);
204 static gboolean namedmutex_is_owned (gpointer handle)
206 struct _WapiHandle_namedmutex *namedmutex_handle;
209 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
210 (gpointer *)&namedmutex_handle);
212 g_warning ("%s: error looking up mutex handle %p", __func__,
217 DEBUG ("%s: testing ownership mutex handle %p", __func__, handle);
219 if (namedmutex_handle->recursion > 0 &&
220 namedmutex_handle->pid == _wapi_getpid () &&
221 pthread_equal (namedmutex_handle->tid, pthread_self ())) {
222 DEBUG ("%s: mutex handle %p owned by %d:%ld", __func__,
223 handle, _wapi_getpid (), pthread_self ());
227 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);
233 /* The shared state is not locked when prewait methods are called */
234 static void namedmutex_prewait (gpointer handle)
236 /* If the mutex is not currently owned, do nothing and let the
237 * usual wait carry on. If it is owned, check that the owner
238 * is still alive; if it isn't we override the previous owner
239 * and assume that process exited abnormally and failed to
242 struct _WapiHandle_namedmutex *namedmutex_handle;
245 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
246 (gpointer *)&namedmutex_handle);
248 g_warning ("%s: error looking up named mutex handle %p",
253 DEBUG ("%s: Checking ownership of named mutex handle %p", __func__,
256 if (namedmutex_handle->recursion == 0) {
257 DEBUG ("%s: Named mutex handle %p not owned", __func__,
259 } else if (namedmutex_handle->pid == _wapi_getpid ()) {
260 DEBUG ("%s: Named mutex handle %p owned by this process",
264 gpointer proc_handle;
266 DEBUG ("%s: Named mutex handle %p owned by another process", __func__, handle);
267 proc_handle = OpenProcess (0, 0, namedmutex_handle->pid);
268 if (proc_handle == NULL) {
269 /* Didn't find the process that this handle
270 * was owned by, overriding it
272 DEBUG ("%s: overriding old owner of named mutex handle %p", __func__, handle);
273 thr_ret = _wapi_handle_lock_shared_handles ();
274 g_assert (thr_ret == 0);
276 namedmutex_handle->pid = 0;
277 namedmutex_handle->tid = 0;
278 namedmutex_handle->recursion = 0;
280 _wapi_shared_handle_set_signal_state (handle, TRUE);
281 _wapi_handle_unlock_shared_handles ();
283 DEBUG ("%s: Found active pid %d for named mutex handle %p", __func__, namedmutex_handle->pid, handle);
285 if (proc_handle != NULL)
286 CloseProcess (proc_handle);
290 static void mutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
292 struct _WapiHandle_mutex *mutex_handle;
296 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
297 (gpointer *)&mutex_handle);
299 g_warning ("%s: error looking up mutex handle %p", __func__,
304 thr_ret = _wapi_handle_lock_handle (handle);
305 g_assert (thr_ret == 0);
307 if (mutex_handle->pid == pid &&
308 pthread_equal (mutex_handle->tid, tid)) {
309 DEBUG ("%s: Mutex handle %p abandoned!", __func__, handle);
311 mutex_handle->recursion = 0;
312 mutex_handle->pid = 0;
313 mutex_handle->tid = 0;
315 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
318 thr_ret = _wapi_handle_unlock_handle (handle);
319 g_assert (thr_ret == 0);
322 static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
324 struct _WapiHandle_namedmutex *mutex_handle;
328 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
329 (gpointer *)&mutex_handle);
331 g_warning ("%s: error looking up named mutex handle %p",
336 thr_ret = _wapi_handle_lock_shared_handles ();
337 g_assert (thr_ret == 0);
339 if (mutex_handle->pid == pid &&
340 pthread_equal (mutex_handle->tid, tid)) {
341 DEBUG ("%s: Mutex handle %p abandoned!", __func__, handle);
343 mutex_handle->recursion = 0;
344 mutex_handle->pid = 0;
345 mutex_handle->tid = 0;
347 _wapi_shared_handle_set_signal_state (handle, TRUE);
350 _wapi_handle_unlock_shared_handles ();
353 /* When a thread exits, any mutexes it still holds need to be
354 * signalled. This function must not be called with the shared handle
355 * lock held, as namedmutex_abandon () will try to acquire it
357 void _wapi_mutex_abandon (gpointer data, pid_t pid, pthread_t tid)
359 WapiHandleType type = _wapi_handle_type (data);
361 if (type == WAPI_HANDLE_MUTEX) {
362 mutex_abandon (data, pid, tid);
363 } else if (type == WAPI_HANDLE_NAMEDMUTEX) {
364 namedmutex_abandon (data, pid, tid);
366 g_assert_not_reached ();
370 static gpointer mutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
373 struct _WapiHandle_mutex mutex_handle = {0};
377 /* Need to blow away any old errors here, because code tests
378 * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
379 * was freshly created
381 SetLastError (ERROR_SUCCESS);
383 DEBUG ("%s: Creating unnamed mutex", __func__);
385 handle = _wapi_handle_new (WAPI_HANDLE_MUTEX, &mutex_handle);
386 if (handle == _WAPI_HANDLE_INVALID) {
387 g_warning ("%s: error creating mutex handle", __func__);
388 SetLastError (ERROR_GEN_FAILURE);
392 thr_ret = _wapi_handle_lock_handle (handle);
393 g_assert (thr_ret == 0);
398 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
401 DEBUG ("%s: returning mutex handle %p", __func__, handle);
403 thr_ret = _wapi_handle_unlock_handle (handle);
404 g_assert (thr_ret == 0);
409 static gpointer namedmutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
410 const gunichar2 *name)
412 struct _WapiHandle_namedmutex namedmutex_handle = {{{0}}, 0};
420 /* w32 seems to guarantee that opening named objects can't
423 thr_ret = _wapi_namespace_lock ();
424 g_assert (thr_ret == 0);
426 /* Need to blow away any old errors here, because code tests
427 * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
428 * was freshly created
430 SetLastError (ERROR_SUCCESS);
432 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
434 DEBUG ("%s: Creating named mutex [%s]", __func__, utf8_name);
436 offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
439 /* The name has already been used for a different
442 SetLastError (ERROR_INVALID_HANDLE);
444 } else if (offset != 0) {
445 /* Not an error, but this is how the caller is
446 * informed that the mutex wasn't freshly created
448 SetLastError (ERROR_ALREADY_EXISTS);
450 /* Fall through to create the mutex handle. */
453 /* A new named mutex, so create both the private and
457 if (strlen (utf8_name) < MAX_PATH) {
458 namelen = strlen (utf8_name);
463 memcpy (&namedmutex_handle.sharedns.name, utf8_name, namelen);
465 handle = _wapi_handle_new (WAPI_HANDLE_NAMEDMUTEX,
468 /* A new reference to an existing named mutex, so just
469 * create the private part
471 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX,
475 if (handle == _WAPI_HANDLE_INVALID) {
476 g_warning ("%s: error creating mutex handle", __func__);
477 SetLastError (ERROR_GEN_FAILURE);
483 /* Set the initial state, as this is a completely new
486 thr_ret = _wapi_handle_lock_shared_handles ();
487 g_assert (thr_ret == 0);
490 namedmutex_own (handle);
492 _wapi_shared_handle_set_signal_state (handle, TRUE);
495 _wapi_handle_unlock_shared_handles ();
498 DEBUG ("%s: returning mutex handle %p", __func__, handle);
503 _wapi_namespace_unlock (NULL);
510 * @security: Ignored for now.
511 * @owned: If %TRUE, the mutex is created with the calling thread
512 * already owning the mutex.
513 * @name:Pointer to a string specifying the name of this mutex, or
516 * Creates a new mutex handle. A mutex is signalled when no thread
517 * owns it. A thread acquires ownership of the mutex by waiting for
518 * it with WaitForSingleObject() or WaitForMultipleObjects(). A
519 * thread relinquishes ownership with ReleaseMutex().
521 * A thread that owns a mutex can specify the same mutex in repeated
522 * wait function calls without blocking. The thread must call
523 * ReleaseMutex() an equal number of times to release the mutex.
525 * Return value: A new handle, or %NULL on error.
527 gpointer CreateMutex(WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
528 const gunichar2 *name)
530 mono_once (&mutex_ops_once, mutex_ops_init);
533 return(mutex_create (security, owned));
535 return(namedmutex_create (security, owned, name));
539 static gboolean mutex_release (gpointer handle)
541 struct _WapiHandle_mutex *mutex_handle;
543 pthread_t tid = pthread_self ();
544 pid_t pid = _wapi_getpid ();
546 gboolean ret = FALSE;
548 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
549 (gpointer *)&mutex_handle);
551 g_warning ("%s: error looking up mutex handle %p", __func__,
556 thr_ret = _wapi_handle_lock_handle (handle);
557 g_assert (thr_ret == 0);
559 DEBUG("%s: Releasing mutex handle %p", __func__, handle);
561 if (!pthread_equal (mutex_handle->tid, tid) ||
562 mutex_handle->pid != pid) {
563 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);
569 /* OK, we own this mutex */
570 mutex_handle->recursion--;
572 if(mutex_handle->recursion==0) {
573 _wapi_thread_disown_mutex (handle);
575 DEBUG("%s: Unlocking mutex handle %p", __func__, handle);
579 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
583 thr_ret = _wapi_handle_unlock_handle (handle);
584 g_assert (thr_ret == 0);
589 static gboolean namedmutex_release (gpointer handle)
591 struct _WapiHandle_namedmutex *mutex_handle;
593 pthread_t tid = pthread_self ();
594 pid_t pid = _wapi_getpid ();
596 gboolean ret = FALSE;
598 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
599 (gpointer *)&mutex_handle);
601 g_warning ("%s: error looking up named mutex handle %p",
606 thr_ret = _wapi_handle_lock_shared_handles ();
607 g_assert (thr_ret == 0);
609 DEBUG("%s: Releasing mutex handle %p", __func__, handle);
611 if (!pthread_equal (mutex_handle->tid, tid) ||
612 mutex_handle->pid != pid) {
613 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);
619 /* OK, we own this mutex */
620 mutex_handle->recursion--;
622 if(mutex_handle->recursion==0) {
623 _wapi_thread_disown_mutex (handle);
625 DEBUG("%s: Unlocking mutex handle %p", __func__, handle);
629 _wapi_shared_handle_set_signal_state (handle, TRUE);
633 _wapi_handle_unlock_shared_handles ();
640 * @handle: The mutex handle.
642 * Releases ownership if the mutex handle @handle.
644 * Return value: %TRUE on success, %FALSE otherwise. This function
645 * fails if the calling thread does not own the mutex @handle.
647 gboolean ReleaseMutex(gpointer handle)
651 if (handle == NULL) {
652 SetLastError (ERROR_INVALID_HANDLE);
656 type = _wapi_handle_type (handle);
658 if (mutex_ops[type].release == NULL) {
659 SetLastError (ERROR_INVALID_HANDLE);
663 return(mutex_ops[type].release (handle));
666 gpointer OpenMutex (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, const gunichar2 *name)
674 mono_once (&mutex_ops_once, mutex_ops_init);
676 /* w32 seems to guarantee that opening named objects can't
679 thr_ret = _wapi_namespace_lock ();
680 g_assert (thr_ret == 0);
682 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
684 DEBUG ("%s: Opening named mutex [%s]", __func__, utf8_name);
686 offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
689 /* The name has already been used for a different
692 SetLastError (ERROR_INVALID_HANDLE);
694 } else if (offset == 0) {
695 /* This name doesn't exist */
696 SetLastError (ERROR_FILE_NOT_FOUND); /* yes, really */
700 /* A new reference to an existing named mutex, so just create
703 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX, offset,
706 if (handle == _WAPI_HANDLE_INVALID) {
707 g_warning ("%s: error opening named mutex handle", __func__);
708 SetLastError (ERROR_GEN_FAILURE);
713 DEBUG ("%s: returning named mutex handle %p", __func__, handle);
718 _wapi_namespace_unlock (NULL);