Пример #1
0
        public Task <bool> WaitAsync(int millisecondsTimeout, CancellationToken cancellationToken)
        {
            this.CheckDispose();
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("totalMilliSeconds", millisecondsTimeout, SemaphoreSlim.GetResourceString("SemaphoreSlim_Wait_TimeoutWrong"));
            }
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCancellation <bool>(cancellationToken));
            }
            object      lockObj = this.m_lockObj;
            Task <bool> result;

            lock (lockObj)
            {
                if (this.m_currentCount > 0)
                {
                    this.m_currentCount--;
                    if (this.m_waitHandle != null && this.m_currentCount == 0)
                    {
                        this.m_waitHandle.Reset();
                    }
                    result = SemaphoreSlim.s_trueTask;
                }
                else
                {
                    SemaphoreSlim.TaskNode taskNode = this.CreateAndAddAsyncWaiter();
                    result = ((millisecondsTimeout == -1 && !cancellationToken.CanBeCanceled) ? taskNode : this.WaitUntilCountOrTimeoutAsync(taskNode, millisecondsTimeout, cancellationToken));
                }
            }
            return(result);
        }
Пример #2
0
        // Token: 0x06003DF7 RID: 15863 RVA: 0x000E6388 File Offset: 0x000E4588
        private async Task <bool> WaitUntilCountOrTimeoutAsync(SemaphoreSlim.TaskNode asyncWaiter, int millisecondsTimeout, CancellationToken cancellationToken)
        {
            using (CancellationTokenSource cts = cancellationToken.CanBeCanceled ? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, default(CancellationToken)) : new CancellationTokenSource())
            {
                Task <Task> task = Task.WhenAny(new Task[]
                {
                    asyncWaiter,
                    Task.Delay(millisecondsTimeout, cts.Token)
                });
                object obj   = asyncWaiter;
                Task   task2 = await task.ConfigureAwait(false);

                if (obj == task2)
                {
                    obj = null;
                    cts.Cancel();
                    return(true);
                }
            }
            CancellationTokenSource cts = null;
            object lockObj = this.m_lockObj;

            lock (lockObj)
            {
                if (this.RemoveAsyncWaiter(asyncWaiter))
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    return(false);
                }
            }
            return(await asyncWaiter.ConfigureAwait(false));
        }
Пример #3
0
        private bool RemoveAsyncWaiter(SemaphoreSlim.TaskNode task)
        {
            int num = this.m_asyncHead == task ? 1 : (task.Prev != null ? 1 : 0);

            if (task.Next != null)
            {
                task.Next.Prev = task.Prev;
            }
            if (task.Prev != null)
            {
                task.Prev.Next = task.Next;
            }
            if (this.m_asyncHead == task)
            {
                this.m_asyncHead = task.Next;
            }
            if (this.m_asyncTail == task)
            {
                this.m_asyncTail = task.Prev;
            }
            SemaphoreSlim.TaskNode taskNode1 = task;
            // ISSUE: variable of the null type
            __Null local;

            SemaphoreSlim.TaskNode taskNode2 = (SemaphoreSlim.TaskNode)(local = null);
            taskNode1.Prev = (SemaphoreSlim.TaskNode)local;
            SemaphoreSlim.TaskNode taskNode3 = taskNode2;
            taskNode1.Next = taskNode3;
            return(num != 0);
        }
