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 *****************************************************************/
16 #include <internal/debug.h>
18 /* GLOBALS ******************************************************************/
20 POBJECT_TYPE ExSemaphoreObjectType
;
22 static GENERIC_MAPPING ExSemaphoreMapping
= {
23 STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
,
24 STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
,
25 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| SEMAPHORE_QUERY_STATE
,
26 SEMAPHORE_ALL_ACCESS
};
28 /* FUNCTIONS *****************************************************************/
31 NtpCreateSemaphore(PVOID ObjectBody
,
34 POBJECT_ATTRIBUTES ObjectAttributes
)
36 DPRINT("NtpCreateSemaphore(ObjectBody %x, Parent %x, RemainingPath %S)\n",
37 ObjectBody
, Parent
, RemainingPath
);
39 if (RemainingPath
!= NULL
&& wcschr(RemainingPath
+1, '\\') != NULL
)
41 return(STATUS_UNSUCCESSFUL
);
44 return(STATUS_SUCCESS
);
48 NtInitializeSemaphoreImplementation(VOID
)
50 ExSemaphoreObjectType
= ExAllocatePool(NonPagedPool
, sizeof(OBJECT_TYPE
));
52 RtlCreateUnicodeString(&ExSemaphoreObjectType
->TypeName
, L
"Semaphore");
54 ExSemaphoreObjectType
->Tag
= TAG('S', 'E', 'M', 'T');
55 ExSemaphoreObjectType
->MaxObjects
= ULONG_MAX
;
56 ExSemaphoreObjectType
->MaxHandles
= ULONG_MAX
;
57 ExSemaphoreObjectType
->TotalObjects
= 0;
58 ExSemaphoreObjectType
->TotalHandles
= 0;
59 ExSemaphoreObjectType
->PagedPoolCharge
= 0;
60 ExSemaphoreObjectType
->NonpagedPoolCharge
= sizeof(KSEMAPHORE
);
61 ExSemaphoreObjectType
->Mapping
= &ExSemaphoreMapping
;
62 ExSemaphoreObjectType
->Dump
= NULL
;
63 ExSemaphoreObjectType
->Open
= NULL
;
64 ExSemaphoreObjectType
->Close
= NULL
;
65 ExSemaphoreObjectType
->Delete
= NULL
;
66 ExSemaphoreObjectType
->Parse
= NULL
;
67 ExSemaphoreObjectType
->Security
= NULL
;
68 ExSemaphoreObjectType
->QueryName
= NULL
;
69 ExSemaphoreObjectType
->OkayToClose
= NULL
;
70 ExSemaphoreObjectType
->Create
= NtpCreateSemaphore
;
71 ExSemaphoreObjectType
->DuplicationNotify
= NULL
;
73 ObpCreateTypeObject(ExSemaphoreObjectType
);
77 NtCreateSemaphore(OUT PHANDLE SemaphoreHandle
,
78 IN ACCESS_MASK DesiredAccess
,
79 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
,
83 PKSEMAPHORE Semaphore
;
86 Status
= ObCreateObject(ExGetPreviousMode(),
87 ExSemaphoreObjectType
,
95 if (!NT_SUCCESS(Status
))
100 KeInitializeSemaphore(Semaphore
,
104 Status
= ObInsertObject ((PVOID
)Semaphore
,
111 ObDereferenceObject(Semaphore
);
118 NtOpenSemaphore(IN HANDLE SemaphoreHandle
,
119 IN ACCESS_MASK DesiredAccess
,
120 IN POBJECT_ATTRIBUTES ObjectAttributes
)
124 Status
= ObOpenObjectByName(ObjectAttributes
,
125 ExSemaphoreObjectType
,
137 NtQuerySemaphore(IN HANDLE SemaphoreHandle
,
138 IN SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass
,
139 OUT PVOID SemaphoreInformation
,
140 IN ULONG SemaphoreInformationLength
,
141 OUT PULONG ReturnLength OPTIONAL
)
143 PSEMAPHORE_BASIC_INFORMATION Info
;
144 PKSEMAPHORE Semaphore
;
147 Info
= (PSEMAPHORE_BASIC_INFORMATION
)SemaphoreInformation
;
149 if (SemaphoreInformationClass
> SemaphoreBasicInformation
)
150 return STATUS_INVALID_INFO_CLASS
;
152 if (SemaphoreInformationLength
< sizeof(SEMAPHORE_BASIC_INFORMATION
))
153 return STATUS_INFO_LENGTH_MISMATCH
;
155 Status
= ObReferenceObjectByHandle(SemaphoreHandle
,
156 SEMAPHORE_QUERY_STATE
,
157 ExSemaphoreObjectType
,
161 if (!NT_SUCCESS(Status
))
164 Info
->CurrentCount
= KeReadStateSemaphore(Semaphore
);
165 Info
->MaximumCount
= Semaphore
->Limit
;
167 if (ReturnLength
!= NULL
)
168 *ReturnLength
= sizeof(SEMAPHORE_BASIC_INFORMATION
);
170 ObDereferenceObject(Semaphore
);
172 return STATUS_SUCCESS
;
176 NtReleaseSemaphore(IN HANDLE SemaphoreHandle
,
177 IN LONG ReleaseCount
,
178 OUT PLONG PreviousCount OPTIONAL
)
180 PKSEMAPHORE Semaphore
;
183 Status
= ObReferenceObjectByHandle(SemaphoreHandle
,
184 SEMAPHORE_MODIFY_STATE
,
185 ExSemaphoreObjectType
,
189 if (!NT_SUCCESS(Status
))
193 KeReleaseSemaphore(Semaphore
,
197 ObDereferenceObject(Semaphore
);
198 return(STATUS_SUCCESS
);