System.Threading.Mutex.CreateMutexHandle C# (CSharp) Method

CreateMutexHandle() private method

private CreateMutexHandle ( bool initiallyOwned, String name, Win32Native securityAttribute, SafeWaitHandle &mutexHandle ) : int
initiallyOwned bool
name String
securityAttribute Win32Native
mutexHandle SafeWaitHandle
return int
        static int CreateMutexHandle(bool initiallyOwned, String name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) {            
            int errorCode;  
            bool fReservedMutexObtained = false;
            bool fAffinity = false;
			
            while(true) {
                mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name);
                errorCode = Marshal.GetLastWin32Error();                                
                if( !mutexHandle.IsInvalid) {
                    break;                
                }

                if( errorCode == Win32Native.ERROR_ACCESS_DENIED) {
                    // If a mutex with the name already exists, OS will try to open it with FullAccess.
                    // It might fail if we don't have enough access. In that case, we try to open the mutex will modify and synchronize access.
                    //
                    
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try 
                    {
                        try 
                        {
                        } 
                        finally 
                        {
                            Thread.BeginThreadAffinity();
                            fAffinity = true;
                        }
                        AcquireReservedMutex(ref fReservedMutexObtained);		
                        mutexHandle = Win32Native.OpenMutex(Win32Native.MUTEX_MODIFY_STATE | Win32Native.SYNCHRONIZE, false, name);
                        if(!mutexHandle.IsInvalid)
                        {
                            errorCode = Win32Native.ERROR_ALREADY_EXISTS;
                        }
                        else
                        {
                            errorCode = Marshal.GetLastWin32Error();
                        }
                    }
                    finally 
                    {
                        if (fReservedMutexObtained)
                            ReleaseReservedMutex();
                        if (fAffinity)						
                            Thread.EndThreadAffinity();						                        
                    }

                    // There could be a race here, the other owner of the mutex can free the mutex,
                    // We need to retry creation in that case.
                    if( errorCode != Win32Native.ERROR_FILE_NOT_FOUND) {
                        if( errorCode == Win32Native.ERROR_SUCCESS) {
                            errorCode =  Win32Native.ERROR_ALREADY_EXISTS;
                        }                        
                        break;
                    }
                }
                else {
                    break;
                }
            }                        
            return errorCode;
        }
        

Usage Example

            internal void MutexTryCode(object userData)
            {
                SafeWaitHandle safeWaitHandle = null;

                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    if (this.m_initiallyOwned)
                    {
                        this.m_cleanupInfo.inCriticalRegion = true;
                        Thread.BeginThreadAffinity();
                        Thread.BeginCriticalRegion();
                    }
                }
                int num = 0;

                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    num = Mutex.CreateMutexHandle(this.m_initiallyOwned, this.m_name, this.m_secAttrs, out safeWaitHandle);
                }
                if (safeWaitHandle.IsInvalid)
                {
                    safeWaitHandle.SetHandleAsInvalid();
                    if (this.m_name != null && this.m_name.Length != 0 && 6 == num)
                    {
                        throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", new object[]
                        {
                            this.m_name
                        }));
                    }
                    __Error.WinIOError(num, this.m_name);
                }
                this.m_newMutex = (num != 183);
                this.m_mutex.SetHandleInternal(safeWaitHandle);
                this.m_mutex.hasThreadAffinity = true;
            }