2007-09-02 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / io-layer / mutexes.c
1 /*
2  * mutexes.c:  Mutex handles
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2002-2006 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12 #include <pthread.h>
13 #include <string.h>
14 #include <unistd.h>
15
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>
22
23 #undef DEBUG
24
25 static void mutex_signal(gpointer handle);
26 static gboolean mutex_own (gpointer handle);
27 static gboolean mutex_is_owned (gpointer handle);
28
29 static void namedmutex_signal (gpointer handle);
30 static gboolean namedmutex_own (gpointer handle);
31 static gboolean namedmutex_is_owned (gpointer handle);
32 static void namedmutex_prewait (gpointer handle);
33
34 struct _WapiHandleOps _wapi_mutex_ops = {
35         NULL,                   /* close */
36         mutex_signal,           /* signal */
37         mutex_own,              /* own */
38         mutex_is_owned,         /* is_owned */
39         NULL,                   /* special_wait */
40         NULL                    /* prewait */
41 };
42
43 void _wapi_mutex_details (gpointer handle_info)
44 {
45         struct _WapiHandle_mutex *mut = (struct _WapiHandle_mutex *)handle_info;
46         
47 #ifdef PTHREAD_POINTER_ID
48         g_print ("own: %5d:%5p, count: %5u", mut->pid, mut->tid,
49                  mut->recursion);
50 #else
51         g_print ("own: %5d:%5ld, count: %5u", mut->pid, mut->tid,
52                  mut->recursion);
53 #endif
54 }
55
56 struct _WapiHandleOps _wapi_namedmutex_ops = {
57         NULL,                   /* close */
58         namedmutex_signal,      /* signal */
59         namedmutex_own,         /* own */
60         namedmutex_is_owned,    /* is_owned */
61         NULL,                   /* special_wait */
62         namedmutex_prewait      /* prewait */
63 };
64
65 static gboolean mutex_release (gpointer handle);
66 static gboolean namedmutex_release (gpointer handle);
67
68 static struct 
69 {
70         gboolean (*release)(gpointer handle);
71 } mutex_ops[WAPI_HANDLE_COUNT] = {
72         {NULL},
73         {NULL},
74         {NULL},
75         {NULL},
76         {NULL},
77         {mutex_release},
78         {NULL},
79         {NULL},
80         {NULL},
81         {NULL},
82         {NULL},
83         {namedmutex_release},
84 };
85
86 static mono_once_t mutex_ops_once=MONO_ONCE_INIT;
87
88 static void mutex_ops_init (void)
89 {
90         _wapi_handle_register_capabilities (WAPI_HANDLE_MUTEX,
91                                             WAPI_HANDLE_CAP_WAIT |
92                                             WAPI_HANDLE_CAP_SIGNAL |
93                                             WAPI_HANDLE_CAP_OWN);
94         _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDMUTEX,
95                                             WAPI_HANDLE_CAP_WAIT |
96                                             WAPI_HANDLE_CAP_SIGNAL |
97                                             WAPI_HANDLE_CAP_OWN);
98 }
99
100 static void mutex_signal(gpointer handle)
101 {
102         ReleaseMutex(handle);
103 }
104
105 static gboolean mutex_own (gpointer handle)
106 {
107         struct _WapiHandle_mutex *mutex_handle;
108         gboolean ok;
109         
110         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
111                                   (gpointer *)&mutex_handle);
112         if (ok == FALSE) {
113                 g_warning ("%s: error looking up mutex handle %p", __func__,
114                            handle);
115                 return(FALSE);
116         }
117
118         _wapi_thread_own_mutex (handle);
119         
120 #ifdef DEBUG
121         g_message("%s: owning mutex handle %p", __func__, handle);
122 #endif
123
124         _wapi_handle_set_signal_state (handle, FALSE, FALSE);
125         
126         mutex_handle->pid = _wapi_getpid ();
127         mutex_handle->tid = pthread_self ();
128         mutex_handle->recursion++;
129
130 #ifdef DEBUG
131         g_message ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
132                    handle, mutex_handle->recursion, mutex_handle->pid,
133                    mutex_handle->tid);
134 #endif
135
136         return(TRUE);
137 }
138
139 static gboolean mutex_is_owned (gpointer handle)
140 {
141         struct _WapiHandle_mutex *mutex_handle;
142         gboolean ok;
143         
144         ok=_wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
145                                 (gpointer *)&mutex_handle);
146         if(ok==FALSE) {
147                 g_warning ("%s: error looking up mutex handle %p", __func__,
148                            handle);
149                 return(FALSE);
150         }
151         
152 #ifdef DEBUG
153         g_message("%s: testing ownership mutex handle %p", __func__, handle);
154 #endif
155
156         if (mutex_handle->recursion > 0 &&
157             mutex_handle->pid == _wapi_getpid () &&
158             pthread_equal (mutex_handle->tid, pthread_self ())) {
159 #ifdef DEBUG
160                 g_message ("%s: mutex handle %p owned by %d:%ld", __func__,
161                            handle, _wapi_getpid (), pthread_self ());
162 #endif
163
164                 return(TRUE);
165         } else {
166 #ifdef DEBUG
167                 g_message ("%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 #endif
169
170                 return(FALSE);
171         }
172 }
173
174 static void namedmutex_signal (gpointer handle)
175 {
176         ReleaseMutex(handle);
177 }
178
179 /* NB, always called with the shared handle lock held */
180 static gboolean namedmutex_own (gpointer handle)
181 {
182         struct _WapiHandle_namedmutex *namedmutex_handle;
183         gboolean ok;
184         
185 #ifdef DEBUG
186         g_message ("%s: owning named mutex handle %p", __func__, handle);
187 #endif
188         
189         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
190                                   (gpointer *)&namedmutex_handle);
191         if (ok == FALSE) {
192                 g_warning ("%s: error looking up named mutex handle %p",
193                            __func__, handle);
194                 return(FALSE);
195         }
196
197         _wapi_thread_own_mutex (handle);
198
199         namedmutex_handle->pid = _wapi_getpid ();
200         namedmutex_handle->tid = pthread_self ();
201         namedmutex_handle->recursion++;
202
203         _wapi_shared_handle_set_signal_state (handle, FALSE);
204
205 #ifdef DEBUG
206         g_message ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
207                    handle, namedmutex_handle->recursion,
208                    namedmutex_handle->pid, namedmutex_handle->tid);
209 #endif
210         
211         return(TRUE);
212 }
213
214 static gboolean namedmutex_is_owned (gpointer handle)
215 {
216         struct _WapiHandle_namedmutex *namedmutex_handle;
217         gboolean ok;
218         
219         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
220                                   (gpointer *)&namedmutex_handle);
221         if (ok == FALSE) {
222                 g_warning ("%s: error looking up mutex handle %p", __func__,
223                            handle);
224                 return(FALSE);
225         }
226         
227 #ifdef DEBUG
228         g_message ("%s: testing ownership mutex handle %p", __func__, handle);
229 #endif
230
231         if (namedmutex_handle->recursion > 0 &&
232             namedmutex_handle->pid == _wapi_getpid () &&
233             pthread_equal (namedmutex_handle->tid, pthread_self ())) {
234 #ifdef DEBUG
235                 g_message ("%s: mutex handle %p owned by %d:%ld", __func__,
236                            handle, _wapi_getpid (), pthread_self ());
237 #endif
238
239                 return(TRUE);
240         } else {
241 #ifdef DEBUG
242                 g_message ("%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);
243 #endif
244
245                 return(FALSE);
246         }
247 }
248
249 /* The shared state is not locked when prewait methods are called */
250 static void namedmutex_prewait (gpointer handle)
251 {
252         /* If the mutex is not currently owned, do nothing and let the
253          * usual wait carry on.  If it is owned, check that the owner
254          * is still alive; if it isn't we override the previous owner
255          * and assume that process exited abnormally and failed to
256          * clean up.
257          */
258         struct _WapiHandle_namedmutex *namedmutex_handle;
259         gboolean ok;
260         
261         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
262                                   (gpointer *)&namedmutex_handle);
263         if (ok == FALSE) {
264                 g_warning ("%s: error looking up named mutex handle %p",
265                            __func__, handle);
266                 return;
267         }
268         
269 #ifdef DEBUG
270         g_message ("%s: Checking ownership of named mutex handle %p", __func__,
271                    handle);
272 #endif
273
274         if (namedmutex_handle->recursion == 0) {
275 #ifdef DEBUG
276                 g_message ("%s: Named mutex handle %p not owned", __func__,
277                            handle);
278 #endif
279         } else if (namedmutex_handle->pid == _wapi_getpid ()) {
280 #ifdef DEBUG
281                 g_message ("%s: Named mutex handle %p owned by this process",
282                            __func__, handle);
283 #endif
284         } else {
285                 guint32 *pids = g_new0 (guint32, 32);
286                 guint32 count = 32, needed_bytes, i;
287                 gboolean ret;
288                 int thr_ret;
289                 
290 #ifdef DEBUG
291                 g_message ("%s: Named mutex handle %p owned by another process", __func__, handle);
292 #endif
293                 
294                 ret = EnumProcesses (pids, count * sizeof(guint32),
295                                      &needed_bytes);
296                 if (ret == FALSE) {
297                         do {
298                                 count = needed_bytes / sizeof(guint32);
299 #ifdef DEBUG
300                                 g_message ("%s: Retrying pid lookup with %d slots", __func__, count);
301 #endif
302                                 pids = g_renew (guint32, pids, count);
303                                 ret = EnumProcesses (pids, needed_bytes,
304                                                      &needed_bytes);
305                         } while (ret == FALSE);
306                 }
307
308                 count = needed_bytes / sizeof(guint32);
309
310 #ifdef DEBUG
311                 g_message ("%s: Need to look at %d pids for named mutex handle %p", __func__, count, handle);
312 #endif
313
314                 thr_ret = _wapi_handle_lock_shared_handles ();
315                 g_assert (thr_ret == 0);
316
317                 for (i = 0; i < count; i++) {
318 #ifdef DEBUG
319                         g_message ("%s: Checking pid %d for named mutex handle %p", __func__, pids[i], handle);
320 #endif
321
322                         if (pids[i] == namedmutex_handle->pid) {
323                                 /* Must be still alive, because
324                                  * EnumProcesses() checks for us
325                                  */
326 #ifdef DEBUG
327                                 g_message ("%s: Found active pid %d for named mutex handle %p", __func__, pids[i], handle);
328 #endif
329
330                                 break;
331                         }
332                 }
333                 
334                 g_free (pids);
335
336                 if (i == count) {
337                         /* Didn't find the process that this handle
338                          * was owned by, overriding it
339                          */
340
341 #ifdef DEBUG
342                         g_message ("%s: overriding old owner of named mutex handle %p", __func__, handle);
343 #endif
344
345                         namedmutex_handle->pid = 0;
346                         namedmutex_handle->tid = 0;
347                         namedmutex_handle->recursion = 0;
348
349                         _wapi_shared_handle_set_signal_state (handle, TRUE);
350                 }
351
352                 _wapi_handle_unlock_shared_handles ();
353         }
354 }
355
356 static void mutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
357 {
358         struct _WapiHandle_mutex *mutex_handle;
359         gboolean ok;
360         int thr_ret;
361         
362         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
363                                   (gpointer *)&mutex_handle);
364         if (ok == FALSE) {
365                 g_warning ("%s: error looking up mutex handle %p", __func__,
366                            handle);
367                 return;
368         }
369
370         pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
371                               handle);
372         thr_ret = _wapi_handle_lock_handle (handle);
373         g_assert (thr_ret == 0);
374         
375         if (mutex_handle->pid == pid &&
376             pthread_equal (mutex_handle->tid, tid)) {
377 #ifdef DEBUG
378                 g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
379 #endif
380
381                 mutex_handle->recursion = 0;
382                 mutex_handle->pid = 0;
383                 mutex_handle->tid = 0;
384                 
385                 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
386         }
387
388         thr_ret = _wapi_handle_unlock_handle (handle);
389         g_assert (thr_ret == 0);
390         pthread_cleanup_pop (0);
391 }
392
393 static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid)
394 {
395         struct _WapiHandle_namedmutex *mutex_handle;
396         gboolean ok;
397         int thr_ret;
398         
399         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
400                                   (gpointer *)&mutex_handle);
401         if (ok == FALSE) {
402                 g_warning ("%s: error looking up named mutex handle %p",
403                            __func__, handle);
404                 return;
405         }
406
407         thr_ret = _wapi_handle_lock_shared_handles ();
408         g_assert (thr_ret == 0);
409         
410         if (mutex_handle->pid == pid &&
411             pthread_equal (mutex_handle->tid, tid)) {
412 #ifdef DEBUG
413                 g_message ("%s: Mutex handle %p abandoned!", __func__, handle);
414 #endif
415
416                 mutex_handle->recursion = 0;
417                 mutex_handle->pid = 0;
418                 mutex_handle->tid = 0;
419                 
420                 _wapi_shared_handle_set_signal_state (handle, TRUE);
421         }
422
423         _wapi_handle_unlock_shared_handles ();
424 }
425
426 /* When a thread exits, any mutexes it still holds need to be
427  * signalled.  This function must not be called with the shared handle
428  * lock held, as namedmutex_abandon () will try to acquire it
429  */
430 void _wapi_mutex_abandon (gpointer data, pid_t pid, pthread_t tid)
431 {
432         WapiHandleType type = _wapi_handle_type (data);
433
434         if (type == WAPI_HANDLE_MUTEX) {
435                 mutex_abandon (data, pid, tid);
436         } else if (type == WAPI_HANDLE_NAMEDMUTEX) {
437                 namedmutex_abandon (data, pid, tid);
438         } else {
439                 g_assert_not_reached ();
440         }
441 }
442
443 static gpointer mutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
444                               gboolean owned)
445 {
446         struct _WapiHandle_mutex mutex_handle = {0};
447         gpointer handle;
448         int thr_ret;
449         
450         /* Need to blow away any old errors here, because code tests
451          * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
452          * was freshly created
453          */
454         SetLastError (ERROR_SUCCESS);
455         
456 #ifdef DEBUG
457         g_message ("%s: Creating unnamed mutex", __func__);
458 #endif
459         
460         handle = _wapi_handle_new (WAPI_HANDLE_MUTEX, &mutex_handle);
461         if (handle == _WAPI_HANDLE_INVALID) {
462                 g_warning ("%s: error creating mutex handle", __func__);
463                 SetLastError (ERROR_GEN_FAILURE);
464                 return(NULL);
465         }
466
467         pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
468                               handle);
469         thr_ret = _wapi_handle_lock_handle (handle);
470         g_assert (thr_ret == 0);
471         
472         if(owned==TRUE) {
473                 mutex_own (handle);
474         } else {
475                 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
476         }
477         
478 #ifdef DEBUG
479         g_message ("%s: returning mutex handle %p", __func__, handle);
480 #endif
481
482         thr_ret = _wapi_handle_unlock_handle (handle);
483         g_assert (thr_ret == 0);
484         pthread_cleanup_pop (0);
485         
486         return(handle);
487 }
488
489 static gpointer namedmutex_create (WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
490                         const gunichar2 *name)
491 {
492         struct _WapiHandle_namedmutex namedmutex_handle = {{{0}}, 0};
493         gpointer handle;
494         gchar *utf8_name;
495         int thr_ret;
496         gpointer ret = NULL;
497         guint32 namelen;
498         gint32 offset;
499
500         /* w32 seems to guarantee that opening named objects can't
501          * race each other
502          */
503         thr_ret = _wapi_namespace_lock ();
504         g_assert (thr_ret == 0);
505
506         /* Need to blow away any old errors here, because code tests
507          * for ERROR_ALREADY_EXISTS on success (!) to see if a mutex
508          * was freshly created
509          */
510         SetLastError (ERROR_SUCCESS);
511         
512         utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
513         
514 #ifdef DEBUG
515         g_message ("%s: Creating named mutex [%s]", __func__, utf8_name);
516 #endif
517         
518         offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
519                                                 utf8_name);
520         if (offset == -1) {
521                 /* The name has already been used for a different
522                  * object.
523                  */
524                 SetLastError (ERROR_INVALID_HANDLE);
525                 goto cleanup;
526         } else if (offset != 0) {
527                 /* Not an error, but this is how the caller is
528                  * informed that the mutex wasn't freshly created
529                  */
530                 SetLastError (ERROR_ALREADY_EXISTS);
531         }
532         /* Fall through to create the mutex handle. */
533
534         if (offset == 0) {
535                 /* A new named mutex, so create both the private and
536                  * shared parts
537                  */
538         
539                 if (strlen (utf8_name) < MAX_PATH) {
540                         namelen = strlen (utf8_name);
541                 } else {
542                         namelen = MAX_PATH;
543                 }
544         
545                 memcpy (&namedmutex_handle.sharedns.name, utf8_name, namelen);
546
547                 handle = _wapi_handle_new (WAPI_HANDLE_NAMEDMUTEX,
548                                            &namedmutex_handle);
549         } else {
550                 /* A new reference to an existing named mutex, so just
551                  * create the private part
552                  */
553                 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX,
554                                                        offset, TRUE);
555         }
556         
557         if (handle == _WAPI_HANDLE_INVALID) {
558                 g_warning ("%s: error creating mutex handle", __func__);
559                 SetLastError (ERROR_GEN_FAILURE);
560                 goto cleanup;
561         }
562         ret = handle;
563
564         if (offset == 0) {
565                 /* Set the initial state, as this is a completely new
566                  * handle
567                  */
568                 thr_ret = _wapi_handle_lock_shared_handles ();
569                 g_assert (thr_ret == 0);
570         
571                 if (owned == TRUE) {
572                         namedmutex_own (handle);
573                 } else {
574                         _wapi_shared_handle_set_signal_state (handle, TRUE);
575                 }
576
577                 _wapi_handle_unlock_shared_handles ();
578         }
579         
580 #ifdef DEBUG
581         g_message ("%s: returning mutex handle %p", __func__, handle);
582 #endif
583
584 cleanup:
585         g_free (utf8_name);
586
587         _wapi_namespace_unlock (NULL);
588         
589         return(ret);
590 }
591
592 /**
593  * CreateMutex:
594  * @security: Ignored for now.
595  * @owned: If %TRUE, the mutex is created with the calling thread
596  * already owning the mutex.
597  * @name:Pointer to a string specifying the name of this mutex, or
598  * %NULL.
599  *
600  * Creates a new mutex handle.  A mutex is signalled when no thread
601  * owns it.  A thread acquires ownership of the mutex by waiting for
602  * it with WaitForSingleObject() or WaitForMultipleObjects().  A
603  * thread relinquishes ownership with ReleaseMutex().
604  *
605  * A thread that owns a mutex can specify the same mutex in repeated
606  * wait function calls without blocking.  The thread must call
607  * ReleaseMutex() an equal number of times to release the mutex.
608  *
609  * Return value: A new handle, or %NULL on error.
610  */
611 gpointer CreateMutex(WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean owned,
612                         const gunichar2 *name)
613 {
614         mono_once (&mutex_ops_once, mutex_ops_init);
615
616         if (name == NULL) {
617                 return(mutex_create (security, owned));
618         } else {
619                 return(namedmutex_create (security, owned, name));
620         }
621 }
622
623 static gboolean mutex_release (gpointer handle)
624 {
625         struct _WapiHandle_mutex *mutex_handle;
626         gboolean ok;
627         pthread_t tid = pthread_self ();
628         pid_t pid = _wapi_getpid ();
629         int thr_ret;
630         gboolean ret = FALSE;
631         
632         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
633                                   (gpointer *)&mutex_handle);
634         if (ok == FALSE) {
635                 g_warning ("%s: error looking up mutex handle %p", __func__,
636                            handle);
637                 return(FALSE);
638         }
639
640         pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
641                               handle);
642         thr_ret = _wapi_handle_lock_handle (handle);
643         g_assert (thr_ret == 0);
644         
645 #ifdef DEBUG
646         g_message("%s: Releasing mutex handle %p", __func__, handle);
647 #endif
648
649         if (!pthread_equal (mutex_handle->tid, tid) ||
650             mutex_handle->pid != pid) {
651 #ifdef DEBUG
652                 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, _wapi_getpid (), tid);
653 #endif
654
655                 goto cleanup;
656         }
657         ret = TRUE;
658         
659         /* OK, we own this mutex */
660         mutex_handle->recursion--;
661         
662         if(mutex_handle->recursion==0) {
663                 _wapi_thread_disown_mutex (handle);
664
665 #ifdef DEBUG
666                 g_message("%s: Unlocking mutex handle %p", __func__, handle);
667 #endif
668
669                 mutex_handle->pid=0;
670                 mutex_handle->tid=0;
671                 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
672         }
673
674 cleanup:
675         thr_ret = _wapi_handle_unlock_handle (handle);
676         g_assert (thr_ret == 0);
677         pthread_cleanup_pop (0);
678         
679         return(ret);
680 }
681
682 static gboolean namedmutex_release (gpointer handle)
683 {
684         struct _WapiHandle_namedmutex *mutex_handle;
685         gboolean ok;
686         pthread_t tid = pthread_self ();
687         pid_t pid = _wapi_getpid ();
688         int thr_ret;
689         gboolean ret = FALSE;
690         
691         ok=_wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX,
692                                 (gpointer *)&mutex_handle);
693         if(ok==FALSE) {
694                 g_warning ("%s: error looking up named mutex handle %p",
695                            __func__, handle);
696                 return(FALSE);
697         }
698
699         thr_ret = _wapi_handle_lock_shared_handles ();
700         g_assert (thr_ret == 0);
701         
702 #ifdef DEBUG
703         g_message("%s: Releasing mutex handle %p", __func__, handle);
704 #endif
705
706         if (!pthread_equal (mutex_handle->tid, tid) ||
707             mutex_handle->pid != pid) {
708 #ifdef DEBUG
709                 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, _wapi_getpid (), tid);
710 #endif
711
712                 goto cleanup;
713         }
714         ret = TRUE;
715         
716         /* OK, we own this mutex */
717         mutex_handle->recursion--;
718         
719         if(mutex_handle->recursion==0) {
720                 _wapi_thread_disown_mutex (handle);
721
722 #ifdef DEBUG
723                 g_message("%s: Unlocking mutex handle %p", __func__, handle);
724 #endif
725
726                 mutex_handle->pid=0;
727                 mutex_handle->tid=0;
728                 _wapi_shared_handle_set_signal_state (handle, TRUE);
729         }
730
731 cleanup:
732         _wapi_handle_unlock_shared_handles ();
733         
734         return(ret);
735 }
736
737 /**
738  * ReleaseMutex:
739  * @handle: The mutex handle.
740  *
741  * Releases ownership if the mutex handle @handle.
742  *
743  * Return value: %TRUE on success, %FALSE otherwise.  This function
744  * fails if the calling thread does not own the mutex @handle.
745  */
746 gboolean ReleaseMutex(gpointer handle)
747 {
748         WapiHandleType type;
749
750         if (handle == NULL) {
751                 SetLastError (ERROR_INVALID_HANDLE);
752                 return(FALSE);
753         }
754         
755         type = _wapi_handle_type (handle);
756         
757         if (mutex_ops[type].release == NULL) {
758                 SetLastError (ERROR_INVALID_HANDLE);
759                 return(FALSE);
760         }
761         
762         return(mutex_ops[type].release (handle));
763 }
764
765 gpointer OpenMutex (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, const gunichar2 *name)
766 {
767         gpointer handle;
768         gchar *utf8_name;
769         int thr_ret;
770         gpointer ret = NULL;
771         gint32 offset;
772
773         mono_once (&mutex_ops_once, mutex_ops_init);
774
775         /* w32 seems to guarantee that opening named objects can't
776          * race each other
777          */
778         thr_ret = _wapi_namespace_lock ();
779         g_assert (thr_ret == 0);
780
781         utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
782         
783 #ifdef DEBUG
784         g_message ("%s: Opening named mutex [%s]", __func__, utf8_name);
785 #endif
786         
787         offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDMUTEX,
788                                                 utf8_name);
789         if (offset == -1) {
790                 /* The name has already been used for a different
791                  * object.
792                  */
793                 SetLastError (ERROR_INVALID_HANDLE);
794                 goto cleanup;
795         } else if (offset == 0) {
796                 /* This name doesn't exist */
797                 SetLastError (ERROR_FILE_NOT_FOUND);    /* yes, really */
798                 goto cleanup;
799         }
800
801         /* A new reference to an existing named mutex, so just create
802          * the private part
803          */
804         handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDMUTEX, offset,
805                                                TRUE);
806         
807         if (handle == _WAPI_HANDLE_INVALID) {
808                 g_warning ("%s: error opening named mutex handle", __func__);
809                 SetLastError (ERROR_GEN_FAILURE);
810                 goto cleanup;
811         }
812         ret = handle;
813
814 #ifdef DEBUG
815         g_message ("%s: returning named mutex handle %p", __func__, handle);
816 #endif
817
818 cleanup:
819         g_free (utf8_name);
820
821         _wapi_namespace_unlock (NULL);
822         
823         return(ret);
824 }