If a mutex already exist, open it instead of create.
[reactos.git] / reactos / lib / kernel32 / synch / mutex.c
index 9e98350..9752796 100644 (file)
@@ -1,4 +1,4 @@
-/* $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
@@ -31,17 +31,24 @@ CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes,
    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;
 }
@@ -57,23 +64,25 @@ CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes,
 {
    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;
          }
@@ -83,10 +92,24 @@ CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes,
                           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;
@@ -119,16 +142,11 @@ OpenMutexA(DWORD dwDesiredAccess,
                                &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,
@@ -168,16 +186,11 @@ OpenMutexW(DWORD 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,