Пример #4
0
 public Task <bool> WaitAsync(int millisecondsTimeout, CancellationToken cancellationToken)
 {
     this.CheckDispose();
     if (millisecondsTimeout < -1)
     {
         throw new ArgumentOutOfRangeException("totalMilliSeconds", (object)millisecondsTimeout, SemaphoreSlim.GetResourceString("SemaphoreSlim_Wait_TimeoutWrong"));
     }
     if (cancellationToken.IsCancellationRequested)
     {
         return(Task.FromCancellation <bool>(cancellationToken));
     }
     lock (this.m_lockObj)
     {
         if (this.m_currentCount > 0)
         {
             this.m_currentCount = this.m_currentCount - 1;
             if (this.m_waitHandle != null && this.m_currentCount == 0)
             {
                 this.m_waitHandle.Reset();
             }
             return(SemaphoreSlim.s_trueTask);
         }
         SemaphoreSlim.TaskNode local_3 = this.CreateAndAddAsyncWaiter();
         return(millisecondsTimeout != -1 || cancellationToken.CanBeCanceled ? this.WaitUntilCountOrTimeoutAsync(local_3, millisecondsTimeout, cancellationToken) : (Task <bool>)local_3);
     }
 }
Пример #5
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);
        }
Пример #6
0
 protected virtual void Dispose(bool disposing)
 {
     if (!disposing)
     {
         return;
     }
     if (this.m_waitHandle != null)
     {
         this.m_waitHandle.Close();
         this.m_waitHandle = (ManualResetEvent)null;
     }
     this.m_lockObj   = (object)null;
     this.m_asyncHead = (SemaphoreSlim.TaskNode)null;
     this.m_asyncTail = (SemaphoreSlim.TaskNode)null;
 }
Пример #7
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);
        }
Пример #8
0
 // Token: 0x06003DF5 RID: 15861 RVA: 0x000E62AC File Offset: 0x000E44AC
 private SemaphoreSlim.TaskNode CreateAndAddAsyncWaiter()
 {
     SemaphoreSlim.TaskNode taskNode = new SemaphoreSlim.TaskNode();
     if (this.m_asyncHead == null)
     {
         this.m_asyncHead = taskNode;
         this.m_asyncTail = taskNode;
     }
     else
     {
         this.m_asyncTail.Next = taskNode;
         taskNode.Prev         = this.m_asyncTail;
         this.m_asyncTail      = taskNode;
     }
     return(taskNode);
 }
Пример #9
0
        // Token: 0x06003DF6 RID: 15862 RVA: 0x000E62F8 File Offset: 0x000E44F8
        private bool RemoveAsyncWaiter(SemaphoreSlim.TaskNode task)
        {
            bool result = this.m_asyncHead == task || task.Prev != null;

            if (task.Next != null)
            {
                task.Next.Prev = task.Prev;
            }
            if (task.Prev != null)
            {
                task.Prev.Next = task.Next;
            }
            if (this.m_asyncHead == task)
            {
                this.m_asyncHead = task.Next;
            }
            if (this.m_asyncTail == task)
            {
                this.m_asyncTail = task.Prev;
            }
            task.Next = (task.Prev = null);
            return(result);
        }
Пример #10
0
        private async Task <bool> WaitUntilCountOrTimeoutAsync(SemaphoreSlim.TaskNode asyncWaiter, int millisecondsTimeout, CancellationToken cancellationToken)
        {
            CancellationTokenSource cts = cancellationToken.CanBeCanceled ? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, new CancellationToken()) : new CancellationTokenSource();

            try
            {
                Task <Task> task1 = Task.WhenAny((Task)asyncWaiter, Task.Delay(millisecondsTimeout, cts.Token));
                object      obj   = (object)asyncWaiter;
                int         num   = 0;
                Task        task2 = await task1.ConfigureAwait(num != 0);

                if (obj == task2)
                {
                    obj = (object)null;
                    cts.Cancel();
                    return(true);
                }
            }
            finally
            {
                if (cts != null)
                {
                    cts.Dispose();
                }
            }
            cts = (CancellationTokenSource)null;
            lock (this.m_lockObj)
            {
                if (this.RemoveAsyncWaiter(asyncWaiter))
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    return(false);
                }
            }
            return(await asyncWaiter.ConfigureAwait(false));
        }
Пример #11
0
 private static void QueueWaiterTask(SemaphoreSlim.TaskNode waiterTask)
 {
     ThreadPool.UnsafeQueueCustomWorkItem(waiterTask, false);
 }