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);
            }
        }
Example #2
0
        internal bool SetCompleted(bool preventDoubleExecution)
        {
            // For this method to be called the Task must have been scheduled,
            // this means that _status must be at least TaskStatus.WaitingForActivation (1),
            // if status is:
            // TaskStatus.WaitingForActivation (1) -> ok
            // WaitingToRun (2) -> ok
            // TaskStatus.Running (3) -> ok if preventDoubleExecution = false
            // TaskStatus.WaitingForChildrenToComplete (4) -> ok if preventDoubleExecution = false
            // TaskStatus.RanToCompletion (5) -> ok if preventDoubleExecution = false
            // TaskStatus.Canceled (6) -> not ok
            // TaskStatus.Faulted (7) -> -> ok if preventDoubleExecution = false
            var count = 0;

            while (true)
            {
                var lastValue = Thread.VolatileRead(ref _status);
                if ((preventDoubleExecution && lastValue >= 3) || lastValue == 6)
                {
                    return(false);
                }
                var tmp = Interlocked.CompareExchange(ref _status, 5, lastValue);
                if (tmp == lastValue)
                {
                    return(true);
                }
                ThreadingHelper.SpinOnce(ref count);
            }
        }
Example #3
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 count = 0;
            var start = ThreadingHelper.TicksNow();

retry:
            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            if (TryEnter(obj))
            {
                return(true);
            }
            if (ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start) < millisecondsTimeout)
            {
                ThreadingHelper.SpinOnce(ref count);
                goto retry;
            }
            return(false);
        }
Example #4
0
        public static void Enter(object obj, CancellationToken cancellationToken)
        {
            var count = 0;

retry:
            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            if (!Monitor.TryEnter(obj))
            {
                ThreadingHelper.SpinOnce(ref count);
                goto retry;
            }
        }
        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 #6
0
        public static bool TryEnter(object obj, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var milliseconds = (long)timeout.TotalMilliseconds;
            var count        = 0;
            var start        = ThreadingHelper.TicksNow();

retry:
            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            if (TryEnter(obj))
            {
                return(true);
            }
            if (ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start) < milliseconds)
            {
                ThreadingHelper.SpinOnce(ref count);
                goto retry;
            }
            return(false);
        }
        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();
                }
            }
        }
        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);
        }