private void WaitExtracted(CancellationToken cancellationToken)
        {
            var count = 0;
            var start = ThreadingHelper.TicksNow();

retry:
            if (!IsSet)
            {
                cancellationToken.ThrowIfCancellationRequested();
                GC.KeepAlive(cancellationToken.WaitHandle);
                if (ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start) < INT_LongTimeOutHint)
                {
                    ThreadingHelper.SpinOnce(ref count);
                    goto retry;
                }
                var handle = RetriveWaitHandle();
                WaitHandle.WaitAny
                (
                    new[]
                {
                    handle,
                    cancellationToken.WaitHandle
                }
                );
                cancellationToken.ThrowIfCancellationRequested();
                GC.KeepAlive(cancellationToken.WaitHandle);
            }
        }
        private static async Task <bool> WaitPrivateAsync(SemaphoreSlim semaphore, int millisecondsTimeout, CancellationToken cancellationToken)
        {
            if (semaphore.Wait(0))
            {
                return(true);
            }
            var start   = ThreadingHelper.TicksNow();
            var timeout = millisecondsTimeout;

            while
            (
                timeout > 0 &&
                await TaskEx.FromWaitHandleInternal
                (
                    semaphore.AvailableWaitHandle,
                    timeout,
                    cancellationToken
                ).ConfigureAwait(false)
            )
            {
                if (semaphore.Wait(0))
                {
                    return(true);
                }
                timeout = (int)(millisecondsTimeout - ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start));
            }
            return(false);
        }
Example #3
0
        private bool WaitExtracted(int millisecondsTimeout)
        {
            var spinWait  = new SpinWait();
            var spinCount = _spinCount;

            if (IsSet)
            {
                return(true);
            }
            var start = ThreadingHelper.TicksNow();

retry_longTimeout:
            if (IsSet)
            {
                return(true);
            }
            var elapsed = ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);

            if (elapsed < millisecondsTimeout)
            {
                if (spinCount > 0)
                {
                    spinCount--;
                    spinWait.SpinOnce();
                    goto retry_longTimeout;
                }
                var handle    = RetriveWaitHandle();
                var remaining = millisecondsTimeout - (int)elapsed;
                if (remaining > 0)
                {
                    return(handle.WaitOne(remaining));
                }
            }
            return(false);
        }
        public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
        {
            CheckDisposed();
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsTimeout");
            }
            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            if (millisecondsTimeout == -1)
            {
                ThreadingHelper.SpinWaitRelativeSet(ref _count, 1);
                return(true);
            }
            var start     = ThreadingHelper.TicksNow();
            var remaining = millisecondsTimeout;

            while (_event.Wait(remaining, cancellationToken))
            {
                // The thread is not allowed here unless there is room in the semaphore
                if (TryEnter())
                {
                    return(true);
                }
                remaining = (int)(millisecondsTimeout - ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start));
                if (remaining <= 0)
                {
                    break;
                }
            }
            // Time out
            return(false);
        }
Example #5
0
        public static bool TryEnter(object obj, int millisecondsTimeout, CancellationToken cancellationToken)
        {
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsTimeout");
            }
            if (millisecondsTimeout == -1)
            {
                Enter(millisecondsTimeout, cancellationToken);
                return(true);
            }
            var spinWait = new SpinWait();
            var start    = ThreadingHelper.TicksNow();

retry:
            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            if (TryEnter(obj))
            {
                return(true);
            }
            if (ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start) < millisecondsTimeout)
            {
                spinWait.SpinOnce();
                goto retry;
            }
            return(false);
        }
