update for HEAD-2003091401
[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 <k32.h>
15
16 #define NDEBUG
17 #include <kernel32/kernel32.h>
18
19 /* FUNCTIONS *****************************************************************/
20
21 /*
22  * @implemented
23  */
24 HANDLE STDCALL
25 CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes,
26              WINBOOL bInitialOwner,
27              LPCSTR lpName)
28 {
29    UNICODE_STRING NameU;
30    ANSI_STRING Name;
31    HANDLE Handle;
32
33    RtlInitAnsiString(&Name,
34                      (LPSTR)lpName);
35    RtlAnsiStringToUnicodeString(&NameU,
36                                 &Name,
37                                 TRUE);
38
39    Handle = CreateMutexW(lpMutexAttributes,
40                          bInitialOwner,
41                          NameU.Buffer);
42
43    RtlFreeUnicodeString(&NameU);
44
45    return Handle;
46 }
47
48
49 /*
50  * @implemented
51  */
52 HANDLE STDCALL
53 CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes,
54              WINBOOL bInitialOwner,
55              LPCWSTR lpName)
56 {
57    OBJECT_ATTRIBUTES ObjectAttributes;
58    NTSTATUS Status;
59    UNICODE_STRING NameString;
60    HANDLE MutantHandle;
61
62    RtlInitUnicodeString(&NameString,
63                         (LPWSTR)lpName);
64
65    ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
66    ObjectAttributes.RootDirectory = hBaseDir;
67    ObjectAttributes.ObjectName = &NameString;
68    ObjectAttributes.Attributes = 0;
69    ObjectAttributes.SecurityDescriptor = NULL;
70    ObjectAttributes.SecurityQualityOfService = NULL;
71
72    if (lpMutexAttributes != NULL)
73      {
74         ObjectAttributes.SecurityDescriptor = lpMutexAttributes->lpSecurityDescriptor;
75         if (lpMutexAttributes->bInheritHandle == TRUE)
76           {
77              ObjectAttributes.Attributes |= OBJ_INHERIT;
78           }
79      }
80
81    Status = NtCreateMutant(&MutantHandle,
82                            MUTEX_ALL_ACCESS,
83                            &ObjectAttributes,
84                            (BOOLEAN)bInitialOwner);
85    if (!NT_SUCCESS(Status))
86      {
87         SetLastErrorByStatus(Status);
88         return NULL;
89      }
90
91    return MutantHandle;
92 }
93
94
95 /*
96  * @implemented
97  */
98 HANDLE STDCALL
99 OpenMutexA(DWORD dwDesiredAccess,
100            WINBOOL bInheritHandle,
101            LPCSTR lpName)
102 {
103    OBJECT_ATTRIBUTES ObjectAttributes;
104    UNICODE_STRING NameU;
105    ANSI_STRING Name;
106    HANDLE Handle;
107    NTSTATUS Status;
108
109    if (lpName == NULL)
110      {
111         SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
112         return NULL;
113      }
114
115    RtlInitAnsiString(&Name,
116                      (LPSTR)lpName);
117    RtlAnsiStringToUnicodeString(&NameU,
118                                 &Name,
119                                 TRUE);
120
121    ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
122    ObjectAttributes.RootDirectory = hBaseDir;
123    ObjectAttributes.ObjectName = &NameU;
124    ObjectAttributes.Attributes = 0;
125    ObjectAttributes.SecurityDescriptor = NULL;
126    ObjectAttributes.SecurityQualityOfService = NULL;
127    if (bInheritHandle == TRUE)
128      {
129         ObjectAttributes.Attributes |= OBJ_INHERIT;
130      }
131
132    Status = NtOpenMutant(&Handle,
133                          (ACCESS_MASK)dwDesiredAccess,
134                          &ObjectAttributes);
135
136    RtlFreeUnicodeString(&NameU);
137
138    if (!NT_SUCCESS(Status))
139      {
140         SetLastErrorByStatus(Status);
141         return NULL;
142      }
143
144    return Handle;
145 }
146
147
148 /*
149  * @implemented
150  */
151 HANDLE STDCALL
152 OpenMutexW(DWORD dwDesiredAccess,
153            WINBOOL bInheritHandle,
154            LPCWSTR lpName)
155 {
156    OBJECT_ATTRIBUTES ObjectAttributes;
157    UNICODE_STRING Name;
158    HANDLE Handle;
159    NTSTATUS Status;
160
161    if (lpName == NULL)
162      {
163         SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
164         return NULL;
165      }
166
167    RtlInitUnicodeString(&Name,
168                         (LPWSTR)lpName);
169
170    ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
171    ObjectAttributes.RootDirectory = hBaseDir;
172    ObjectAttributes.ObjectName = &Name;
173    ObjectAttributes.Attributes = 0;
174    ObjectAttributes.SecurityDescriptor = NULL;
175    ObjectAttributes.SecurityQualityOfService = NULL;
176    if (bInheritHandle == TRUE)
177      {
178         ObjectAttributes.Attributes |= OBJ_INHERIT;
179      }
180
181    Status = NtOpenMutant(&Handle,
182                          (ACCESS_MASK)dwDesiredAccess,
183                          &ObjectAttributes);
184    if (!NT_SUCCESS(Status))
185      {
186         SetLastErrorByStatus(Status);
187         return NULL;
188      }
189
190    return Handle;
191 }
192
193
194 /*
195  * @implemented
196  */
197 WINBOOL STDCALL
198 ReleaseMutex(HANDLE hMutex)
199 {
200    NTSTATUS Status;
201
202    Status = NtReleaseMutant(hMutex,
203                             NULL);
204    if (!NT_SUCCESS(Status))
205      {
206         SetLastErrorByStatus(Status);
207         return FALSE;
208      }
209
210    return TRUE;
211 }
212
213
214 /* EOF */