public static WaitableObject FromHandle(IntPtr handle)
            {
                if (handle == IntPtr.Zero || handle == new IntPtr(-1))
                {
                    WaitHandle.ThrowInvalidHandleException();
                }

                // We don't know if any other handles are invalid, and this may crash or otherwise do bad things, that is by
                // design, IntPtr is unsafe by nature.
                return((WaitableObject)GCHandle.FromIntPtr(handle).Target !);
            }
            public void UnsignalEvent()
            {
                s_lock.VerifyIsLocked();

                if (!IsEvent)
                {
                    WaitHandle.ThrowInvalidHandleException();
                }

                if (IsSignaled)
                {
                    --_signalCount;
                }
            }
            public void UnsignalEvent(ref LockHolder lockHolder)
            {
                s_lock.VerifyIsLocked();

                if (!IsEvent)
                {
                    lockHolder.Dispose();
                    WaitHandle.ThrowInvalidHandleException();
                }

                if (IsSignaled)
                {
                    --_signalCount;
                }
            }
            public void SignalMutex()
            {
                s_lock.VerifyIsLocked();

                if (!IsMutex)
                {
                    WaitHandle.ThrowInvalidHandleException();
                }

                if (IsSignaled || _ownershipInfo.Thread != RuntimeThread.CurrentThread)
                {
                    throw new ApplicationException(SR.Arg_SynchronizationLockException);
                }

                if (!_ownershipInfo.TryDecrementReacquireCount())
                {
                    SignalMutex(isAbandoned: false);
                }
            }
            public void SignalEvent()
            {
                s_lock.VerifyIsLocked();

                switch (_type)
                {
                case WaitableObjectType.ManualResetEvent:
                    SignalManualResetEvent();
                    break;

                case WaitableObjectType.AutoResetEvent:
                    SignalAutoResetEvent();
                    break;

                default:
                    WaitHandle.ThrowInvalidHandleException();
                    break;
                }
            }
Пример #6
0
            public int SignalSemaphore(int count)
            {
                s_lock.VerifyIsLocked();
                Debug.Assert(count > 0);

                if (!IsSemaphore)
                {
                    WaitHandle.ThrowInvalidHandleException();
                }

                int oldSignalCount = _signalCount;

                Debug.Assert(oldSignalCount <= _maximumSignalCount);
                if (count > _maximumSignalCount - oldSignalCount)
                {
                    throw new SemaphoreFullException();
                }

                if (oldSignalCount != 0)
                {
                    Debug.Assert(_waitersHead == null);
                    Debug.Assert(_waitersTail == null);

                    _signalCount = oldSignalCount + count;
                    return(oldSignalCount);
                }

                for (ThreadWaitInfo.WaitedListNode waiterNode = _waitersHead, nextWaiterNode;
                     waiterNode != null;
                     waiterNode = nextWaiterNode)
                {
                    // Signaling the waiter will unregister the waiter node, so keep the next node before trying
                    nextWaiterNode = waiterNode.Next;

                    if (waiterNode.WaitInfo.TrySignalToSatisfyWait(waiterNode, isAbandonedMutex: false) && --count == 0)
                    {
                        return(oldSignalCount);
                    }
                }

                _signalCount = count;
                return(oldSignalCount);
            }