Example #6
0
        public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
        {
            var state = GetState();

            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout));
            }

            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            var spinWait = new SpinWait();

            if (TryOffset(-1, out _, state))
            {
                return(true);
            }

            if (millisecondsTimeout == -1)
            {
                while (true)
                {
                    state.CanEnter.Wait(-1, cancellationToken);
                    // The thread is not allowed here unless there is room in the semaphore
                    if (TryOffset(-1, out _, state))
                    {
                        return(true);
                    }

                    spinWait.SpinOnce();
                }
            }

            var start     = ThreadingHelper.TicksNow();
            var remaining = millisecondsTimeout;

            while (state.CanEnter.Wait(remaining, cancellationToken))
            {
                // The thread is not allowed here unless there is room in the semaphore
                if (TryOffset(-1, out _, state))
                {
                    return(true);
                }

                remaining = (int)(millisecondsTimeout - ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start));
                if (remaining <= 0)
                {
                    break;
                }

                spinWait.SpinOnce();
            }

            // Time out
            return(false);
        }
        private bool WaitExtracted(int millisecondsTimeout, CancellationToken cancellationToken)
        {
            var spinWait  = new SpinWait();
            var spinCount = SpinCount;

            if (IsSet)
            {
                return(true);
            }

            var start = ThreadingHelper.TicksNow();

retry_longTimeout:
            if (IsSet)
            {
                return(true);
            }

            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            var elapsed = ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);

            if (elapsed < millisecondsTimeout)
            {
                if (spinCount > 0)
                {
                    spinCount--;
                    spinWait.SpinOnce();
                    goto retry_longTimeout;
                }

                var handle    = GetOrCreateWaitHandle();
                var remaining = millisecondsTimeout - (int)elapsed;
                if (remaining > 0)
                {
                    var result = WaitHandle.WaitAny
                                 (
                        new[]
                    {
                        handle,
                        cancellationToken.WaitHandle
                    },
                        remaining
                                 );
                    cancellationToken.ThrowIfCancellationRequested();
                    GC.KeepAlive(cancellationToken.WaitHandle);
                    return(result != WaitHandle.WaitTimeout);
                }
            }

            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            return(false);
        }
Example #8
0
 private void WaitAntecedent(CancellationToken cancellationToken, int milliseconds, long start)
 {
     if (IsContinuationTask)
     {
         var antecedent = ((IContinuationTask)this).Antecedent;
         antecedent?.Wait
         (
             milliseconds - (int)ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start),
             cancellationToken
         );
     }
 }
        private bool WaitExtracted(int millisecondsTimeout)
        {
            var count = 0;

            if (IsSet)
            {
                return(true);
            }
            var start = ThreadingHelper.TicksNow();

            if (millisecondsTimeout > INT_LongTimeOutHint)
            {
retry_longTimeout:
                if (IsSet)
                {
                    return(true);
                }
                var elapsed = ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);
                if (elapsed < millisecondsTimeout)
                {
                    if (elapsed < INT_LongTimeOutHint)
                    {
                        ThreadingHelper.SpinOnce(ref count);
                        goto retry_longTimeout;
                    }
                    var handle    = RetriveWaitHandle();
                    var remaining = millisecondsTimeout - (int)elapsed;
                    if (remaining > 0)
                    {
                        return(handle.WaitOne(remaining));
                    }
                }
            }
            else
            {
retry_shortTimeout:
                if (IsSet)
                {
                    return(true);
                }
                var elapsed = ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);
                if (elapsed < millisecondsTimeout)
                {
                    ThreadingHelper.SpinOnce(ref count);
                    goto retry_shortTimeout;
                }
            }
            return(false);
        }
Example #10
0
 public override void Wait(int milliseconds, CancellationToken cancellationToken)
 {
     if (Interlocked.CompareExchange(ref _status, 1, 0) != 0)
     {
         base.Wait(milliseconds, cancellationToken);
     }
     else
     {
         var start = ThreadingHelper.TicksNow();
         base.Execute();
         milliseconds = milliseconds - (int)ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);
         if (milliseconds > 0)
         {
             base.Wait(milliseconds, cancellationToken);
         }
     }
 }
Example #11
0
 public override void Wait(int milliseconds)
 {
     if (Interlocked.CompareExchange(ref _status, 1, 0) != 0)
     {
         base.Wait(milliseconds);
     }
     else
     {
         var start = ThreadingHelper.TicksNow();
         base.Initialize();
         milliseconds -= (int)ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);
         if (milliseconds > 0)
         {
             base.Wait(milliseconds);
         }
     }
 }
