Example #1
0
        private void timerCallback(object state)
        {
            if (_cacheCollection.Count <= 0)
            {
                return;
            }

            timerPoolItem.Stop();
            var n = DateTime.Now.AddMilliseconds(-1 * _timeOut);

            readerWriterLock.EnterReadLock();

            foreach (var itemTimeout in _cacheCollection)
            {
                TimeoutItem <int, TValue> v = itemTimeout.Value;

                if (v.LastActiveTime < n)
                {
                    Interlocked.Increment(ref v.TimeoutCount);
                    Events.EventHelper.RaiseAsync(ItemTimeout, this,
                                                  new IntIndexAutoTimeoutSetTimeoutEventArgs <int, TValue>(itemTimeout.Key,
                                                                                                           v.TimeoutCount,
                                                                                                           v.LastActiveTime,
                                                                                                           v.Value));
                    v.LastActiveTime = DateTime.Now;
                }
            }

            readerWriterLock.ExitReadLock();

            timerPoolItem.Start();
        }
        void TimerTick(object state)
        {
            this.AddPending();
            this.RemoveCancelled();
            //go through the timeouts in the current bucket and subtract the round
            //or expire
            Bucket bucket = this.wheel[this.Index];

            for (int index = bucket.Count - 1; index >= 0; index--)
            {
                TimeoutItem timeout = bucket[index];
                if (!timeout.IsAssigned)
                {
                    continue;
                }

                if (timeout.Rounds == 0)
                {
                    timeout.Expire();
                    bucket.RemoveAt(index, timeout);
                }
                else if (timeout.IsCancelled)
                {
                    bucket.RemoveAt(index, timeout);
                }
                else
                {
                    timeout.Rounds--;
                }
            }
            bucket.Compact();
            //while (timeout != null)
            //{
            //    if (timeout.Rounds == 0)
            //    {
            //        timeout.Expire();
            //        bucket.Remove(timeout);
            //    }
            //    else if (timeout.IsCancelled)
            //    {
            //        bucket.Remove(timeout);
            //    }
            //    else
            //    {
            //        timeout.Rounds--;
            //    }
            //    timeout = timeout.Next;
            //}
            //Setup the next tick
            this.Index = (this.Index + 1) % this.wheel.Length;
            try
            {
                this.timer.Change(this.tickDuration, Timeout.Infinite);
            }
            catch (ObjectDisposedException)
            {
                //the _timer might already have been disposed of
            }
        }
        public IAutoTimeoutCollectionItem <T> Add(T value, int timeout, WaitCallback callback, object state)
        {
            var t = new TimeoutItem <T>(value, timeout, callback, state);

            _link.AddLast(t);

            return(t);
        }
Example #4
0
 private void DoDispose()
 {
     //Break references
     Next         = null;
     Previous     = null;
     Bucket       = null;
     _actionState = null;
     _action      = null;
     _timer       = null;
 }
        /// <summary>
        ///     Adds a new action to be executed with a delay
        /// </summary>
        /// <param name="action">
        ///     Action to be executed. Consider that the action is going to be invoked in an IO thread.
        /// </param>
        /// <param name="delay">Delay in milliseconds</param>
        public TimeoutItem NewTimeout(T action, long delay)
        {
            if (delay < this.tickDuration)
            {
                delay = this.tickDuration;
            }
            var item = new TimeoutItem(this, action);

            this.pendingToAdd.Enqueue(ValueTuple.Create(item, delay));
            return(item);
        }
Example #6
0
        /// <summary>
        /// Adds a new action to be executed with a delay
        /// </summary>
        public ITimeout NewTimeout(Action <object> action, object state, long delay)
        {
            if (delay < _tickDuration)
            {
                delay = _tickDuration;
            }
            var item = new TimeoutItem(this, action, state);

            _pendingToAdd.Enqueue(Tuple.Create(item, delay));
            return(item);
        }
