Win32OperatingSystem.st
changeset 20591 e4155b00819c
parent 20562 d41673799706
child 20600 222ed6c9364e
child 20677 e3fe2f476abb
--- a/Win32OperatingSystem.st	Tue Oct 11 01:57:32 2016 +0200
+++ b/Win32OperatingSystem.st	Tue Oct 11 12:32:44 2016 +0200
@@ -173,6 +173,13 @@
 	privateIn:Win32OperatingSystem
 !
 
+Win32Handle subclass:#Win32MutexHandle
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	privateIn:Win32OperatingSystem
+!
+
 Win32Handle subclass:#Win32NetworkResourceHandle
 	instanceVariableNames:''
 	classVariableNames:'ScopeMappingTable TypeMappingTable DisplayTypeMappingTable
@@ -967,7 +974,6 @@
     "Modified: 7.1.1997 / 19:36:11 / stefan"
 ! !
 
-
 !Win32OperatingSystem class methodsFor:'OS signal constants'!
 
 sigABRT
@@ -7764,16 +7770,14 @@
 primGetLastError
     "get the last error code"
 %{  /* NOCONTEXT */
-    DWORD e;
-
-    e = GetLastError();
+    DWORD e = GetLastError();
     RETURN(__MKUINT(e));
 %}.
 
     "/ <apicall: dword "GetLastError" () module: "kernel32.dll" >
 
     "
-	self primGetLastError
+        self primGetLastError
     "
 !
 
@@ -7835,25 +7839,20 @@
 
     |handle lastErrorCode|
 
-    "/ "Without clear reasons, before creating the mutex we must call #printCR"
-    "/ 'Creating mutex' printCR.
     self primSetLastError:0.
     self primGetLastError.
     handle := self primCreateMutex:nil initialOwner:true name:name.
     lastErrorCode := self primGetLastError.
     "/ lastErrorCode printCR.
-
-    "/    self assert: lastErrorCode == 0.
     "/    lastErrorCode == 5 "ERROR_ACCESS_DENIED" ifTrue:[Transcript showCR: 'Mutex not accesible (GetLastError = ERROR_ACCESS_DENIED)'.].
     "/    lastErrorCode == 183 "ERROR_ALREADY_EXISTS" ifTrue:[Transcript showCR: 'Mutex already exists (GetLastError = ERROR_ALREADY_EXISTS)'.].
-    (handle isNil or:[handle address ~~ 0]) ifFalse:[
-	Transcript showCR: 'CreateMutexNamed: "', name printString, '" failed'.
-	handle := nil.
-    ].
     ^ Array with: handle with: lastErrorCode
 
     "
-     self createMutexNamed: '8906f5e0-54ed-11dd-9da4-001558137da0'
+     |arr|
+     arr := self createMutexNamed: '8906f5e0-54ed-11dd-9da4-001558137da0'.
+     self releaseMutex: arr first.
+
      self releaseMutexNamed: '8906f5e0-54ed-11dd-9da4-001558137da0'
     "
 
@@ -7885,10 +7884,6 @@
     "/    lastErrorCode := self primGetLastError.
     "/    lastErrorCode = 2 ifTrue:[Transcript showCR: 'Mutex does not exist (GetLastError = ERROR_FILE_NOT_FOUND)'.].
     "/    lastErrorCode = 5 ifTrue:[Transcript showCR: 'Mutex not accessable (GetLastError = ERROR_ACCESS_DENIED)'.].
-    (handle isNil or:[handle address ~~ 0]) ifFalse:[
-	Transcript showCR: 'OpenMutexNamed: "', name printString, '" failed'.
-	^ nil.
-    ].
     ^ handle
 
     "
@@ -7900,32 +7895,34 @@
 
 primCreateMutex:lpSecurityDescriptor initialOwner:bInitialOwner name:lpName
     "If the function succeeds, the return value is a handle to the newly created mutex object.
-     If the function fails, the return value is NULL.
+     If the function fails, the return value is nil.
      If the mutex is a named mutex and the object existed before this function call, the return value is a handle to the existing object."
 
     |handle|
 
