[runtime] Use coop handles for ves_icall_System_Threading_Mutex_CreateMutex_internal
[mono.git] / mono / metadata / w32mutex-win32.c
1 /**
2  * \file
3  * Runtime support for managed Mutex on Win32
4  *
5  * Author:
6  *      Ludovic Henry (luhenry@microsoft.com)
7  *
8  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10
11 #include "w32mutex.h"
12
13 #include <windows.h>
14 #include <winbase.h>
15 #include <mono/metadata/handle.h>
16 #include <mono/utils/mono-error-internals.h>
17
18
19 void
20 mono_w32mutex_init (void)
21 {
22 }
23
24 gpointer
25 ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoStringHandle name, MonoBoolean *created, MonoError *error)
26 {
27         HANDLE mutex;
28
29         error_init (error);
30
31         *created = TRUE;
32
33         if (MONO_HANDLE_IS_NULL (name)) {
34                 MONO_ENTER_GC_SAFE;
35                 mutex = CreateMutex (NULL, owned, NULL);
36                 MONO_EXIT_GC_SAFE;
37         } else {
38                 uint32_t gchandle;
39                 gunichar2 *uniname = mono_string_handle_pin_chars (name, &gchandle);
40                 MONO_ENTER_GC_SAFE;
41                 mutex = CreateMutex (NULL, owned, uniname);
42
43                 if (GetLastError () == ERROR_ALREADY_EXISTS)
44                         *created = FALSE;
45                 MONO_EXIT_GC_SAFE;
46                 mono_gchandle_free (gchandle);
47         }
48
49         return mutex;
50 }
51
52 MonoBoolean
53 ves_icall_System_Threading_Mutex_ReleaseMutex_internal (gpointer handle)
54 {
55         return ReleaseMutex (handle);
56 }
57
58 gpointer
59 ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString *name, gint32 rights, gint32 *error)
60 {
61         HANDLE ret;
62
63         *error = ERROR_SUCCESS;
64
65         ret = OpenMutex (rights, FALSE, mono_string_chars (name));
66         if (!ret)
67                 *error = GetLastError ();
68
69         return ret;
70 }