Example #7
0
        /// <summary>
        /// Adds a new action to be executed with a delay
        /// </summary>
        public ITimeout NewTimeout(Action action, long delay)
        {
            if (delay < _tickDuration)
            {
                delay = _tickDuration;
            }
            var item = new TimeoutItem(action);

            _pendingToAdd.Enqueue(Tuple.Create(item, delay));
            return(item);
        }
        public IAutoTimeoutDictionaryItem <TKey, TValue> Add(TKey key, TValue value, int timeout, WaitCallback callback, object state)
        {
            lockSlim.EnterWriteLock();

            var t = new TimeoutItem <TKey, TValue>(key, value, timeout, callback, state);

            _cacheValue.Add(key, t);

            lockSlim.ExitWriteLock();

            return(t);
        }
Example #9
0
        private void Add(ref TimeoutItem item)
        {
            lock (timeouts) {
                int index = timeouts.BinarySearch(item);
                index = index >= 0 ? index : ~index;
                timeouts.Insert(index, item);

                if (index == 0 && timeouts.Count > 1)
                {
                    wait.Set();
                }
            }
        }
Example #10
0
        public long Add(TimeSpan timeout, TimeoutHandler handler, object state)
        {
            CheckDisposed();
            if (timeout == TimeSpan.Zero)
                timeout = TimeSpan.FromMilliseconds(1);
            var item = new TimeoutItem
                {Id = Interlocked.Increment(ref _timeoutIds), Timeout = timeout, Handler = handler, State = state};
            item.Timer = new Timer(TimerCallback, item, timeout, timeout);

            Add(ref item);

            return item.Id;
        }
Example #11
0
        private void Add(ref TimeoutItem item)
        {
            lock (_timeouts)
            {
                var index = _timeouts.BinarySearch(item);
                index = index >= 0 ? index : ~index;
                _timeouts.Insert(index, item);

                if (index == 0)
                {
                    _wait.Set();
                }
            }
        }