Example #12
0
        public static bool TryEnter(object obj, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var milliseconds = (long)timeout.TotalMilliseconds;
            var spinWait     = new SpinWait();
            var start        = ThreadingHelper.TicksNow();

retry:
            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            if (TryEnter(obj))
            {
                return(true);
            }
            if (ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start) < milliseconds)
            {
                spinWait.SpinOnce();
                goto retry;
            }
            return(false);
        }
Example #13
0
 public override void Wait(int milliseconds)
 {
     if (milliseconds == -1)
     {
         Wait();
         return;
     }
     if (Interlocked.CompareExchange(ref _status, 1, 0) != 0)
     {
         base.Wait(milliseconds);
     }
     else
     {
         var start = ThreadingHelper.TicksNow();
         base.Execute();
         milliseconds = milliseconds - (int)ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);
         if (milliseconds > 0)
         {
             base.Wait(milliseconds);
         }
     }
 }
        private bool WaitExtracted(int millisecondsTimeout)
        {
            var spinWait  = new SpinWait();
            var spinCount = SpinCount;

            if (IsSet)
            {
                return(true);
            }

            var start = ThreadingHelper.TicksNow();

            while (true)
            {
                if (IsSet)
                {
                    return(true);
                }

                var elapsed = ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);
                if (elapsed >= millisecondsTimeout)
                {
                    return(false);
                }

                if (spinCount > 0)
                {
                    spinCount--;
                    spinWait.SpinOnce();
                    continue;
                }

                var handle    = GetOrCreateWaitHandle();
                var remaining = millisecondsTimeout - (int)elapsed;
                return(remaining > 0 && handle.WaitOne(remaining));
            }
        }
        public void Wait()
        {
            if (Thread.VolatileRead(ref _state) == -1)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            var count = 0;

            if (!IsSet)
            {
                var start = ThreadingHelper.TicksNow();
retry:
                if (!IsSet)
                {
                    if (ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start) < INT_LongTimeOutHint)
                    {
                        ThreadingHelper.SpinOnce(ref count);
                        goto retry;
                    }
                    var handle = RetriveWaitHandle();
                    handle.WaitOne();
                }
            }
        }
Example #16
0
        public static int TryTakeFromAny(BlockingCollection <T>[] collections, out T item, int millisecondsTimeout, CancellationToken cancellationToken)
        {
            if (collections == null)
            {
                throw new ArgumentNullException(nameof(collections));
            }

            if (collections.Length == 0)
            {
                throw new ArgumentException("The collections argument is a 0-length array", nameof(collections));
            }

            var waitHandles = new WaitHandle[collections.Length + 1];

            for (var index = 0; index < collections.Length; index++)
            {
                var collection = collections[index];
                if (collection == null)
                {
                    throw new ArgumentException("The collections argument contains a null element", nameof(collections));
                }

                waitHandles[index] = collection.Data.WaitHandle;
                if (collection.IsAddingCompleted)
                {
                    throw new ArgumentException("At least one of collections has been marked as complete for adding.", nameof(collections));
                }

                if (collection.TryTake(out item, 0, cancellationToken))
                {
                    return(index);
                }
            }

            waitHandles[collections.Length] = cancellationToken.WaitHandle;
            if (millisecondsTimeout == -1)
            {
                while (true)
                {
                    WaitHandle.WaitAny(waitHandles);
                    cancellationToken.ThrowIfCancellationRequested();
                    for (var index = 0; index < collections.Length; index++)
                    {
                        var collection = collections[index];
                        if (collection.IsAddingCompleted)
                        {
                            throw new ArgumentException("At least one of collections has been marked as complete for adding.", nameof(collections));
                        }

                        if (collection.TryTake(out item, 0, cancellationToken))
                        {
                            return(index);
                        }
                    }
                }
            }

            var start     = ThreadingHelper.TicksNow();
            var remaining = millisecondsTimeout;

            while (true)
            {
                WaitHandle.WaitAny(waitHandles, remaining);
                cancellationToken.ThrowIfCancellationRequested();
                for (var index = 0; index < collections.Length; index++)
                {
                    var collection = collections[index];
                    if (collection.IsAddingCompleted)
                    {
                        throw new ArgumentException("At least one of collections has been marked as complete for adding.", nameof(collections));
                    }

                    if (collection.TryTake(out item, 0, cancellationToken))
                    {
                        return(index);
                    }

                    remaining = (int)(millisecondsTimeout - ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start));
                    if (remaining <= 0)
                    {
                        return(-1);
                    }
                }
            }
        }