-    handle := Win32Handle new.
+    handle := Win32MutexHandle new.
 %{
     if (__isString(lpName)
      && ((bInitialOwner == true) || (bInitialOwner == false))) {
-	void *c_descr = NULL;
-	char *c_name;
-	BOOL c_initialOwner = (bInitialOwner == true);
-	HANDLE c_handle;
-
-	c_name = __stringVal(lpName);
-
-	if (lpSecurityDescriptor != nil) {
-	    if (__isExternalAddressLike(lpSecurityDescriptor)
-	     || __isExternalBytesLike(lpSecurityDescriptor) ) {
-		c_descr = __externalAddressVal(lpSecurityDescriptor);
-	    } else
-		goto badArg;
-	}
-	c_handle = CreateMutexA(c_descr, c_initialOwner, c_name);
-	__externalAddressVal(handle) = c_handle;
-	RETURN(handle);
+        void *c_descr = NULL;
+        char *c_name;
+        HANDLE c_handle;
+
+        c_name = __stringVal(lpName);
+
+        if (lpSecurityDescriptor != nil) {
+            if (__isExternalAddressLike(lpSecurityDescriptor)
+             || __isExternalBytesLike(lpSecurityDescriptor) ) {
+                c_descr = __externalAddressVal(lpSecurityDescriptor);
+            } else
+                goto badArg;
+        }
+        c_handle = CreateMutexA(c_descr, bInitialOwner == true, c_name);
+        if (c_handle == NULL) {
+            RETURN(nil);
+        }
+        __externalAddressVal(handle) = c_handle;
+        RETURN(handle);
     }
     badArg: ;
 %}.
@@ -7937,31 +7934,34 @@
 
 primOpenMutex:dwDesiredAccess initialOwner:bInitialOwner name:lpName
     "If the function succeeds, the return value is a handle to the mutex object.
-     If the function fails, the return value is NULL. To get extended error information, call GetLastError.
+     If the function fails, the return value is nil. To get extended error information, call GetLastError.
      If a named mutex does not exist, the function fails and GetLastError returns ERROR_FILE_NOT_FOUND."
 
     |handle|
 
-    handle := Win32Handle new.
+    handle := Win32MutexHandle new.
 %{
     if (__isString(lpName)
      && ((bInitialOwner == true) || (bInitialOwner == false))) {
-	DWORD c_dwDesiredAccess = 0;
-	char *c_name;
-	BOOL c_initialOwner = (bInitialOwner == true);
-	HANDLE c_handle;
-
-	c_name = __stringVal(lpName);
-
-	if (dwDesiredAccess != nil) {
-	    if (! __isSmallInteger(dwDesiredAccess)) {
-		goto badArg;
-	    }
-	    c_dwDesiredAccess = __intVal(dwDesiredAccess);
-	}
-	c_handle = OpenMutexA(c_dwDesiredAccess, c_initialOwner, c_name);
-	__externalAddressVal(handle) = c_handle;
-	RETURN(handle);
+        DWORD c_dwDesiredAccess = 0;
+        char *c_name;
+        BOOL c_initialOwner = (bInitialOwner == true);
+        HANDLE c_handle;
+
+        c_name = __stringVal(lpName);
+
+        if (dwDesiredAccess != nil) {
+            if (! __isSmallInteger(dwDesiredAccess)) {
+                goto badArg;
+            }
+            c_dwDesiredAccess = __intVal(dwDesiredAccess);
+        }
+        c_handle = OpenMutexA(c_dwDesiredAccess, c_initialOwner, c_name);
+        if (c_handle == NULL) {
+            RETURN(nil);
+        }
+        __externalAddressVal(handle) = c_handle;
+        RETURN(handle);
     }
     badArg: ;
 %}.
@@ -17151,6 +17151,15 @@
     self unregisterForFinalization.
 ! !
 
+!Win32OperatingSystem::Win32MutexHandle class methodsFor:'documentation'!
+
+documentation
+"
+    I represent a mutex (can be used from more than a single OS processe).
+    I can be waited upon in a WaitForHandle / WaitForMultipleObjects call.
+"
+! !
+
 !Win32OperatingSystem::Win32NetworkResourceHandle class methodsFor:'accessing - types'!
 
 displayTypeMappingTable