Example #12
0
        private void TimerThread(object state)
        {
            bool        hasItem;
            TimeoutItem item = default(TimeoutItem);

            while (true)
            {
                if (disposed)
                {
#if NETSTANDARD1_5
                    wait.Dispose();
#else
                    wait.Close();
#endif
                    return;
                }

                lock (timeouts) {
                    hasItem = timeouts.Count > 0;
                    if (hasItem)
                    {
                        item = timeouts[0];
                    }
                }

                TimeSpan interval = hasItem ? item.Trigger - DateTime.UtcNow : TimeSpan.FromMilliseconds(-1);
                if (hasItem && interval < TimeSpan.Zero)
                {
                    interval = TimeSpan.Zero;
                }

#if NETSTANDARD1_5
                if (!wait.WaitOne(interval) && hasItem)
                {
#else
                if (!wait.WaitOne(interval, false) && hasItem)
                {
#endif
                    bool requeue = item.Handler(item.State, ref item.Timeout);
                    lock (timeouts) {
                        Remove(item.Id);
                        if (requeue)
                        {
                            item.Trigger += item.Timeout;
                            Add(ref item);
                        }
                    }
                }
            }
        }
Example #13
0
        public uint Add(TimeSpan timeout, TimeoutHandler handler, object state)
        {
            CheckDisposed();
            TimeoutItem item = new TimeoutItem();

            item.Id      = timeout_ids++;
            item.Timeout = timeout;
            item.Trigger = DateTime.UtcNow + timeout;
            item.Handler = handler;
            item.State   = state;

            Add(ref item);

            return(item.Id);
        }
        public bool TryGetValue(TKey key, out IAutoTimeoutDictionaryItem <TKey, TValue> value)
        {
            var isContrans = false;

            lockSlim.EnterReadLock();

            TimeoutItem <TKey, TValue> tempItem = null;

            isContrans = _cacheValue.TryGetValue(key, out tempItem);

            lockSlim.ExitReadLock();

            value = tempItem;

            return(isContrans);
        }
Example #15
0
        public uint Add(TimeSpan timeout, TimeoutHandler handler, object state)
        {
            CheckDisposed();
            var item = new TimeoutItem
            {
                Id      = _timeoutIds++,
                Timeout = timeout,
                Trigger = DateTime.UtcNow + timeout,
                Handler = handler,
                State   = state
            };

            Add(ref item);

            return(item.Id);
        }
Example #16
0
        public DateTime GetItemLastActiveTime(int key)
        {
            readerWriterLock.EnterReadLock();

            TimeoutItem <int, TValue> item = null;

            if (_cacheCollection.TryGetValue(key, out item))
            {
                readerWriterLock.ExitReadLock();
                return(item.LastActiveTime);
            }
            else
            {
                readerWriterLock.ExitReadLock();
                return(DateTime.MinValue);
            }
        }
Example #17
0
        public int GetItemTimeoutCount(int key)
        {
            readerWriterLock.EnterReadLock();

            TimeoutItem <int, TValue> item = null;

            if (_cacheCollection.TryGetValue(key, out item))
            {
                readerWriterLock.ExitReadLock();
                return(item.TimeoutCount);
            }
            else
            {
                readerWriterLock.ExitReadLock();
                return(int.MaxValue);
            }
        }
Example #18
0
        private void TimerThread()
        {
            TimeoutItem item = default(TimeoutItem);

            while (true)
            {
                if (_disposed)
                {
                    _wait.Dispose();
                    return;
                }

                bool hasItem;
                lock (_timeouts)
                {
                    hasItem = _timeouts.Count > 0;
                    if (hasItem)
                    {
                        item = _timeouts[0];
                    }
                }

                TimeSpan interval = hasItem ? item.Trigger - DateTime.UtcNow : TimeSpan.FromMilliseconds(-1);
                if (hasItem && (interval < TimeSpan.Zero))
                {
                    interval = TimeSpan.Zero;
                }

                if (!_wait.WaitOne(interval) && hasItem)
                {
                    bool requeue = item.Handler(item.State, item.Timeout);
                    lock (_timeouts)
                    {
                        Remove(item.Id);
                        if (requeue)
                        {
                            item.Trigger += item.Timeout;
                            Add(ref item);
                        }
                    }
                }
            }
        }
Example #19
0
        public uint Add(TimeSpan timeout, TimeoutHandler handler, object state)
        {
            lock (this) {
                CheckDisposed();
                TimeoutItem item = new TimeoutItem();
                item.Id      = timeout_ids++;
                item.Timeout = timeout;
                item.Trigger = DateTime.Now + timeout;
                item.Handler = handler;
                item.State   = state;

                Add(ref item);

                if (timeouts.Count == 1)
                {
                    Start();
                }

                return(item.Id);
            }
        }
Example #20
0
        private void AddTimeout(TimeoutItem item, long delay)
        {
            if (item.IsCancelled)
            {
                //It has been cancelled since then
                return;
            }
            //delay expressed in tickets
            var ticksDelay = delay / _tickDuration +
                             //As index is for the current tick and it was added since the last tick
                             Index - 1;
            var bucketIndex = Convert.ToInt32(ticksDelay % _wheel.Length);
            var rounds      = ticksDelay / _wheel.Length;

            if (rounds > 0 && bucketIndex < Index)
            {
                rounds--;
            }
            item.Rounds = rounds;
            _wheel[bucketIndex].Add(item);
        }
Example #21
0
        public bool ActiveItem(int key, bool isCleanTimeoutCount)
        {
            readerWriterLock.EnterWriteLock();

            TimeoutItem <int, TValue> item = null;
            bool isSuccess = false;

            if (_cacheCollection.TryGetValue(key, out item))
            {
                item.LastActiveTime = DateTime.Now;

                if (isCleanTimeoutCount)
                {
                    Interlocked.Exchange(ref item.TimeoutCount, 0);
                }

                isSuccess = true;
            }

            readerWriterLock.ExitWriteLock();

            return(isSuccess);
        }
Example #22
0
 private void Add(ref TimeoutItem item)
 {
     _timeouts.TryAdd(item.Id, item);
 }