internal static Queue GetOrCreateQueue(int durationMilliseconds)
 {
     TimerQueue queue;
     if (durationMilliseconds == -1)
     {
         return new InfiniteTimerQueue();
     }
     if (durationMilliseconds < 0)
     {
         throw new ArgumentOutOfRangeException("durationMilliseconds");
     }
     WeakReference reference = (WeakReference) s_QueuesCache[durationMilliseconds];
     if ((reference == null) || ((queue = (TimerQueue) reference.Target) == null))
     {
         lock (s_NewQueues)
         {
             reference = (WeakReference) s_QueuesCache[durationMilliseconds];
             if ((reference != null) && ((queue = (TimerQueue) reference.Target) != null))
             {
                 return queue;
             }
             queue = new TimerQueue(durationMilliseconds);
             reference = new WeakReference(queue);
             s_NewQueues.AddLast(reference);
             s_QueuesCache[durationMilliseconds] = reference;
             if ((++s_CacheScanIteration % 0x20) != 0)
             {
                 return queue;
             }
             List<int> list = new List<int>();
             foreach (DictionaryEntry entry in s_QueuesCache)
             {
                 if (((WeakReference) entry.Value).Target == null)
                 {
                     list.Add((int) entry.Key);
                 }
             }
             for (int i = 0; i < list.Count; i++)
             {
                 s_QueuesCache.Remove(list[i]);
             }
         }
     }
     return queue;
 }
 internal static Queue CreateQueue(int durationMilliseconds)
 {
     TimerQueue queue;
     if (durationMilliseconds == -1)
     {
         return new InfiniteTimerQueue();
     }
     if (durationMilliseconds < 0)
     {
         throw new ArgumentOutOfRangeException("durationMilliseconds");
     }
     lock (s_NewQueues)
     {
         queue = new TimerQueue(durationMilliseconds);
         WeakReference reference = new WeakReference(queue);
         s_NewQueues.AddLast(reference);
     }
     return queue;
 }
Example #3
0
 public TimerGroup()
 {
     this.waitableTimer = new WaitableTimer();
     this.waitableTimer.Set(long.MaxValue);
     this.timerQueue = new TimerQueue();
 }
        /// <summary>
        /// This method is executed on a dedicated a timer thread. Its purpose is
        /// to handle timer requests and notify the TimerQueue when a timer expires.
        /// </summary>
        private static void TimerThread()
        {
            AutoResetEvent    timerEvent   = s_timerEvent;
            List <TimerQueue> timersToFire = s_scheduledTimersToFire !;
            List <TimerQueue> timers;

            lock (timerEvent)
            {
                timers = s_scheduledTimers !;
            }

            int shortestWaitDurationMs = Timeout.Infinite;

            while (true)
            {
                timerEvent.WaitOne(shortestWaitDurationMs);

                long currentTimeMs = TickCount64;
                shortestWaitDurationMs = int.MaxValue;
                lock (timerEvent)
                {
                    for (int i = timers.Count - 1; i >= 0; --i)
                    {
                        TimerQueue timer          = timers[i];
                        long       waitDurationMs = timer._scheduledDueTimeMs - currentTimeMs;
                        if (waitDurationMs <= 0)
                        {
                            timer._isScheduled = false;
                            timersToFire.Add(timer);

                            int lastIndex = timers.Count - 1;
                            if (i != lastIndex)
                            {
                                timers[i] = timers[lastIndex];
                            }
                            timers.RemoveAt(lastIndex);
                            continue;
                        }

                        if (waitDurationMs < shortestWaitDurationMs)
                        {
                            shortestWaitDurationMs = (int)waitDurationMs;
                        }
                    }
                }

                if (timersToFire.Count > 0)
                {
                    foreach (TimerQueue timerToFire in timersToFire)
                    {
                        ThreadPool.UnsafeQueueHighPriorityWorkItemInternal(timerToFire);
                    }
                    timersToFire.Clear();
                }

                if (shortestWaitDurationMs == int.MaxValue)
                {
                    shortestWaitDurationMs = Timeout.Infinite;
                }
            }
        }
Example #5
0
 protected override bool ReleaseHandle()
 {
     return(TimerQueue.DeleteAppDomainTimer(this.handle));
 }
