예제 #1
0
        public static bool Wait(IntPtr handle, int timeoutMilliseconds)
        {
            Debug.Assert(timeoutMilliseconds >= -1);

            ThreadWaitInfo waitInfo = RuntimeThread.CurrentThread.WaitInfo;

            if (waitInfo.CheckAndResetPendingInterrupt)
            {
                throw new ThreadInterruptedException();
            }

            return(HandleManager.FromHandle(handle).Wait(waitInfo, timeoutMilliseconds));
        }
예제 #2
0
        public static int SignalAndWait(
            IntPtr handleToSignal,
            IntPtr handleToWaitOn,
            int timeoutMilliseconds)
        {
            Debug.Assert(timeoutMilliseconds >= -1);

            return
                (SignalAndWait(
                     HandleManager.FromHandle(handleToSignal),
                     HandleManager.FromHandle(handleToWaitOn),
                     timeoutMilliseconds));
        }
예제 #3
0
        public static void ReleaseMutex(IntPtr handle)
        {
            WaitableObject waitableObject = HandleManager.FromHandle(handle);

            s_lock.Acquire();
            try
            {
                waitableObject.SignalMutex();
            }
            finally
            {
                s_lock.Release();
            }
        }
예제 #4
0
        public static void ResetEvent(IntPtr handle)
        {
            WaitableObject waitableObject = HandleManager.FromHandle(handle);

            s_lock.Acquire();
            try
            {
                waitableObject.UnsignalEvent();
            }
            finally
            {
                s_lock.Release();
            }
        }
예제 #5
0
        public static int ReleaseSemaphore(IntPtr handle, int count)
        {
            Debug.Assert(count > 0);

            WaitableObject waitableObject = HandleManager.FromHandle(handle);

            s_lock.Acquire();
            try
            {
                return(waitableObject.SignalSemaphore(count));
            }
            finally
            {
                s_lock.Release();
            }
        }
예제 #6
0
        public static bool SignalAndWait(IntPtr handleToSignal, IntPtr handleToWaitOn, int timeoutMilliseconds)
        {
            Debug.Assert(timeoutMilliseconds >= -1);

            ThreadWaitInfo waitInfo = RuntimeThread.CurrentThread.WaitInfo;

            // A pending interrupt does not signal the specified handle
            if (waitInfo.CheckAndResetPendingInterrupt)
            {
                throw new ThreadInterruptedException();
            }

            WaitableObject waitableObjectToSignal = HandleManager.FromHandle(handleToSignal);
            WaitableObject waitableObjectToWaitOn = HandleManager.FromHandle(handleToWaitOn);

            bool waitCalled = false;

            s_lock.Acquire();
            try
            {
                waitableObjectToSignal.Signal(1);
                waitCalled = true;
                return(waitableObjectToWaitOn.Wait_Locked(waitInfo, timeoutMilliseconds));
            }
            finally
            {
                // Once the wait function is called, it will release the lock
                if (waitCalled)
                {
                    s_lock.VerifyIsNotLocked();
                }
                else
                {
                    s_lock.Release();
                }
            }
        }
예제 #7
0
        public static int Wait(
            Span <IntPtr> waitHandles,
            bool waitForAll,
            int timeoutMilliseconds)
        {
            Debug.Assert(waitHandles != null);
            Debug.Assert(waitHandles.Length > 0);
            Debug.Assert(waitHandles.Length <= WaitHandle.MaxWaitHandles);
            Debug.Assert(timeoutMilliseconds >= -1);

            ThreadWaitInfo waitInfo = Thread.CurrentThread.WaitInfo;

            WaitableObject?[] waitableObjects = waitInfo.GetWaitedObjectArray(waitHandles.Length);
            bool success = false;

            try
            {
                for (int i = 0; i < waitHandles.Length; ++i)
                {
                    Debug.Assert(waitHandles[i] != IntPtr.Zero);
                    WaitableObject waitableObject = HandleManager.FromHandle(waitHandles[i]);
                    if (waitForAll)
                    {
                        // Check if this is a duplicate, as wait-for-all does not support duplicates. Including the parent
                        // loop, this becomes a brute force O(n^2) search, which is intended since the typical array length is
                        // short enough that this would actually be faster than other alternatives. Also, the worst case is not
                        // so bad considering that the array length is limited by <see cref="WaitHandle.MaxWaitHandles"/>.
                        for (int j = 0; j < i; ++j)
                        {
                            if (waitableObject == waitableObjects[j])
                            {
                                throw new DuplicateWaitObjectException("waitHandles[" + i + ']');
                            }
                        }
                    }

                    waitableObjects[i] = waitableObject;
                }
                success = true;
            }
            finally
            {
                if (!success)
                {
                    for (int i = 0; i < waitHandles.Length; ++i)
                    {
                        waitableObjects[i] = null;
                    }
                }
            }

            if (waitHandles.Length == 1)
            {
                WaitableObject waitableObject = waitableObjects[0] !;
                waitableObjects[0] = null;
                return
                    (waitableObject.Wait(waitInfo, timeoutMilliseconds, interruptible: true, prioritize: false));
            }

            return
                (WaitableObject.Wait(
                     waitableObjects,
                     waitHandles.Length,
                     waitForAll,
                     waitInfo,
                     timeoutMilliseconds,
                     interruptible: true,
                     prioritize: false));
        }