Example #17
0
        public bool Wait(int milliseconds, CancellationToken cancellationToken)
        {
            if (milliseconds < -1)
            {
                throw new ArgumentOutOfRangeException("milliseconds");
            }
            if (milliseconds == -1)
            {
                Wait(cancellationToken);
                return(true);
            }
            var start = ThreadingHelper.TicksNow();

            do
            {
                CancellationCheck(cancellationToken);
                switch (Status)
                {
                case TaskStatus.WaitingForActivation:
                    WaitAntecedent(cancellationToken, milliseconds, start);
                    ExecutingTaskScheduler.InernalTryExecuteTaskInline(this, true);
                    break;

                case TaskStatus.Created:
                case TaskStatus.WaitingToRun:
                case TaskStatus.Running:
                case TaskStatus.WaitingForChildrenToComplete:
                    var waitHandle = _waitHandle.Value;
                    if (_waitHandle.IsAlive)
                    {
                        waitHandle.Wait
                        (
                            TimeSpan.FromMilliseconds
                            (
                                milliseconds - ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start)
                            ),
                            cancellationToken
                        );
                    }
                    break;

                case TaskStatus.RanToCompletion:
                    return(true);

                case TaskStatus.Canceled:
                    ThrowIfExceptional(true);
                    return(true);

                case TaskStatus.Faulted:
                    ThrowIfExceptional(true);
                    return(true);
                }
            } while (ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start) < milliseconds);
            switch (Status)
            {
            case TaskStatus.RanToCompletion:
                return(true);

            case TaskStatus.Canceled:
                ThrowIfExceptional(true);
                return(true);

            case TaskStatus.Faulted:
                ThrowIfExceptional(true);
                return(true);

            default:
                return(false);
            }
        }
        private bool WaitExtracted(int millisecondsTimeout, CancellationToken cancellationToken)
        {
            var count = 0;

            if (IsSet)
            {
                return(true);
            }
            var start = ThreadingHelper.TicksNow();

            if (millisecondsTimeout > INT_LongTimeOutHint)
            {
retry_longTimeout:
                if (IsSet)
                {
                    return(true);
                }
                cancellationToken.ThrowIfCancellationRequested();
                GC.KeepAlive(cancellationToken.WaitHandle);
                var elapsed = ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);
                if (elapsed < millisecondsTimeout)
                {
                    if (elapsed < INT_LongTimeOutHint)
                    {
                        ThreadingHelper.SpinOnce(ref count);
                        goto retry_longTimeout;
                    }
                    var handle    = RetriveWaitHandle();
                    var remaining = millisecondsTimeout - (int)elapsed;
                    if (remaining > 0)
                    {
                        var result = WaitHandle.WaitAny
                                     (
                            new[]
                        {
                            handle,
                            cancellationToken.WaitHandle
                        },
                            remaining
                                     );
                        cancellationToken.ThrowIfCancellationRequested();
                        GC.KeepAlive(cancellationToken.WaitHandle);
                        return(result != WaitHandle.WaitTimeout);
                    }
                }
                cancellationToken.ThrowIfCancellationRequested();
                GC.KeepAlive(cancellationToken.WaitHandle);
            }
            else
            {
retry_shortTimeout:
                if (IsSet)
                {
                    return(true);
                }
                cancellationToken.ThrowIfCancellationRequested();
                GC.KeepAlive(cancellationToken.WaitHandle);
                var elapsed = ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start);
                if (elapsed < millisecondsTimeout)
                {
                    ThreadingHelper.SpinOnce(ref count);
                    goto retry_shortTimeout;
                }
                cancellationToken.ThrowIfCancellationRequested();
                GC.KeepAlive(cancellationToken.WaitHandle);
            }
            return(false);
        }