1 /* $Id: sem.c,v 1.4 2002/09/08 10:22:45 chorns Exp $
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 *****************************************************************/
14 #include <ddk/ntddk.h>
18 #include <kernel32/kernel32.h>
19 #include <kernel32/error.h>
21 /* FUNCTIONS ****************************************************************/
24 CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes
,
33 RtlInitAnsiString(&Name
,
35 RtlAnsiStringToUnicodeString(&NameU
,
39 Handle
= CreateSemaphoreW(lpSemaphoreAttributes
,
44 RtlFreeUnicodeString (&NameU
);
51 CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes
,
56 OBJECT_ATTRIBUTES ObjectAttributes
;
58 UNICODE_STRING NameString
;
59 HANDLE SemaphoreHandle
;
63 NameString
.Length
= lstrlenW(lpName
)*sizeof(WCHAR
);
67 NameString
.Length
= 0;
70 NameString
.Buffer
= (WCHAR
*)lpName
;
71 NameString
.MaximumLength
= NameString
.Length
;
73 ObjectAttributes
.Length
= sizeof(OBJECT_ATTRIBUTES
);
74 ObjectAttributes
.RootDirectory
= hBaseDir
;
75 ObjectAttributes
.ObjectName
= &NameString
;
76 ObjectAttributes
.Attributes
= 0;
77 ObjectAttributes
.SecurityDescriptor
= NULL
;
78 ObjectAttributes
.SecurityQualityOfService
= NULL
;
79 if (lpSemaphoreAttributes
!= NULL
)
81 ObjectAttributes
.SecurityDescriptor
= lpSemaphoreAttributes
->lpSecurityDescriptor
;
82 if (lpSemaphoreAttributes
->bInheritHandle
== TRUE
)
84 ObjectAttributes
.Attributes
|= OBJ_INHERIT
;
88 Status
= NtCreateSemaphore(&SemaphoreHandle
,
93 if (!NT_SUCCESS(Status
))
95 SetLastErrorByStatus(Status
);
98 return SemaphoreHandle
;
103 OpenSemaphoreA(DWORD dwDesiredAccess
,
104 WINBOOL bInheritHandle
,
107 OBJECT_ATTRIBUTES ObjectAttributes
;
108 UNICODE_STRING NameU
;
115 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
119 RtlInitAnsiString(&Name
,
121 RtlAnsiStringToUnicodeString(&NameU
,
125 ObjectAttributes
.Length
= sizeof(OBJECT_ATTRIBUTES
);
126 ObjectAttributes
.RootDirectory
= hBaseDir
;
127 ObjectAttributes
.ObjectName
= &NameU
;
128 ObjectAttributes
.Attributes
= 0;
129 ObjectAttributes
.SecurityDescriptor
= NULL
;
130 ObjectAttributes
.SecurityQualityOfService
= NULL
;
131 if (bInheritHandle
== TRUE
)
133 ObjectAttributes
.Attributes
|= OBJ_INHERIT
;
136 Status
= NtOpenSemaphore(&Handle
,
137 (ACCESS_MASK
)dwDesiredAccess
,
140 RtlFreeUnicodeString(&NameU
);
142 if (!NT_SUCCESS(Status
))
144 SetLastErrorByStatus(Status
);
153 OpenSemaphoreW(DWORD dwDesiredAccess
,
154 WINBOOL bInheritHandle
,
157 OBJECT_ATTRIBUTES ObjectAttributes
;
164 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
168 RtlInitUnicodeString(&Name
,
171 ObjectAttributes
.Length
= sizeof(OBJECT_ATTRIBUTES
);
172 ObjectAttributes
.RootDirectory
= hBaseDir
;
173 ObjectAttributes
.ObjectName
= &Name
;
174 ObjectAttributes
.Attributes
= 0;
175 ObjectAttributes
.SecurityDescriptor
= NULL
;
176 ObjectAttributes
.SecurityQualityOfService
= NULL
;
177 if (bInheritHandle
== TRUE
)
179 ObjectAttributes
.Attributes
|= OBJ_INHERIT
;
182 Status
= NtOpenSemaphore(&Handle
,
183 (ACCESS_MASK
)dwDesiredAccess
,
185 if (!NT_SUCCESS(Status
))
187 SetLastErrorByStatus(Status
);
196 ReleaseSemaphore(HANDLE hSemaphore
,
198 LPLONG lpPreviousCount
)
202 Status
= NtReleaseSemaphore(hSemaphore
,
205 if (!NT_SUCCESS(Status
))
207 SetLastErrorByStatus(Status
);