3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/synch/sem.c
6 * PURPOSE: Semaphore functions
7 * PROGRAMMER: Eric Kohl (ekohl@rz-online.de)
12 /* INCLUDES *****************************************************************/
17 #include <kernel32/kernel32.h>
19 /* FUNCTIONS ****************************************************************/
25 CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
34 RtlInitAnsiString(&Name,
36 RtlAnsiStringToUnicodeString(&NameU,
40 Handle = CreateSemaphoreW(lpSemaphoreAttributes,
45 RtlFreeUnicodeString (&NameU);
55 CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
60 OBJECT_ATTRIBUTES ObjectAttributes;
62 UNICODE_STRING NameString;
63 HANDLE SemaphoreHandle;
67 NameString.Length = lstrlenW(lpName)*sizeof(WCHAR);
71 NameString.Length = 0;
74 NameString.Buffer = (WCHAR *)lpName;
75 NameString.MaximumLength = NameString.Length;
77 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
78 ObjectAttributes.RootDirectory = hBaseDir;
79 ObjectAttributes.ObjectName = &NameString;
80 ObjectAttributes.Attributes = 0;
81 ObjectAttributes.SecurityDescriptor = NULL;
82 ObjectAttributes.SecurityQualityOfService = NULL;
83 if (lpSemaphoreAttributes != NULL)
85 ObjectAttributes.SecurityDescriptor = lpSemaphoreAttributes->lpSecurityDescriptor;
86 if (lpSemaphoreAttributes->bInheritHandle == TRUE)
88 ObjectAttributes.Attributes |= OBJ_INHERIT;
92 Status = NtCreateSemaphore(&SemaphoreHandle,
97 if (!NT_SUCCESS(Status))
99 SetLastErrorByStatus(Status);
102 return SemaphoreHandle;
110 OpenSemaphoreA(DWORD dwDesiredAccess,
111 WINBOOL bInheritHandle,
114 OBJECT_ATTRIBUTES ObjectAttributes;
115 UNICODE_STRING NameU;
122 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
126 RtlInitAnsiString(&Name,
128 RtlAnsiStringToUnicodeString(&NameU,
132 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
133 ObjectAttributes.RootDirectory = hBaseDir;
134 ObjectAttributes.ObjectName = &NameU;
135 ObjectAttributes.Attributes = 0;
136 ObjectAttributes.SecurityDescriptor = NULL;
137 ObjectAttributes.SecurityQualityOfService = NULL;
138 if (bInheritHandle == TRUE)
140 ObjectAttributes.Attributes |= OBJ_INHERIT;
143 Status = NtOpenSemaphore(&Handle,
144 (ACCESS_MASK)dwDesiredAccess,
147 RtlFreeUnicodeString(&NameU);
149 if (!NT_SUCCESS(Status))
151 SetLastErrorByStatus(Status);
163 OpenSemaphoreW(DWORD dwDesiredAccess,
164 WINBOOL bInheritHandle,
167 OBJECT_ATTRIBUTES ObjectAttributes;
174 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
178 RtlInitUnicodeString(&Name,
181 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
182 ObjectAttributes.RootDirectory = hBaseDir;
183 ObjectAttributes.ObjectName = &Name;
184 ObjectAttributes.Attributes = 0;
185 ObjectAttributes.SecurityDescriptor = NULL;
186 ObjectAttributes.SecurityQualityOfService = NULL;
187 if (bInheritHandle == TRUE)
189 ObjectAttributes.Attributes |= OBJ_INHERIT;
192 Status = NtOpenSemaphore(&Handle,
193 (ACCESS_MASK)dwDesiredAccess,
195 if (!NT_SUCCESS(Status))
197 SetLastErrorByStatus(Status);
209 ReleaseSemaphore(HANDLE hSemaphore,
211 LPLONG lpPreviousCount)
215 Status = NtReleaseSemaphore(hSemaphore,
218 if (!NT_SUCCESS(Status))
220 SetLastErrorByStatus(Status);