Example #6
0
        // Token: 0x06003D1F RID: 15647 RVA: 0x000E3504 File Offset: 0x000E1704
        private void FireNextTimers()
        {
            TimerQueueTimer timerQueueTimer = null;

            lock (this)
            {
                try
                {
                }
                finally
                {
                    this.m_isAppDomainTimerScheduled = false;
                    bool            flag2            = false;
                    uint            num              = uint.MaxValue;
                    int             tickCount        = TimerQueue.TickCount;
                    TimerQueueTimer timerQueueTimer2 = this.m_timers;
                    while (timerQueueTimer2 != null)
                    {
                        uint num2 = (uint)(tickCount - timerQueueTimer2.m_startTicks);
                        if (num2 >= timerQueueTimer2.m_dueTime)
                        {
                            TimerQueueTimer next = timerQueueTimer2.m_next;
                            if (timerQueueTimer2.m_period != 4294967295U)
                            {
                                timerQueueTimer2.m_startTicks = tickCount;
                                timerQueueTimer2.m_dueTime    = timerQueueTimer2.m_period;
                                if (timerQueueTimer2.m_dueTime < num)
                                {
                                    flag2 = true;
                                    num   = timerQueueTimer2.m_dueTime;
                                }
                            }
                            else
                            {
                                this.DeleteTimer(timerQueueTimer2);
                            }
                            if (timerQueueTimer == null)
                            {
                                timerQueueTimer = timerQueueTimer2;
                            }
                            else
                            {
                                TimerQueue.QueueTimerCompletion(timerQueueTimer2);
                            }
                            timerQueueTimer2 = next;
                        }
                        else
                        {
                            uint num3 = timerQueueTimer2.m_dueTime - num2;
                            if (num3 < num)
                            {
                                flag2 = true;
                                num   = num3;
                            }
                            timerQueueTimer2 = timerQueueTimer2.m_next;
                        }
                    }
                    if (flag2)
                    {
                        this.EnsureAppDomainTimerFiresBy(num);
                    }
                }
            }
            if (timerQueueTimer != null)
            {
                timerQueueTimer.Fire();
            }
        }
        /// <summary>
        /// <para>Alternative cache-based queue factory.  Always synchronized.</para>
        /// </summary>
        internal static Queue GetOrCreateQueue(int durationMilliseconds) {
            if (durationMilliseconds == Timeout.Infinite) {
                return new InfiniteTimerQueue();
            }

            if (durationMilliseconds < 0) {
                throw new ArgumentOutOfRangeException("durationMilliseconds");
            }

            TimerQueue queue;
            WeakReference weakQueue = (WeakReference) s_QueuesCache[durationMilliseconds];
            if (weakQueue == null || (queue = (TimerQueue) weakQueue.Target) == null) {
                lock(s_NewQueues) {
                    weakQueue = (WeakReference) s_QueuesCache[durationMilliseconds];
                    if (weakQueue == null || (queue = (TimerQueue) weakQueue.Target) == null) {
                        queue = new TimerQueue(durationMilliseconds);
                        weakQueue = new WeakReference(queue);
                        s_NewQueues.AddLast(weakQueue);
                        s_QueuesCache[durationMilliseconds] = weakQueue;

                        // Take advantage of this lock to periodically scan the table for garbage.
                        if (++s_CacheScanIteration % c_CacheScanPerIterations == 0) {
                            List<int> garbage = new List<int>();
                            foreach (DictionaryEntry pair in s_QueuesCache) {
                                if (((WeakReference) pair.Value).Target == null) {
                                    garbage.Add((int) pair.Key);
                                }
                            }
                            for (int i = 0; i < garbage.Count; i++) {
                                s_QueuesCache.Remove(garbage[i]);
                            }
                        }
                    }
                }
            }

            return queue;
        }
        /// <summary>
        /// <para>The main external entry-point, allows creating new timer queues.</para>
        /// </summary>
        internal static Queue CreateQueue(int durationMilliseconds)
        {
            if (durationMilliseconds == Timeout.Infinite) {
                return new InfiniteTimerQueue();
            }

            if (durationMilliseconds < 0) {
                throw new ArgumentOutOfRangeException("durationMilliseconds");
            }

            // Queues are held with a weak reference so they can simply be abandoned and automatically cleaned up.
            TimerQueue queue;
            lock(s_NewQueues) {
                queue = new TimerQueue(durationMilliseconds);
                WeakReference weakQueue = new WeakReference(queue);
                s_NewQueues.AddLast(weakQueue);
            }

            return queue;
        }
Example #9
0
        private void FireNextTimers()
        {
            TimerQueueTimer timerQueueTimer = (TimerQueueTimer)null;

            lock (this)
            {
                try
                {
                }
                finally
                {
                    this.m_isAppDomainTimerScheduled = false;
                    bool            local_3 = false;
                    uint            local_4 = uint.MaxValue;
                    int             local_5 = TimerQueue.TickCount;
                    TimerQueueTimer local_6 = this.m_timers;
                    while (local_6 != null)
                    {
                        uint local_7 = (uint)(local_5 - local_6.m_startTicks);
                        if (local_7 >= local_6.m_dueTime)
                        {
                            TimerQueueTimer temp_33 = local_6.m_next;
                            if ((int)local_6.m_period != -1)
                            {
                                local_6.m_startTicks = local_5;
                                TimerQueueTimer temp_44 = local_6;
                                int             temp_45 = (int)temp_44.m_period;
                                temp_44.m_dueTime = (uint)temp_45;
                                if (local_6.m_dueTime < local_4)
                                {
                                    local_3 = true;
                                    local_4 = local_6.m_dueTime;
                                }
                            }
                            else
                            {
                                this.DeleteTimer(local_6);
                            }
                            if (timerQueueTimer == null)
                            {
                                timerQueueTimer = local_6;
                            }
                            else
                            {
                                TimerQueue.QueueTimerCompletion(local_6);
                            }
                            local_6 = temp_33;
                        }
                        else
                        {
                            uint local_8 = local_6.m_dueTime - local_7;
                            if (local_8 < local_4)
                            {
                                local_3 = true;
                                local_4 = local_8;
                            }
                            local_6 = local_6.m_next;
                        }
                    }
                    if (local_3)
                    {
                        this.EnsureAppDomainTimerFiresBy(local_4);
                    }
                }
            }
            if (timerQueueTimer == null)
            {
                return;
            }
            timerQueueTimer.Fire();
        }