예제 #8
0
 public static int Wait(IntPtr handle, int timeoutMilliseconds, bool interruptible)
 {
     Debug.Assert(timeoutMilliseconds >= -1);
     return(Wait(HandleManager.FromHandle(handle), timeoutMilliseconds, interruptible));
 }
예제 #9
0
 public static void ReleaseMutex(IntPtr handle)
 {
     ReleaseMutex(HandleManager.FromHandle(handle));
 }
예제 #10
0
 public static int ReleaseSemaphore(IntPtr handle, int count)
 {
     Debug.Assert(count > 0);
     return(ReleaseSemaphore(HandleManager.FromHandle(handle), count));
 }
예제 #11
0
 public static void ResetEvent(IntPtr handle)
 {
     ResetEvent(HandleManager.FromHandle(handle));
 }
예제 #12
0
        public static int Wait(
            RuntimeThread currentThread,
            SafeWaitHandle[] safeWaitHandles,
            WaitHandle[] waitHandles,
            bool waitForAll,
            int timeoutMilliseconds)
        {
            Debug.Assert(currentThread == RuntimeThread.CurrentThread);
            Debug.Assert(safeWaitHandles != null);
            Debug.Assert(safeWaitHandles.Length >= waitHandles.Length);
            Debug.Assert(waitHandles.Length > 0);
            Debug.Assert(waitHandles.Length <= WaitHandle.MaxWaitHandles);
            Debug.Assert(timeoutMilliseconds >= -1);

            ThreadWaitInfo waitInfo = currentThread.WaitInfo;

            if (waitInfo.CheckAndResetPendingInterrupt)
            {
                throw new ThreadInterruptedException();
            }

            int count = waitHandles.Length;

            WaitableObject[] waitableObjects = waitInfo.GetWaitedObjectArray(count);
            bool             success         = false;

            try
            {
                for (int i = 0; i < count; ++i)
                {
                    Debug.Assert(safeWaitHandles[i] != null);
                    WaitableObject waitableObject = HandleManager.FromHandle(safeWaitHandles[i].DangerousGetHandle());
                    if (waitForAll)
                    {
                        /// Check if this is a duplicate, as wait-for-all does not support duplicates. Including the parent
                        /// loop, this becomes a brute force O(n^2) search, which is intended since the typical array length is
                        /// short enough that this would actually be faster than other alternatives. Also, the worst case is not
                        /// so bad considering that the array length is limited by <see cref="WaitHandle.MaxWaitHandles"/>.
                        for (int j = 0; j < i; ++j)
                        {
                            if (waitableObject == waitableObjects[j])
                            {
                                throw new DuplicateWaitObjectException("waitHandles[" + i + ']');
                            }
                        }
                    }

                    waitableObjects[i] = waitableObject;
                }
                success = true;
            }
            finally
            {
                if (!success)
                {
                    for (int i = 0; i < count; ++i)
                    {
                        waitableObjects[i] = null;
                    }
                }
            }

            if (count == 1)
            {
                WaitableObject waitableObject = waitableObjects[0];
                waitableObjects[0] = null;
                return(waitableObject.Wait(waitInfo, timeoutMilliseconds) ? 0 : WaitHandle.WaitTimeout);
            }

            return
                (WaitableObject.Wait(
                     waitableObjects,
                     count,
                     waitForAll,
                     waitInfo,
                     timeoutMilliseconds,
                     waitHandles));
        }
예제 #13
0
 public static bool Wait(IntPtr handle, int timeoutMilliseconds)
 {
     Debug.Assert(timeoutMilliseconds >= -1);
     return(Wait(HandleManager.FromHandle(handle), timeoutMilliseconds));
 }