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