Ejemplo n.º 1
0
        private void CreateMutexCore(bool initiallyOwned, string name, out bool createdNew)
        {
#if !PLATFORM_UNIX
            Debug.Assert(name == null || name.Length <= Interop.Kernel32.MAX_PATH);
#endif

            uint mutexFlags = initiallyOwned ? Win32Native.CREATE_MUTEX_INITIAL_OWNER : 0;

            SafeWaitHandle mutexHandle = Win32Native.CreateMutexEx(null, name, mutexFlags, AccessRights);
            int            errorCode   = Marshal.GetLastWin32Error();

            if (mutexHandle.IsInvalid)
            {
                mutexHandle.SetHandleAsInvalid();
#if PLATFORM_UNIX
                if (errorCode == Interop.Errors.ERROR_FILENAME_EXCED_RANGE)
                {
                    // On Unix, length validation is done by CoreCLR's PAL after converting to utf-8
                    throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, WaitHandleNameMax), nameof(name));
                }
#endif
                if (errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
                {
                    throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name));
                }
                throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
            }

            createdNew     = errorCode != Interop.Errors.ERROR_ALREADY_EXISTS;
            SafeWaitHandle = mutexHandle;
        }
Ejemplo n.º 2
0
            internal void MutexTryCode(object userData)
            {
                // try block
                if (m_initiallyOwned)
                {
                    m_cleanupInfo.inCriticalRegion = true;
                }

                uint           mutexFlags  = m_initiallyOwned ? Win32Native.CREATE_MUTEX_INITIAL_OWNER : 0;
                SafeWaitHandle mutexHandle = Win32Native.CreateMutexEx(m_secAttrs, m_name, mutexFlags, AccessRights);

                int errorCode = Marshal.GetLastWin32Error();

                if (mutexHandle.IsInvalid)
                {
                    mutexHandle.SetHandleAsInvalid();
                    if (m_name != null)
                    {
                        switch (errorCode)
                        {
#if PLATFORM_UNIX
                        case Win32Native.ERROR_FILENAME_EXCED_RANGE:
                            // On Unix, length validation is done by CoreCLR's PAL after converting to utf-8
                            throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, Interop.Sys.MaxName), "name");
#endif

                        case Win32Native.ERROR_INVALID_HANDLE:
                            throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, m_name));
                        }
                    }
                    __Error.WinIOError(errorCode, m_name);
                }
                m_newMutex = errorCode != Win32Native.ERROR_ALREADY_EXISTS;
                m_mutex.SetHandleInternal(mutexHandle);

                m_mutex.hasThreadAffinity = true;
            }