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 ****************************************************************/
22 CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
31 RtlInitAnsiString(&Name,
33 RtlAnsiStringToUnicodeString(&NameU,
37 Handle = CreateSemaphoreW(lpSemaphoreAttributes,
42 RtlFreeUnicodeString (&NameU);
49 CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
54 OBJECT_ATTRIBUTES ObjectAttributes;
56 UNICODE_STRING NameString;
57 HANDLE SemaphoreHandle;
61 NameString.Length = lstrlenW(lpName)*sizeof(WCHAR);
65 NameString.Length = 0;
68 NameString.Buffer = (WCHAR *)lpName;
69 NameString.MaximumLength = NameString.Length;
71 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
72 ObjectAttributes.RootDirectory = hBaseDir;
73 ObjectAttributes.ObjectName = &NameString;
74 ObjectAttributes.Attributes = 0;
75 ObjectAttributes.SecurityDescriptor = NULL;
76 ObjectAttributes.SecurityQualityOfService = NULL;
77 if (lpSemaphoreAttributes != NULL)
79 ObjectAttributes.SecurityDescriptor = lpSemaphoreAttributes->lpSecurityDescriptor;
80 if (lpSemaphoreAttributes->bInheritHandle == TRUE)
82 ObjectAttributes.Attributes |= OBJ_INHERIT;
86 Status = NtCreateSemaphore(&SemaphoreHandle,
91 if (!NT_SUCCESS(Status))
93 SetLastErrorByStatus(Status);
96 return SemaphoreHandle;
101 OpenSemaphoreA(DWORD dwDesiredAccess,
102 WINBOOL bInheritHandle,
105 OBJECT_ATTRIBUTES ObjectAttributes;
106 UNICODE_STRING NameU;
113 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
117 RtlInitAnsiString(&Name,
119 RtlAnsiStringToUnicodeString(&NameU,
123 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
124 ObjectAttributes.RootDirectory = hBaseDir;
125 ObjectAttributes.ObjectName = &NameU;
126 ObjectAttributes.Attributes = 0;
127 ObjectAttributes.SecurityDescriptor = NULL;
128 ObjectAttributes.SecurityQualityOfService = NULL;
129 if (bInheritHandle == TRUE)
131 ObjectAttributes.Attributes |= OBJ_INHERIT;
134 Status = NtOpenSemaphore(&Handle,
135 (ACCESS_MASK)dwDesiredAccess,
138 RtlFreeUnicodeString(&NameU);
140 if (!NT_SUCCESS(Status))
142 SetLastErrorByStatus(Status);
151 OpenSemaphoreW(DWORD dwDesiredAccess,
152 WINBOOL bInheritHandle,
155 OBJECT_ATTRIBUTES ObjectAttributes;
162 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
166 RtlInitUnicodeString(&Name,
169 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
170 ObjectAttributes.RootDirectory = hBaseDir;
171 ObjectAttributes.ObjectName = &Name;
172 ObjectAttributes.Attributes = 0;
173 ObjectAttributes.SecurityDescriptor = NULL;
174 ObjectAttributes.SecurityQualityOfService = NULL;
175 if (bInheritHandle == TRUE)
177 ObjectAttributes.Attributes |= OBJ_INHERIT;
180 Status = NtOpenSemaphore(&Handle,
181 (ACCESS_MASK)dwDesiredAccess,
183 if (!NT_SUCCESS(Status))
185 SetLastErrorByStatus(Status);
194 ReleaseSemaphore(HANDLE hSemaphore,
196 LPLONG lpPreviousCount)
200 Status = NtReleaseSemaphore(hSemaphore,
203 if (!NT_SUCCESS(Status))
205 SetLastErrorByStatus(Status);