28f78f1876f86cf38433ebe16432a8b0b1e2e77f
[reactos.git] / lib / kernel32 / synch / sem.c
1 /* $Id$
2  *
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)
8  * UPDATE HISTORY:
9  *                  Created 01/20/2001
10  */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <k32.h>
15
16 #define NDEBUG
17 #include <kernel32/kernel32.h>
18
19 /* FUNCTIONS ****************************************************************/
20
21 HANDLE STDCALL
22 CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
23                  LONG lInitialCount,
24                  LONG lMaximumCount,
25                  LPCSTR lpName)
26 {
27    UNICODE_STRING NameU;
28    ANSI_STRING Name;
29    HANDLE Handle;
30
31    RtlInitAnsiString(&Name,
32                      (LPSTR)lpName);
33    RtlAnsiStringToUnicodeString(&NameU,
34                                 &Name,
35                                 TRUE);
36
37    Handle = CreateSemaphoreW(lpSemaphoreAttributes,
38                              lInitialCount,
39                              lMaximumCount,
40                              NameU.Buffer);
41
42    RtlFreeUnicodeString (&NameU);
43
44    return Handle;
45 }
46
47
48 HANDLE STDCALL
49 CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
50                  LONG lInitialCount,
51                  LONG lMaximumCount,
52                  LPCWSTR lpName)
53 {
54    OBJECT_ATTRIBUTES ObjectAttributes;
55    NTSTATUS Status;
56    UNICODE_STRING NameString;
57    HANDLE SemaphoreHandle;
58
59    if (lpName)
60      {
61         NameString.Length = lstrlenW(lpName)*sizeof(WCHAR);
62      }
63    else
64      {
65         NameString.Length = 0;
66      }
67
68    NameString.Buffer = (WCHAR *)lpName;
69    NameString.MaximumLength = NameString.Length;
70
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)
78      {
79         ObjectAttributes.SecurityDescriptor = lpSemaphoreAttributes->lpSecurityDescriptor;
80         if (lpSemaphoreAttributes->bInheritHandle == TRUE)
81           {
82              ObjectAttributes.Attributes |= OBJ_INHERIT;
83           }
84      }
85
86    Status = NtCreateSemaphore(&SemaphoreHandle,
87                               SEMAPHORE_ALL_ACCESS,
88                               &ObjectAttributes,
89                               lInitialCount,
90                               lMaximumCount);
91    if (!NT_SUCCESS(Status))
92      {
93         SetLastErrorByStatus(Status);
94         return NULL;
95      }
96    return SemaphoreHandle;
97 }
98
99
100 HANDLE STDCALL
101 OpenSemaphoreA(DWORD dwDesiredAccess,
102                WINBOOL bInheritHandle,
103                LPCSTR lpName)
104 {
105    OBJECT_ATTRIBUTES ObjectAttributes;
106    UNICODE_STRING NameU;
107    ANSI_STRING Name;
108    HANDLE Handle;
109    NTSTATUS Status;
110
111    if (lpName == NULL)
112      {
113         SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
114         return NULL;
115      }
116
117    RtlInitAnsiString(&Name,
118                      (LPSTR)lpName);
119    RtlAnsiStringToUnicodeString(&NameU,
120                                 &Name,
121                                 TRUE);
122
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)
130      {
131         ObjectAttributes.Attributes |= OBJ_INHERIT;
132      }
133
134    Status = NtOpenSemaphore(&Handle,
135                             (ACCESS_MASK)dwDesiredAccess,
136                             &ObjectAttributes);
137
138    RtlFreeUnicodeString(&NameU);
139
140    if (!NT_SUCCESS(Status))
141      {
142         SetLastErrorByStatus(Status);
143         return NULL;
144      }
145
146    return Handle;
147 }
148
149
150 HANDLE STDCALL
151 OpenSemaphoreW(DWORD dwDesiredAccess,
152                WINBOOL bInheritHandle,
153                LPCWSTR lpName)
154 {
155    OBJECT_ATTRIBUTES ObjectAttributes;
156    UNICODE_STRING Name;
157    HANDLE Handle;
158    NTSTATUS Status;
159
160    if (lpName == NULL)
161      {
162         SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
163         return NULL;
164      }
165
166    RtlInitUnicodeString(&Name,
167                         (LPWSTR)lpName);
168
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)
176      {
177         ObjectAttributes.Attributes |= OBJ_INHERIT;
178      }
179
180    Status = NtOpenSemaphore(&Handle,
181                             (ACCESS_MASK)dwDesiredAccess,
182                             &ObjectAttributes);
183    if (!NT_SUCCESS(Status))
184      {
185         SetLastErrorByStatus(Status);
186         return NULL;
187      }
188
189    return Handle;
190 }
191
192
193 WINBOOL STDCALL
194 ReleaseSemaphore(HANDLE hSemaphore,
195                  LONG lReleaseCount,
196                  LPLONG lpPreviousCount)
197 {
198    NTSTATUS Status;
199
200    Status = NtReleaseSemaphore(hSemaphore,
201                                lReleaseCount,
202                                lpPreviousCount);
203    if (!NT_SUCCESS(Status))
204      {
205         SetLastErrorByStatus(Status);
206         return FALSE;
207      }
208
209    return TRUE;
210 }
211
212 /* EOF */