:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / kernel32 / synch / mutex.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS system libraries
5  * FILE:            lib/kernel32/synch/mutex.c
6  * PURPOSE:         Mutex functions
7  * PROGRAMMER:      Eric Kohl (ekohl@rz-online.de)
8  * UPDATE HISTORY:
9  *                  Created 01/20/2001
10  */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <kernel32/error.h>
16 #include <windows.h>
17 #include <wchar.h>
18
19 #include <kernel32/kernel32.h>
20
21 /* FUNCTIONS *****************************************************************/
22
23 HANDLE STDCALL
24 CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes,
25              WINBOOL bInitialOwner,
26              LPCSTR lpName)
27 {
28    UNICODE_STRING NameU;
29    ANSI_STRING Name;
30    HANDLE Handle;
31
32    RtlInitAnsiString(&Name,
33                      (LPSTR)lpName);
34    RtlAnsiStringToUnicodeString(&NameU,
35                                 &Name,
36                                 TRUE);
37
38    Handle = CreateMutexW(lpMutexAttributes,
39                          bInitialOwner,
40                          NameU.Buffer);
41
42    RtlFreeUnicodeString(&NameU);
43
44    return Handle;
45 }
46
47
48 HANDLE STDCALL
49 CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes,
50              WINBOOL bInitialOwner,
51              LPCWSTR lpName)
52 {
53    OBJECT_ATTRIBUTES ObjectAttributes;
54    NTSTATUS Status;
55    UNICODE_STRING NameString;
56    HANDLE MutantHandle;
57
58    RtlInitUnicodeString(&NameString,
59                         (LPWSTR)lpName);
60
61    ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
62    ObjectAttributes.RootDirectory = hBaseDir;
63    ObjectAttributes.ObjectName = &NameString;
64    ObjectAttributes.Attributes = 0;
65    ObjectAttributes.SecurityDescriptor = NULL;
66    ObjectAttributes.SecurityQualityOfService = NULL;
67
68    if (lpMutexAttributes != NULL)
69      {
70         ObjectAttributes.SecurityDescriptor = lpMutexAttributes->lpSecurityDescriptor;
71         if (lpMutexAttributes->bInheritHandle == TRUE)
72           {
73              ObjectAttributes.Attributes |= OBJ_INHERIT;
74           }
75      }
76
77    Status = NtCreateMutant(&MutantHandle,
78                            MUTEX_ALL_ACCESS,
79                            &ObjectAttributes,
80                            (BOOLEAN)bInitialOwner);
81    if (!NT_SUCCESS(Status))
82      {
83         SetLastErrorByStatus(Status);
84         return NULL;
85      }
86
87    return MutantHandle;
88 }
89
90
91 HANDLE STDCALL
92 OpenMutexA(DWORD dwDesiredAccess,
93            WINBOOL bInheritHandle,
94            LPCSTR lpName)
95 {
96    OBJECT_ATTRIBUTES ObjectAttributes;
97    UNICODE_STRING NameU;
98    ANSI_STRING Name;
99    HANDLE Handle;
100    NTSTATUS Status;
101
102    if (lpName == NULL)
103      {
104         SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
105         return NULL;
106      }
107
108    RtlInitAnsiString(&Name,
109                      (LPSTR)lpName);
110    RtlAnsiStringToUnicodeString(&NameU,
111                                 &Name,
112                                 TRUE);
113
114    ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
115    ObjectAttributes.RootDirectory = hBaseDir;
116    ObjectAttributes.ObjectName = &NameU;
117    ObjectAttributes.Attributes = 0;
118    ObjectAttributes.SecurityDescriptor = NULL;
119    ObjectAttributes.SecurityQualityOfService = NULL;
120    if (bInheritHandle == TRUE)
121      {
122         ObjectAttributes.Attributes |= OBJ_INHERIT;
123      }
124
125    Status = NtOpenMutant(&Handle,
126                          (ACCESS_MASK)dwDesiredAccess,
127                          &ObjectAttributes);
128
129    RtlFreeUnicodeString(&NameU);
130
131    if (!NT_SUCCESS(Status))
132      {
133         SetLastErrorByStatus(Status);
134         return NULL;
135      }
136
137    return Handle;
138 }
139
140
141 HANDLE STDCALL
142 OpenMutexW(DWORD dwDesiredAccess,
143            WINBOOL bInheritHandle,
144            LPCWSTR lpName)
145 {
146    OBJECT_ATTRIBUTES ObjectAttributes;
147    UNICODE_STRING Name;
148    HANDLE Handle;
149    NTSTATUS Status;
150
151    if (lpName == NULL)
152      {
153         SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
154         return NULL;
155      }
156
157    RtlInitUnicodeString(&Name,
158                         (LPWSTR)lpName);
159
160    ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
161    ObjectAttributes.RootDirectory = hBaseDir;
162    ObjectAttributes.ObjectName = &Name;
163    ObjectAttributes.Attributes = 0;
164    ObjectAttributes.SecurityDescriptor = NULL;
165    ObjectAttributes.SecurityQualityOfService = NULL;
166    if (bInheritHandle == TRUE)
167      {
168         ObjectAttributes.Attributes |= OBJ_INHERIT;
169      }
170
171    Status = NtOpenMutant(&Handle,
172                          (ACCESS_MASK)dwDesiredAccess,
173                          &ObjectAttributes);
174    if (!NT_SUCCESS(Status))
175      {
176         SetLastErrorByStatus(Status);
177         return NULL;
178      }
179
180    return Handle;
181 }
182
183
184 WINBOOL STDCALL
185 ReleaseMutex(HANDLE hMutex)
186 {
187    NTSTATUS Status;
188
189    Status = NtReleaseMutant(hMutex,
190                             NULL);
191    if (!NT_SUCCESS(Status))
192      {
193         SetLastErrorByStatus(Status);
194         return FALSE;
195      }
196
197    return TRUE;
198 }
199
200
201 /* EOF */