3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/nt/ntsem.c
6 * PURPOSE: Synchronization primitives
7 * PROGRAMMER: David Welch (welch@mcmail.com)
12 /* INCLUDES *****************************************************************/
15 #include <ddk/ntddk.h>
16 #include <ntos/synch.h>
17 #include <internal/pool.h>
18 #include <internal/ps.h>
21 #include <internal/debug.h>
23 /* GLOBALS ******************************************************************/
25 POBJECT_TYPE ExSemaphoreType;
27 static GENERIC_MAPPING ExSemaphoreMapping = {
28 STANDARD_RIGHTS_READ | SEMAPHORE_QUERY_STATE,
29 STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE,
30 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | SEMAPHORE_QUERY_STATE,
31 SEMAPHORE_ALL_ACCESS};
33 /* FUNCTIONS *****************************************************************/
36 NtpCreateSemaphore(PVOID ObjectBody,
39 POBJECT_ATTRIBUTES ObjectAttributes)
41 DPRINT("NtpCreateSemaphore(ObjectBody %x, Parent %x, RemainingPath %S)\n",
42 ObjectBody, Parent, RemainingPath);
44 if (RemainingPath != NULL && wcschr(RemainingPath+1, '\\') != NULL)
46 return(STATUS_UNSUCCESSFUL);
49 return(STATUS_SUCCESS);
52 VOID NtInitializeSemaphoreImplementation(VOID)
54 ExSemaphoreType = ExAllocatePool(NonPagedPool, sizeof(OBJECT_TYPE));
56 RtlCreateUnicodeString(&ExSemaphoreType->TypeName, L"Semaphore");
58 ExSemaphoreType->Tag = TAG('S', 'E', 'M', 'T');
59 ExSemaphoreType->MaxObjects = ULONG_MAX;
60 ExSemaphoreType->MaxHandles = ULONG_MAX;
61 ExSemaphoreType->TotalObjects = 0;
62 ExSemaphoreType->TotalHandles = 0;
63 ExSemaphoreType->PagedPoolCharge = 0;
64 ExSemaphoreType->NonpagedPoolCharge = sizeof(KSEMAPHORE);
65 ExSemaphoreType->Mapping = &ExSemaphoreMapping;
66 ExSemaphoreType->Dump = NULL;
67 ExSemaphoreType->Open = NULL;
68 ExSemaphoreType->Close = NULL;
69 ExSemaphoreType->Delete = NULL;
70 ExSemaphoreType->Parse = NULL;
71 ExSemaphoreType->Security = NULL;
72 ExSemaphoreType->QueryName = NULL;
73 ExSemaphoreType->OkayToClose = NULL;
74 ExSemaphoreType->Create = NtpCreateSemaphore;
75 ExSemaphoreType->DuplicationNotify = NULL;
79 NtCreateSemaphore(OUT PHANDLE SemaphoreHandle,
80 IN ACCESS_MASK DesiredAccess,
81 IN POBJECT_ATTRIBUTES ObjectAttributes,
85 PKSEMAPHORE Semaphore;
88 Status = ObCreateObject(SemaphoreHandle,
93 if (!NT_SUCCESS(Status))
97 KeInitializeSemaphore(Semaphore,
100 ObDereferenceObject(Semaphore);
101 return(STATUS_SUCCESS);
106 NtOpenSemaphore(IN HANDLE SemaphoreHandle,
107 IN ACCESS_MASK DesiredAccess,
108 IN POBJECT_ATTRIBUTES ObjectAttributes)
112 Status = ObOpenObjectByName(ObjectAttributes,
125 NtQuerySemaphore(IN HANDLE SemaphoreHandle,
126 IN SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass,
127 OUT PVOID SemaphoreInformation,
128 IN ULONG SemaphoreInformationLength,
129 OUT PULONG ReturnLength)
131 PSEMAPHORE_BASIC_INFORMATION Info;
132 PKSEMAPHORE Semaphore;
135 Info = (PSEMAPHORE_BASIC_INFORMATION)SemaphoreInformation;
137 if (SemaphoreInformationClass > SemaphoreBasicInformation)
138 return STATUS_INVALID_INFO_CLASS;
140 if (SemaphoreInformationLength < sizeof(SEMAPHORE_BASIC_INFORMATION))
141 return STATUS_INFO_LENGTH_MISMATCH;
143 Status = ObReferenceObjectByHandle(SemaphoreHandle,
144 SEMAPHORE_QUERY_STATE,
149 if (!NT_SUCCESS(Status))
152 Info->CurrentCount = KeReadStateSemaphore(Semaphore);
153 Info->MaximumCount = Semaphore->Limit;
155 *ReturnLength = sizeof(SEMAPHORE_BASIC_INFORMATION);
157 ObDereferenceObject(Semaphore);
159 return STATUS_SUCCESS;
163 NtReleaseSemaphore(IN HANDLE SemaphoreHandle,
164 IN LONG ReleaseCount,
165 OUT PLONG PreviousCount)
167 PKSEMAPHORE Semaphore;
170 Status = ObReferenceObjectByHandle(SemaphoreHandle,
171 SEMAPHORE_MODIFY_STATE,
176 if (!NT_SUCCESS(Status))
180 KeReleaseSemaphore(Semaphore,
184 ObDereferenceObject(Semaphore);
185 return(STATUS_SUCCESS);