Beispiel #1
0
        public int Release(int releaseCount)
        {
            this.CheckDispose();
            if (releaseCount < 1)
            {
                throw new ArgumentOutOfRangeException("releaseCount", (object)releaseCount, SemaphoreSlim.GetResourceString("SemaphoreSlim_Release_CountWrong"));
            }
            int num;

            lock (this.m_lockObj)
            {
                int local_3 = this.m_currentCount;
                num = local_3;
                if (this.m_maxCount - local_3 < releaseCount)
                {
                    throw new SemaphoreFullException();
                }
                int local_3_1 = local_3 + releaseCount;
                int local_4   = this.m_waitCount;
                if (local_3_1 == 1 || local_4 == 1)
                {
                    Monitor.Pulse(this.m_lockObj);
                }
                else if (local_4 > 1)
                {
                    Monitor.PulseAll(this.m_lockObj);
                }
                if (this.m_asyncHead != null)
                {
                    int local_5 = local_3_1 - local_4;
                    while (local_5 > 0 && this.m_asyncHead != null)
                    {
                        --local_3_1;
                        --local_5;
                        SemaphoreSlim.TaskNode local_6 = this.m_asyncHead;
                        this.RemoveAsyncWaiter(local_6);
                        SemaphoreSlim.QueueWaiterTask(local_6);
                    }
                }
                this.m_currentCount = local_3_1;
                if (this.m_waitHandle != null)
                {
                    if (num == 0)
                    {
                        if (local_3_1 > 0)
                        {
                            this.m_waitHandle.Set();
                        }
                    }
                }
            }
            return(num);
        }
Beispiel #2
0
        public int Release(int releaseCount)
        {
            this.CheckDispose();
            if (releaseCount < 1)
            {
                throw new ArgumentOutOfRangeException("releaseCount", releaseCount, SemaphoreSlim.GetResourceString("SemaphoreSlim_Release_CountWrong"));
            }
            object lockObj = this.m_lockObj;
            int    num2;

            lock (lockObj)
            {
                int num = this.m_currentCount;
                num2 = num;
                if (this.m_maxCount - num < releaseCount)
                {
                    throw new SemaphoreFullException();
                }
                num += releaseCount;
                int waitCount = this.m_waitCount;
                if (num == 1 || waitCount == 1)
                {
                    Monitor.Pulse(this.m_lockObj);
                }
                else if (waitCount > 1)
                {
                    Monitor.PulseAll(this.m_lockObj);
                }
                if (this.m_asyncHead != null)
                {
                    int num3 = num - waitCount;
                    while (num3 > 0 && this.m_asyncHead != null)
                    {
                        num--;
                        num3--;
                        SemaphoreSlim.TaskNode asyncHead = this.m_asyncHead;
                        this.RemoveAsyncWaiter(asyncHead);
                        SemaphoreSlim.QueueWaiterTask(asyncHead);
                    }
                }
                this.m_currentCount = num;
                if (this.m_waitHandle != null && num2 == 0 && num > 0)
                {
                    this.m_waitHandle.Set();
                }
            }
            return(num2);
        }