-/* $Id: mutex.c,v 1.8 2004/01/23 21:16:04 ekohl Exp $
+/* $Id$
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
ANSI_STRING Name;
HANDLE Handle;
- RtlInitAnsiString(&Name,
- (LPSTR)lpName);
- RtlAnsiStringToUnicodeString(&NameU,
- &Name,
- TRUE);
+ if (lpName != NULL)
+ {
+ RtlInitAnsiString(&Name,
+ (LPSTR)lpName);
+
+ RtlAnsiStringToUnicodeString(&NameU,
+ &Name,
+ TRUE);
+ }
Handle = CreateMutexW(lpMutexAttributes,
bInitialOwner,
- NameU.Buffer);
+ (lpName ? NameU.Buffer : NULL));
- RtlFreeUnicodeString(&NameU);
+ if (lpName != NULL)
+ {
+ RtlFreeUnicodeString(&NameU);
+ }
return Handle;
}
{
OBJECT_ATTRIBUTES ObjectAttributes;
NTSTATUS Status;
- UNICODE_STRING NameString;
+ UNICODE_STRING UnicodeName;
HANDLE MutantHandle;
- RtlInitUnicodeString(&NameString,
- (LPWSTR)lpName);
+ if (lpName != NULL)
+ {
+ RtlInitUnicodeString(&UnicodeName,
+ (LPWSTR)lpName);
+ }
- ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
- ObjectAttributes.RootDirectory = hBaseDir;
- ObjectAttributes.ObjectName = &NameString;
- ObjectAttributes.Attributes = 0;
- ObjectAttributes.SecurityDescriptor = NULL;
- ObjectAttributes.SecurityQualityOfService = NULL;
+ InitializeObjectAttributes(&ObjectAttributes,
+ (lpName ? &UnicodeName : NULL),
+ 0,
+ (lpName ? hBaseDir : NULL),
+ NULL);
if (lpMutexAttributes != NULL)
{
ObjectAttributes.SecurityDescriptor = lpMutexAttributes->lpSecurityDescriptor;
- if (lpMutexAttributes->bInheritHandle == TRUE)
+ if (lpMutexAttributes->bInheritHandle)
{
ObjectAttributes.Attributes |= OBJ_INHERIT;
}
MUTEX_ALL_ACCESS,
&ObjectAttributes,
(BOOLEAN)bInitialOwner);
+ if (Status == STATUS_OBJECT_NAME_COLLISION)
+ {
+ Status = NtOpenMutant(&MutantHandle,
+ MUTEX_ALL_ACCESS,
+ &ObjectAttributes);
+ if (NT_SUCCESS(Status))
+ {
+ if(bInitialOwner)
+ {
+ WaitForSingleObject(MutantHandle, INFINITE);
+ }
+ SetLastError(ERROR_ALREADY_EXISTS);
+ }
+ }
if (!NT_SUCCESS(Status))
{
- SetLastErrorByStatus(Status);
- return NULL;
+ SetLastErrorByStatus(Status);
+ return NULL;
}
return MutantHandle;
&Name,
TRUE);
- ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
- ObjectAttributes.RootDirectory = hBaseDir;
- ObjectAttributes.ObjectName = &NameU;
- ObjectAttributes.Attributes = 0;
- ObjectAttributes.SecurityDescriptor = NULL;
- ObjectAttributes.SecurityQualityOfService = NULL;
- if (bInheritHandle == TRUE)
- {
- ObjectAttributes.Attributes |= OBJ_INHERIT;
- }
+ InitializeObjectAttributes(&ObjectAttributes,
+ &NameU,
+ (bInheritHandle ? OBJ_INHERIT : 0),
+ hBaseDir,
+ NULL);
Status = NtOpenMutant(&Handle,
(ACCESS_MASK)dwDesiredAccess,
RtlInitUnicodeString(&Name,
(LPWSTR)lpName);
- ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
- ObjectAttributes.RootDirectory = hBaseDir;
- ObjectAttributes.ObjectName = &Name;
- ObjectAttributes.Attributes = 0;
- ObjectAttributes.SecurityDescriptor = NULL;
- ObjectAttributes.SecurityQualityOfService = NULL;
- if (bInheritHandle == TRUE)
- {
- ObjectAttributes.Attributes |= OBJ_INHERIT;
- }
+ InitializeObjectAttributes(&ObjectAttributes,
+ &Name,
+ (bInheritHandle ? OBJ_INHERIT : 0),
+ hBaseDir,
+ NULL);
Status = NtOpenMutant(&Handle,
(ACCESS_MASK)dwDesiredAccess,