3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/nt/mutant.c
23 * PURPOSE: Synchronization primitives
24 * PROGRAMMER: David Welch (welch@cwcom.net)
29 /* INCLUDES *****************************************************************/
32 #define NTOS_MODE_KERNEL
34 #include <ntos/synch.h>
37 #include <internal/debug.h>
39 POBJECT_TYPE ExMutantObjectType = NULL;
41 static GENERIC_MAPPING ExpMutantMapping = {
42 STANDARD_RIGHTS_READ | SYNCHRONIZE | MUTANT_QUERY_STATE,
43 STANDARD_RIGHTS_WRITE | SYNCHRONIZE,
44 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | MUTANT_QUERY_STATE,
47 /* FUNCTIONS *****************************************************************/
51 NtpCreateMutant(PVOID ObjectBody,
54 POBJECT_ATTRIBUTES ObjectAttributes)
56 DPRINT("NtpCreateMutant(ObjectBody %x, Parent %x, RemainingPath %S)\n",
57 ObjectBody, Parent, RemainingPath);
59 if (RemainingPath != NULL && wcschr(RemainingPath+1, '\\') != NULL)
61 return(STATUS_UNSUCCESSFUL);
64 return(STATUS_SUCCESS);
69 NtpDeleteMutant(PVOID ObjectBody)
71 DPRINT("NtpDeleteMutant(ObjectBody %x)\n", ObjectBody);
73 KeReleaseMutant((PKMUTANT)ObjectBody,
81 NtInitializeMutantImplementation(VOID)
83 ExMutantObjectType = ExAllocatePool(NonPagedPool,sizeof(OBJECT_TYPE));
85 RtlCreateUnicodeString(&ExMutantObjectType->TypeName, L"Mutant");
87 ExMutantObjectType->Tag = TAG('M', 'T', 'N', 'T');
88 ExMutantObjectType->MaxObjects = ULONG_MAX;
89 ExMutantObjectType->MaxHandles = ULONG_MAX;
90 ExMutantObjectType->TotalObjects = 0;
91 ExMutantObjectType->TotalHandles = 0;
92 ExMutantObjectType->PagedPoolCharge = 0;
93 ExMutantObjectType->NonpagedPoolCharge = sizeof(KMUTANT);
94 ExMutantObjectType->Mapping = &ExpMutantMapping;
95 ExMutantObjectType->Dump = NULL;
96 ExMutantObjectType->Open = NULL;
97 ExMutantObjectType->Close = NULL;
98 ExMutantObjectType->Delete = NtpDeleteMutant;
99 ExMutantObjectType->Parse = NULL;
100 ExMutantObjectType->Security = NULL;
101 ExMutantObjectType->QueryName = NULL;
102 ExMutantObjectType->OkayToClose = NULL;
103 ExMutantObjectType->Create = NtpCreateMutant;
104 ExMutantObjectType->DuplicationNotify = NULL;
109 NtCreateMutant(OUT PHANDLE MutantHandle,
110 IN ACCESS_MASK DesiredAccess,
111 IN POBJECT_ATTRIBUTES ObjectAttributes,
112 IN BOOLEAN InitialOwner)
117 Status = ObRosCreateObject(MutantHandle,
122 if (!NT_SUCCESS(Status))
126 KeInitializeMutant(Mutant,
128 ObDereferenceObject(Mutant);
130 return(STATUS_SUCCESS);
135 NtOpenMutant(OUT PHANDLE MutantHandle,
136 IN ACCESS_MASK DesiredAccess,
137 IN POBJECT_ATTRIBUTES ObjectAttributes)
139 return(ObOpenObjectByName(ObjectAttributes,
150 NtQueryMutant(IN HANDLE MutantHandle,
151 IN CINT MutantInformationClass,
152 OUT PVOID MutantInformation,
154 OUT PULONG ResultLength)
156 PMUTANT_BASIC_INFORMATION Info;
160 Info = (PMUTANT_BASIC_INFORMATION)MutantInformation;
162 if (MutantInformationClass > MutantBasicInformation)
163 return(STATUS_INVALID_INFO_CLASS);
165 if (Length < sizeof(MUTANT_BASIC_INFORMATION))
166 return(STATUS_INFO_LENGTH_MISMATCH);
168 Status = ObReferenceObjectByHandle(MutantHandle,
174 if (!NT_SUCCESS(Status))
179 Info->Count = KeReadStateMutant(Mutant);
180 Info->Owned = (Mutant->OwnerThread != NULL);
181 Info->Abandoned = Mutant->Abandoned;
183 ObDereferenceObject(Mutant);
185 return(STATUS_SUCCESS);
190 NtReleaseMutant(IN HANDLE MutantHandle,
191 IN PULONG ReleaseCount OPTIONAL)
197 Status = ObReferenceObjectByHandle(MutantHandle,
203 if (!NT_SUCCESS(Status))
208 Count = KeReleaseMutant(Mutant,
212 ObDereferenceObject(Mutant);
214 if (ReleaseCount != NULL)
216 *ReleaseCount = Count;
219 return(STATUS_SUCCESS);