BeginThreadAffinity() public static method

public static BeginThreadAffinity ( ) : void
return void
Esempio n. 1
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 (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(Environment.GetResourceString("Argument_WaitHandleNameTooLong", PathInternal.MaxComponentLength), "name");
#endif

                        case Win32Native.ERROR_INVALID_HANDLE:
                            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);

                m_mutex.hasThreadAffinity = true;
            }
Esempio n. 2
0
        private static int CreateMutexHandle(bool initiallyOwned, string name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle)
        {
            bool bHandleObtained = false;
            bool flag2           = false;

Label_0004:
            mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name);
            int num = Marshal.GetLastWin32Error();

            if (mutexHandle.IsInvalid && (num == 5))
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    try
                    {
                    }
                    finally
                    {
                        Thread.BeginThreadAffinity();
                        flag2 = true;
                    }
                    AcquireReservedMutex(ref bHandleObtained);
                    mutexHandle = Win32Native.OpenMutex(0x100001, false, name);
                    if (!mutexHandle.IsInvalid)
                    {
                        num = 0xb7;
                    }
                    else
                    {
                        num = Marshal.GetLastWin32Error();
                    }
                }
                finally
                {
                    if (bHandleObtained)
                    {
                        ReleaseReservedMutex();
                    }
                    if (flag2)
                    {
                        Thread.EndThreadAffinity();
                    }
                }
                switch (num)
                {
                case 2:
                    goto Label_0004;

                case 0:
                    return(0xb7);
                }
            }
            return(num);
        }
Esempio n. 3
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;
        }
        private static int CreateMutexHandle(bool initiallyOwned, string name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle)
        {
            bool flag = false;
            int  num;

            do
            {
                mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name);
                num         = Marshal.GetLastWin32Error();
                if (!mutexHandle.IsInvalid || num != 5)
                {
                    return(num);
                }
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    try
                    {
                    }
                    finally
                    {
                        Thread.BeginThreadAffinity();
                        flag = true;
                    }
                    mutexHandle = Win32Native.OpenMutex(1048577, false, name);
                    if (!mutexHandle.IsInvalid)
                    {
                        num = 183;
                    }
                    else
                    {
                        num = Marshal.GetLastWin32Error();
                    }
                }
                finally
                {
                    if (flag)
                    {
                        Thread.EndThreadAffinity();
                    }
                }
            }while (num == 2);
            if (num == 0)
            {
                num = 183;
            }
            return(num);
        }
Esempio n. 5
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;
            }
            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;
            }
        public static CancellationRegion SetNonCancelable()
        {
            CancellationSignal signal = new CancellationSignal(false);
            Thread             t      = Thread.CurrentThread;

            signal.Thread = t;
            List <CancellationSignal> signals = t.CancellationSignals;
            CancellationRegion        region  = new CancellationRegion(signal);

            lock (signals) {
                signals.Add(signal);
                // Note that all failures due to allocations will be above
                // this point in this method.  Also, note that it's fine to
                // Add first then get thread affinity later - the Cancel
                // method holds this same lock while cancelling.
                // Ensure that another fiber cannot run on this thread.
                Thread.BeginThreadAffinity();
            }
            return(region);
        }
Esempio n. 8
0
        static int CreateMutexHandle(bool initiallyOwned, String name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle)
        {
            int  errorCode;
            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
                        {
#if !FEATURE_CORECLR
                            Thread.BeginThreadAffinity();
#endif
                            fAffinity = true;
                        }
                        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 (fAffinity)
                        {
#if !FEATURE_CORECLR
                            Thread.EndThreadAffinity();
#endif
                        }
                    }

                    // There could be a ---- 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);
        }
Esempio n. 9
0
 public IoBackgroundModeToken()
 {
     Thread.BeginThreadAffinity();
     NativeMethods.EnterBackgroundMode();
 }
Esempio n. 10
0
        public unsafe Mutex(bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
        {
            if ((name != null) && (260 < name.Length))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name }));
            }
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
            if (mutexSecurity != null)
            {
                secAttrs = new Win32Native.SECURITY_ATTRIBUTES {
                    nLength = Marshal.SizeOf(secAttrs)
                };
                byte[] securityDescriptorBinaryForm = mutexSecurity.GetSecurityDescriptorBinaryForm();
                byte * pDest = stackalloc byte[1 * securityDescriptorBinaryForm.Length];
                Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length);
                secAttrs.pSecurityDescriptor = pDest;
            }
            SafeWaitHandle mutexHandle = null;
            bool           newMutex    = false;

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

            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(delegate(object userData) {
                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 (((name != null) && (name.Length != 0)) && (6 == errorCode))
                    {
                        throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { name }));
                    }
                    __Error.WinIOError(errorCode, name);
                }
                newMutex = errorCode != 0xb7;
                this.SetHandleInternal(mutexHandle);
                this.hasThreadAffinity = true;
            }, backoutCode, cleanupInfo);
            createdNew = newMutex;
        }
Esempio n. 11
0
        private static int CreateMutexHandle(bool initiallyOwned, string name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle)
        {
            bool flag  = false;
            bool flag2 = false;
            bool flag3 = false;

Label_0006:
            flag2       = false;
            flag3       = false;
            mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name);
            int num = Marshal.GetLastWin32Error();

            if (!mutexHandle.IsInvalid || (num != 5))
            {
                return(num);
            }
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    Thread.BeginThreadAffinity();
                    flag = true;
                }
                mutexHandle = Win32Native.OpenMutex(0x100001, false, name);
                if (!mutexHandle.IsInvalid)
                {
                    num = 0xb7;
                    if (Environment.IsW2k3)
                    {
                        SafeWaitHandle handle = Win32Native.OpenMutex(0x100001, false, name);
                        if (!handle.IsInvalid)
                        {
                            RuntimeHelpers.PrepareConstrainedRegions();
                            try
                            {
                                uint     num2    = 0;
                                IntPtr   ptr     = mutexHandle.DangerousGetHandle();
                                IntPtr   ptr2    = handle.DangerousGetHandle();
                                IntPtr[] handles = new IntPtr[] { ptr, ptr2 };
                                num2 = Win32Native.WaitForMultipleObjects(2, handles, true, 0);
                                GC.KeepAlive(handles);
                                if (num2 == uint.MaxValue)
                                {
                                    if (Marshal.GetLastWin32Error() != 0x57)
                                    {
                                        mutexHandle.Dispose();
                                        flag3 = true;
                                    }
                                }
                                else
                                {
                                    flag2 = true;
                                    if ((num2 >= 0) && (num2 < 2))
                                    {
                                        Win32Native.ReleaseMutex(mutexHandle);
                                        Win32Native.ReleaseMutex(handle);
                                    }
                                    else if ((num2 >= 0x80) && (num2 < 130))
                                    {
                                        Win32Native.ReleaseMutex(mutexHandle);
                                        Win32Native.ReleaseMutex(handle);
                                    }
                                    mutexHandle.Dispose();
                                }
                                goto Label_0166;
                            }
                            finally
                            {
                                handle.Dispose();
                            }
                        }
                        mutexHandle.Dispose();
                        flag3 = true;
                    }
                }
                else
                {
                    num = Marshal.GetLastWin32Error();
                }
            }
            finally
            {
                if (flag)
                {
                    Thread.EndThreadAffinity();
                }
            }
Label_0166:
            if ((flag2 || flag3) || (num == 2))
            {
                goto Label_0006;
            }
            if (num == 0)
            {
                num = 0xb7;
            }
            return(num);
        }