2 * mutexes.c: Mutex handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 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/mono-mutex.h>
21 #include <mono/io-layer/mutex-private.h>
25 static void mutex_signal(gpointer handle);
26 static gboolean mutex_own (gpointer handle);
27 static gboolean mutex_is_owned (gpointer handle);
29 static void namedmutex_signal (gpointer handle);
30 static gboolean namedmutex_own (gpointer handle);
31 static gboolean namedmutex_is_owned (gpointer handle);
33 struct _WapiHandleOps _wapi_mutex_ops = {
35 mutex_signal, /* signal */
37 mutex_is_owned, /* is_owned */
40 void _wapi_mutex_details (gpointer handle_info)
42 struct _WapiHandle_mutex *mut = (struct _WapiHandle_mutex *)handle_info;
44 #ifdef PTHREAD_POINTER_ID
45 g_print ("own: %5d:%5p, count: %5u", mut->pid, mut->tid,
48 g_print ("own: %5d:%5ld, count: %5u", mut->pid, mut->tid,
53 struct _WapiHandleOps _wapi_namedmutex_ops = {
55 namedmutex_signal, /* signal */
56 namedmutex_own, /* own */
57 namedmutex_is_owned, /* is_owned */
60 static gboolean mutex_release (gpointer handle);
61 static gboolean namedmutex_release (gpointer handle);
65 gboolean (*release)(gpointer handle);
66 } mutex_ops[WAPI_HANDLE_COUNT] = {
81 static mono_once_t mutex_ops_once=MONO_ONCE_INIT;
83 static void mutex_ops_init (void)
85 _wapi_handle_register_capabilities (WAPI_HANDLE_MUTEX,
86 WAPI_HANDLE_CAP_WAIT |
87 WAPI_HANDLE_CAP_SIGNAL |
89 _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDMUTEX,
90 WAPI_HANDLE_CAP_WAIT |
91 WAPI_HANDLE_CAP_SIGNAL |
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 (pthread_self (), handle);
116 g_message("%s: owning mutex handle %p", __func__, handle);
119 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
121 mutex_handle->pid = getpid ();
122 mutex_handle->tid = pthread_self ();
123 mutex_handle->recursion++;
126 g_message ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
127 handle, mutex_handle->recursion, mutex_handle->pid,
134 static gboolean mutex_is_owned (gpointer handle)
136 struct _WapiHandle_mutex *mutex_handle;
139 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
140 (gpointer *)&mutex_handle);
142 g_warning ("%s: error looking up mutex handle %p", __func__,
148 g_message("%s: testing ownership mutex handle %p", __func__, handle);
151 if (mutex_handle->recursion > 0 &&
152 mutex_handle->pid == getpid () &&
153 mutex_handle->tid == pthread_self ()) {
155 g_message ("%s: mutex handle %p owned by %d:%ld", __func__,
156 handle, getpid (), pthread_self ());
162 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);
169 static void namedmutex_signal (gpointer handle)
171 ReleaseMutex(handle);
174 /* NB, always called with the shared handle lock held */
175 static gboolean namedmutex_own (gpointer handle)
177 struct _WapiHandle_namedmutex *namedmutex_handle;
181 g_message ("%s: owning named mutex handle %p", __func__, handle);
184 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
185 (gpointer *)&namedmutex_handle);
187 g_warning ("%s: error looking up named mutex handle %p",
192 _wapi_thread_own_mutex (pthread_self (), handle);
194 namedmutex_handle->pid = getpid ();
195 namedmutex_handle->tid = pthread_self ();
196 namedmutex_handle->recursion++;
198 _wapi_shared_handle_set_signal_state (handle, FALSE);
201 g_message ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
202 handle, namedmutex_handle->recursion,
203 namedmutex_handle->pid, namedmutex_handle->tid);
209 static gboolean namedmutex_is_owned (gpointer handle)
211 struct _WapiHandle_namedmutex *namedmutex_handle;
214 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
215 (gpointer *)&namedmutex_handle);
217 g_warning ("%s: error looking up mutex handle %p", __func__,
223 g_message ("%s: testing ownership mutex handle %p", __func__, handle);
226 if (namedmutex_handle->recursion > 0 &&
227 namedmutex_handle->pid == getpid () &&
228 namedmutex_handle->tid == pthread_self ()) {
230 g_message ("%s: mutex handle %p owned by %d:%ld", __func__,
231 handle, getpid (), pthread_self ());
237 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);
244 static void mutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
246 struct _WapiHandle_mutex *mutex_handle;
250 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
251 (gpointer *)&mutex_handle);
253 g_warning ("%s: error looking up mutex handle %p", __func__,
258 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
260 thr_ret = _wapi_handle_lock_handle (handle);
261 g_assert (thr_ret == 0);
263 if (mutex_handle->pid == pid &&
264 mutex_handle->tid == tid) {
266 g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
269 mutex_handle->recursion = 0;
270 mutex_handle->pid = 0;
271 mutex_handle->tid = 0;
273 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
276 thr_ret = _wapi_handle_unlock_handle (handle);
277 g_assert (thr_ret == 0);
278 pthread_cleanup_pop (0);
281 static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
283 struct _WapiHandle_namedmutex *mutex_handle;
287 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
288 (gpointer *)&mutex_handle);
290 g_warning ("%s: error looking up named mutex handle %p",
295 thr_ret = _wapi_handle_lock_shared_handles ();
296 g_assert (thr_ret == 0);
298 if (mutex_handle->pid == pid &&
299 mutex_handle->tid == tid) {
301 g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
304 mutex_handle->recursion = 0;
305 mutex_handle->pid = 0;
306 mutex_handle->tid = 0;
308 _wapi_shared_handle_set_signal_state (handle, TRUE);
311 _wapi_handle_unlock_shared_handles ();
314 /* When a thread exits, any mutexes it still holds need to be
315 * signalled. This function must not be called with the shared handle
316 * lock held, as namedmutex_abandon () will try to acquire it
318 void _wapi_mutex_abandon (gpointer data, pid_t pid, pthread_t tid)
320 WapiHandleType type = _wapi_handle_type (data);
322 if (type == WAPI_HANDLE_MUTEX) {
323 mutex_abandon (data, pid, tid);
324 } else if (type == WAPI_HANDLE_NAMEDMUTEX) {
325 namedmutex_abandon (data, pid, tid);
327 g_assert_not_reached ();
331 static gpointer mutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
334 struct _WapiHandle_mutex mutex_handle = {0};
338 /* Need to blow away any old errors here, because code tests
339 * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
340 * was freshly created
342 SetLastError (ERROR_SUCCESS);
345 g_message ("%s: Creating unnamed mutex", __func__);
348 handle = _wapi_handle_new (WAPI_HANDLE_MUTEX, &mutex_handle);
349 if (handle == _WAPI_HANDLE_INVALID) {
350 g_warning ("%s: error creating mutex handle", __func__);
351 SetLastError (ERROR_GEN_FAILURE);
355 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
357 thr_ret = _wapi_handle_lock_handle (handle);
358 g_assert (thr_ret == 0);
363 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
367 g_message ("%s: returning mutex handle %p", __func__, handle);
370 thr_ret = _wapi_handle_unlock_handle (handle);
371 g_assert (thr_ret == 0);
372 pthread_cleanup_pop (0);
377 static gpointer namedmutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
378 const gunichar2 *name)
380 struct _WapiHandle_namedmutex namedmutex_handle = {{{0}}, 0};
388 /* w32 seems to guarantee that opening named mutexes can't
391 pthread_cleanup_push ((void(*)(void *))_wapi_namespace_unlock, NULL);
392 thr_ret = _wapi_namespace_lock ();
393 g_assert (thr_ret == 0);
395 /* Need to blow away any old errors here, because code tests
396 * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
397 * was freshly created
399 SetLastError (ERROR_SUCCESS);
401 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
404 g_message ("%s: Creating named mutex [%s]", __func__, utf8_name);
407 offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
410 /* The name has already been used for a different
413 SetLastError (ERROR_INVALID_HANDLE);
415 } else if (offset != 0) {
416 /* Not an error, but this is how the caller is
417 * informed that the mutex wasn't freshly created
419 SetLastError (ERROR_ALREADY_EXISTS);
421 /* Fall through to create the mutex handle. */
423 if (strlen (utf8_name) < MAX_PATH) {
424 namelen = strlen (utf8_name);
429 memcpy (&namedmutex_handle.sharedns.name, utf8_name, namelen);
432 /* A new named mutex, so create both the private and
435 handle = _wapi_handle_new (WAPI_HANDLE_NAMEDMUTEX,
438 /* A new reference to an existing named mutex, so just
439 * create the private part
441 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX,
445 if (handle == _WAPI_HANDLE_INVALID) {
446 g_warning ("%s: error creating mutex handle", __func__);
447 SetLastError (ERROR_GEN_FAILURE);
453 /* Set the initial state, as this is a completely new
456 thr_ret = _wapi_handle_lock_shared_handles ();
457 g_assert (thr_ret == 0);
460 namedmutex_own (handle);
462 _wapi_shared_handle_set_signal_state (handle, TRUE);
465 _wapi_handle_unlock_shared_handles ();
469 g_message ("%s: returning mutex handle %p", __func__, handle);
475 _wapi_namespace_unlock (NULL);
476 pthread_cleanup_pop (0);
483 * @security: Ignored for now.
484 * @owned: If %TRUE, the mutex is created with the calling thread
485 * already owning the mutex.
486 * @name:Pointer to a string specifying the name of this mutex, or
489 * Creates a new mutex handle. A mutex is signalled when no thread
490 * owns it. A thread acquires ownership of the mutex by waiting for
491 * it with WaitForSingleObject() or WaitForMultipleObjects(). A
492 * thread relinquishes ownership with ReleaseMutex().
494 * A thread that owns a mutex can specify the same mutex in repeated
495 * wait function calls without blocking. The thread must call
496 * ReleaseMutex() an equal number of times to release the mutex.
498 * Return value: A new handle, or %NULL on error.
500 gpointer CreateMutex(WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
501 const gunichar2 *name)
503 mono_once (&mutex_ops_once, mutex_ops_init);
506 return(mutex_create (security, owned));
508 return(namedmutex_create (security, owned, name));
512 static gboolean mutex_release (gpointer handle)
514 struct _WapiHandle_mutex *mutex_handle;
516 pthread_t tid=pthread_self();
519 gboolean ret = FALSE;
521 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
522 (gpointer *)&mutex_handle);
524 g_warning ("%s: error looking up mutex handle %p", __func__,
529 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
531 thr_ret = _wapi_handle_lock_handle (handle);
532 g_assert (thr_ret == 0);
535 g_message("%s: Releasing mutex handle %p", __func__, handle);
538 if(mutex_handle->tid!=tid || mutex_handle->pid!=pid) {
540 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);
547 /* OK, we own this mutex */
548 mutex_handle->recursion--;
550 if(mutex_handle->recursion==0) {
551 _wapi_thread_disown_mutex (tid, handle);
554 g_message("%s: Unlocking mutex handle %p", __func__, handle);
559 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
563 thr_ret = _wapi_handle_unlock_handle (handle);
564 g_assert (thr_ret == 0);
565 pthread_cleanup_pop (0);
570 static gboolean namedmutex_release (gpointer handle)
572 struct _WapiHandle_namedmutex *mutex_handle;
574 pthread_t tid=pthread_self();
577 gboolean ret = FALSE;
579 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
580 (gpointer *)&mutex_handle);
582 g_warning ("%s: error looking up named mutex handle %p",
587 thr_ret = _wapi_handle_lock_shared_handles ();
588 g_assert (thr_ret == 0);
591 g_message("%s: Releasing mutex handle %p", __func__, handle);
594 if(mutex_handle->tid!=tid || mutex_handle->pid!=pid) {
596 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);
603 /* OK, we own this mutex */
604 mutex_handle->recursion--;
606 if(mutex_handle->recursion==0) {
607 _wapi_thread_disown_mutex (tid, handle);
610 g_message("%s: Unlocking mutex handle %p", __func__, handle);
615 _wapi_shared_handle_set_signal_state (handle, TRUE);
619 _wapi_handle_unlock_shared_handles ();
626 * @handle: The mutex handle.
628 * Releases ownership if the mutex handle @handle.
630 * Return value: %TRUE on success, %FALSE otherwise. This function
631 * fails if the calling thread does not own the mutex @handle.
633 gboolean ReleaseMutex(gpointer handle)
637 if (handle == NULL) {
638 SetLastError (ERROR_INVALID_HANDLE);
642 type = _wapi_handle_type (handle);
644 if (mutex_ops[type].release == NULL) {
645 SetLastError (ERROR_INVALID_HANDLE);
649 return(mutex_ops[type].release (handle));