Ejemplo n.º 1
0
        public Mutex(bool initiallyOwned, String name, out bool createdNew)
        {
            if (null != name && System.IO.Path.MAX_PATH < name.Length)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
            }
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
            SafeWaitHandle mutexHandle = null;
            bool           newMutex    = false;

            RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(MutexCleanupCode);
            MutexCleanupInfo           cleanupInfo = new MutexCleanupInfo(mutexHandle, false);

            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(
                delegate(object userData)  {  // try block
                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                }
                finally {
                    if (initiallyOwned)
                    {
                        cleanupInfo.inCriticalRegion = true;
                        Thread.BeginThreadAffinity();
                        Thread.BeginCriticalRegion();
                    }
                }

                int errorCode = 0;
                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                }
                finally {
                    errorCode = CreateMutexHandle(initiallyOwned, name, secAttrs, out mutexHandle);
                }

                if (mutexHandle.IsInvalid)
                {
                    mutexHandle.SetHandleAsInvalid();
                    if (null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
                    {
                        throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
                    }
                    __Error.WinIOError(errorCode, name);
                }
                newMutex = errorCode != Win32Native.ERROR_ALREADY_EXISTS;
                SetHandleInternal(mutexHandle);
                mutexHandle.SetAsMutex();

                hasThreadAffinity = true;
            },
                cleanupCode,
                cleanupInfo);
            createdNew = newMutex;
        }
Ejemplo n.º 2
0
            internal void MutexTryCode(object userData)
            {
                SafeWaitHandle mutexHandle = null;

                // try block
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    if (m_initiallyOwned)
                    {
                        m_cleanupInfo.inCriticalRegion = true;
#if !FEATURE_CORECLR
                        Thread.BeginThreadAffinity();
                        Thread.BeginCriticalRegion();
#endif //!FEATURE_CORECLR
                    }
                }

                int errorCode = 0;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    errorCode = CreateMutexHandle(m_initiallyOwned, m_name, m_secAttrs, out mutexHandle);
                }

                if (mutexHandle.IsInvalid)
                {
                    mutexHandle.SetHandleAsInvalid();
                    if (null != m_name && 0 != m_name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
                    {
                        throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", m_name));
                    }
                    __Error.WinIOError(errorCode, m_name);
                }
                m_newMutex = errorCode != Win32Native.ERROR_ALREADY_EXISTS;
                m_mutex.SetHandleInternal(mutexHandle);
                mutexHandle.SetAsMutex();

                m_mutex.hasThreadAffinity = true;
            }
Ejemplo n.º 3
0
            internal void MutexTryCode(object userData)
            {
                SafeWaitHandle mutexHandle = null;

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

                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    errorCode = Mutex.CreateMutexHandle(this.m_initiallyOwned, this.m_name, this.m_secAttrs, out mutexHandle);
                }
                if (mutexHandle.IsInvalid)
                {
                    mutexHandle.SetHandleAsInvalid();
                    if (((this.m_name != null) && (this.m_name.Length != 0)) && (6 == errorCode))
                    {
                        throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { this.m_name }));
                    }
                    __Error.WinIOError(errorCode, this.m_name);
                }
                this.m_newMutex = errorCode != 0xb7;
                this.m_mutex.SetHandleInternal(mutexHandle);
                mutexHandle.SetAsMutex();
                this.m_mutex.hasThreadAffinity = true;
            }
Ejemplo n.º 4
0
 private Mutex(SafeWaitHandle handle)
 {
     SetHandleInternal(handle);
     handle.SetAsMutex();
     hasThreadAffinity = true;
 }