示例#1
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && (state & StateDisposed) == 0)
            {
                if (CustomInterlocked.CompareExchange(ref state, StateDisposed, StateValid) == StateValid)
                {
                    UnregisterLinkedTokens();
                    callbacks = null;
                }
                else
                {
                    if (handle != null)
                    {
                        handle.WaitOne();
                    }

                    state |= StateDisposed;
                    Thread.MemoryBarrier();
                }
                if (timer != null)
                {
                    Timer.Dispose();
                }

                ((IDisposable)handle).Dispose();
                handle = null;
            }
        }
示例#2
0
        public void CancelAfter(int millisecondsDelay)
        {
            if (millisecondsDelay < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsDelay");
            }

            CheckDisposed();

            if (IsCancellationRequested || millisecondsDelay == Timeout.Infinite)
            {
                return;
            }

            if (timer == null)
            {
                // Have to be carefull not to create secondary background timer
                var t = new Timer(timer_callback, this, Timeout.Infinite, Timeout.Infinite);
                if (CustomInterlocked.CompareExchange(ref timer, t, null) != null)
                {
                    t.Dispose();
                }
            }

            Timer.Change(millisecondsDelay, Timeout.Infinite);
        }
        long UpdateStateWithOp(bool set)
        {
            int oldValue, newValue;

            do
            {
                oldValue = state;
                newValue = (int)(((oldValue >> 1) + 1) << 1) | (set ? 1 : 0);
            } while (CustomInterlocked.CompareExchange(ref state, newValue, oldValue) != oldValue);
            return(newValue);
        }
        bool ApplyOperation(int num, out int newValue)
        {
            int oldCount;

            do
            {
                oldCount = initialCount;
                if (oldCount == 0)
                {
                    newValue = 0;
                    return(false);
                }

                newValue = oldCount + num;

                if (newValue < 0)
                {
                    return(false);
                }
            } while (CustomInterlocked.CompareExchange(ref initialCount, newValue, oldCount) != oldCount);

            return(true);
        }
示例#5
0
        void Cancellation(bool throwOnFirstException)
        {
            if (CustomInterlocked.CompareExchange(ref state, StateCanceled, StateValid) != StateValid)
            {
                return;
            }

            handle.Set();

            if (linkedTokens != null)
            {
                UnregisterLinkedTokens();
            }

            var cbs = callbacks;

            if (cbs == null)
            {
                return;
            }

            List <Exception> exceptions = null;

            try {
                Action cb;
                for (int id = currId; id != int.MinValue; id--)
                {
                    if (!cbs.TryRemove(new CancellationTokenRegistration(id, this), out cb))
                    {
                        continue;
                    }
                    if (cb == null)
                    {
                        continue;
                    }

                    if (throwOnFirstException)
                    {
                        cb();
                    }
                    else
                    {
                        try {
                            cb();
                        } catch (Exception e) {
                            if (exceptions == null)
                            {
                                exceptions = new List <Exception> ();
                            }

                            exceptions.Add(e);
                        }
                    }
                }
            } finally {
                cbs.Clear();
            }

            if (exceptions != null)
            {
                throw new AggregateException(exceptions);
            }
        }