Merge pull request #4615 from alexanderkyte/string_error_handling
[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
16 void
17 mono_w32mutex_init (void)
18 {
19 }
20
21 gpointer
22 ves_icall_System_Threading_Mutex_CreateMutex_internal (MonoBoolean owned, MonoString *name, MonoBoolean *created)
23 {
24         HANDLE mutex;
25
26         *created = TRUE;
27
28         if (!name) {
29                 mutex = CreateMutex (NULL, owned, NULL);
30         } else {
31                 mutex = CreateMutex (NULL, owned, mono_string_chars (name));
32
33                 if (GetLastError () == ERROR_ALREADY_EXISTS)
34                         *created = FALSE;
35         }
36
37         return mutex;
38 }
39
40 MonoBoolean
41 ves_icall_System_Threading_Mutex_ReleaseMutex_internal (gpointer handle)
42 {
43         return ReleaseMutex (handle);
44 }
45
46 gpointer
47 ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString *name, gint32 rights, gint32 *error)
48 {
49         HANDLE ret;
50
51         *error = ERROR_SUCCESS;
52
53         ret = OpenMutex (rights, FALSE, mono_string_chars (name));
54         if (!ret)
55                 *error = GetLastError ();
56
57         return ret;
58 }