1 /* $Id: ntsem.c,v 1.16 2002/09/07 15:13:04 chorns Exp $
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 *****************************************************************/
17 #include <internal/debug.h>
20 /* GLOBALS ******************************************************************/
22 POBJECT_TYPE ExSemaphoreType
;
24 static GENERIC_MAPPING ExSemaphoreMapping
= {
25 STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
,
26 STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
,
27 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| SEMAPHORE_QUERY_STATE
,
28 SEMAPHORE_ALL_ACCESS
};
30 /* FUNCTIONS *****************************************************************/
33 NtpCreateSemaphore(PVOID ObjectBody
,
36 POBJECT_ATTRIBUTES ObjectAttributes
)
38 DPRINT("NtpCreateSemaphore(ObjectBody %x, Parent %x, RemainingPath %S)\n",
39 ObjectBody
, Parent
, RemainingPath
);
41 if (RemainingPath
!= NULL
&& wcschr(RemainingPath
+1, '\\') != NULL
)
43 return(STATUS_UNSUCCESSFUL
);
46 return(STATUS_SUCCESS
);
49 VOID
NtInitializeSemaphoreImplementation(VOID
)
51 ExSemaphoreType
= ExAllocatePool(NonPagedPool
, sizeof(OBJECT_TYPE
));
53 RtlCreateUnicodeString(&ExSemaphoreType
->TypeName
, L
"Semaphore");
55 ExSemaphoreType
->Tag
= TAG('S', 'E', 'M', 'T');
56 ExSemaphoreType
->MaxObjects
= ULONG_MAX
;
57 ExSemaphoreType
->MaxHandles
= ULONG_MAX
;
58 ExSemaphoreType
->TotalObjects
= 0;
59 ExSemaphoreType
->TotalHandles
= 0;
60 ExSemaphoreType
->PagedPoolCharge
= 0;
61 ExSemaphoreType
->NonpagedPoolCharge
= sizeof(KSEMAPHORE
);
62 ExSemaphoreType
->Mapping
= &ExSemaphoreMapping
;
63 ExSemaphoreType
->Dump
= NULL
;
64 ExSemaphoreType
->Open
= NULL
;
65 ExSemaphoreType
->Close
= NULL
;
66 ExSemaphoreType
->Delete
= NULL
;
67 ExSemaphoreType
->Parse
= NULL
;
68 ExSemaphoreType
->Security
= NULL
;
69 ExSemaphoreType
->QueryName
= NULL
;
70 ExSemaphoreType
->OkayToClose
= NULL
;
71 ExSemaphoreType
->Create
= NtpCreateSemaphore
;
72 ExSemaphoreType
->DuplicationNotify
= NULL
;
76 NtCreateSemaphore(OUT PHANDLE SemaphoreHandle
,
77 IN ACCESS_MASK DesiredAccess
,
78 IN POBJECT_ATTRIBUTES ObjectAttributes
,
82 PKSEMAPHORE Semaphore
;
85 Status
= ObRosCreateObject(SemaphoreHandle
,
90 if (!NT_SUCCESS(Status
))
94 KeInitializeSemaphore(Semaphore
,
97 ObDereferenceObject(Semaphore
);
98 return(STATUS_SUCCESS
);
103 NtOpenSemaphore(IN HANDLE SemaphoreHandle
,
104 IN ACCESS_MASK DesiredAccess
,
105 IN POBJECT_ATTRIBUTES ObjectAttributes
)
109 Status
= ObOpenObjectByName(ObjectAttributes
,
122 NtQuerySemaphore(IN HANDLE SemaphoreHandle
,
123 IN SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass
,
124 OUT PVOID SemaphoreInformation
,
125 IN ULONG SemaphoreInformationLength
,
126 OUT PULONG ReturnLength
)
128 PSEMAPHORE_BASIC_INFORMATION Info
;
129 PKSEMAPHORE Semaphore
;
132 Info
= (PSEMAPHORE_BASIC_INFORMATION
)SemaphoreInformation
;
134 if (SemaphoreInformationClass
> SemaphoreBasicInformation
)
135 return STATUS_INVALID_INFO_CLASS
;
137 if (SemaphoreInformationLength
< sizeof(SEMAPHORE_BASIC_INFORMATION
))
138 return STATUS_INFO_LENGTH_MISMATCH
;
140 Status
= ObReferenceObjectByHandle(SemaphoreHandle
,
141 SEMAPHORE_QUERY_STATE
,
146 if (!NT_SUCCESS(Status
))
149 Info
->CurrentCount
= KeReadStateSemaphore(Semaphore
);
150 Info
->MaximumCount
= Semaphore
->Limit
;
152 *ReturnLength
= sizeof(SEMAPHORE_BASIC_INFORMATION
);
154 ObDereferenceObject(Semaphore
);
156 return STATUS_SUCCESS
;
160 NtReleaseSemaphore(IN HANDLE SemaphoreHandle
,
161 IN LONG ReleaseCount
,
162 OUT PLONG PreviousCount
)
164 PKSEMAPHORE Semaphore
;
167 Status
= ObReferenceObjectByHandle(SemaphoreHandle
,
168 SEMAPHORE_MODIFY_STATE
,
173 if (!NT_SUCCESS(Status
))
177 KeReleaseSemaphore(Semaphore
,
181 ObDereferenceObject(Semaphore
);
182 return(STATUS_SUCCESS
);