migrate substitution keywords to SVN
[reactos.git] / reactos / 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 "../include/debug.h"
18
19
20 /* FUNCTIONS *****************************************************************/
21
22 /*
23 * @implemented
24 */
25 HANDLE STDCALL
26 CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes,
27 BOOL bInitialOwner,
28 LPCSTR lpName)
29 {
30 UNICODE_STRING NameU;
31 ANSI_STRING Name;
32 HANDLE Handle;
33
34 if (lpName != NULL)
35 {
36 RtlInitAnsiString(&Name,
37 (LPSTR)lpName);
38
39 RtlAnsiStringToUnicodeString(&NameU,
40 &Name,
41 TRUE);
42 }
43
44 Handle = CreateMutexW(lpMutexAttributes,
45 bInitialOwner,
46 (lpName ? NameU.Buffer : NULL));
47
48 if (lpName != NULL)
49 {
50 RtlFreeUnicodeString(&NameU);
51 }
52
53 return Handle;
54 }
55
56
57 /*
58 * @implemented
59 */
60 HANDLE STDCALL
61 CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes,
62 BOOL bInitialOwner,
63 LPCWSTR lpName)
64 {
65 OBJECT_ATTRIBUTES ObjectAttributes;
66 NTSTATUS Status;
67 UNICODE_STRING UnicodeName;
68 HANDLE MutantHandle;
69
70 if (lpName != NULL)
71 {
72 RtlInitUnicodeString(&UnicodeName,
73 (LPWSTR)lpName);
74 }
75
76 InitializeObjectAttributes(&ObjectAttributes,
77 (lpName ? &UnicodeName : NULL),
78 0,
79 hBaseDir,
80 NULL);
81
82 if (lpMutexAttributes != NULL)
83 {
84 ObjectAttributes.SecurityDescriptor = lpMutexAttributes->lpSecurityDescriptor;
85 if (lpMutexAttributes->bInheritHandle)
86 {
87 ObjectAttributes.Attributes |= OBJ_INHERIT;
88 }
89 }
90
91 Status = NtCreateMutant(&MutantHandle,
92 MUTEX_ALL_ACCESS,
93 &ObjectAttributes,
94 (BOOLEAN)bInitialOwner);
95 if (!NT_SUCCESS(Status))
96 {
97 SetLastErrorByStatus(Status);
98 return NULL;
99 }
100
101 return MutantHandle;
102 }
103
104
105 /*
106 * @implemented
107 */
108 HANDLE STDCALL
109 OpenMutexA(DWORD dwDesiredAccess,
110 BOOL bInheritHandle,
111 LPCSTR lpName)
112 {
113 OBJECT_ATTRIBUTES ObjectAttributes;
114 UNICODE_STRING NameU;
115 ANSI_STRING Name;
116 HANDLE Handle;
117 NTSTATUS Status;
118
119 if (lpName == NULL)
120 {
121 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
122 return NULL;
123 }
124
125 RtlInitAnsiString(&Name,
126 (LPSTR)lpName);
127 RtlAnsiStringToUnicodeString(&NameU,
128 &Name,
129 TRUE);
130
131 InitializeObjectAttributes(&ObjectAttributes,
132 &NameU,
133 (bInheritHandle ? OBJ_INHERIT : 0),
134 hBaseDir,
135 NULL);
136
137 Status = NtOpenMutant(&Handle,
138 (ACCESS_MASK)dwDesiredAccess,
139 &ObjectAttributes);
140
141 RtlFreeUnicodeString(&NameU);
142
143 if (!NT_SUCCESS(Status))
144 {
145 SetLastErrorByStatus(Status);
146 return NULL;
147 }
148
149 return Handle;
150 }
151
152
153 /*
154 * @implemented
155 */
156 HANDLE STDCALL
157 OpenMutexW(DWORD dwDesiredAccess,
158 BOOL bInheritHandle,
159 LPCWSTR lpName)
160 {
161 OBJECT_ATTRIBUTES ObjectAttributes;
162 UNICODE_STRING Name;
163 HANDLE Handle;
164 NTSTATUS Status;
165
166 if (lpName == NULL)
167 {
168 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
169 return NULL;
170 }
171
172 RtlInitUnicodeString(&Name,
173 (LPWSTR)lpName);
174
175 InitializeObjectAttributes(&ObjectAttributes,
176 &Name,
177 (bInheritHandle ? OBJ_INHERIT : 0),
178 hBaseDir,
179 NULL);
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 BOOL 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 */