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