コード例 #1
0
            public bool UpdateTimer(IOThreadTimer timer, long newDueTime)
            {
                int index = timer.index;

                IOThreadTimer[] tempTimers = this.timers;
                int             tempCount  = this.count;

                int parentIndex = index / 2;

                if (parentIndex == 0 ||
                    tempTimers[parentIndex].dueTime <= newDueTime)
                {
                    int leftChildIndex = index * 2;
                    if (leftChildIndex > tempCount ||
                        tempTimers[leftChildIndex].dueTime >= newDueTime)
                    {
                        int rightChildIndex = leftChildIndex + 1;
                        if (rightChildIndex > tempCount ||
                            tempTimers[rightChildIndex].dueTime >= newDueTime)
                        {
                            timer.dueTime = newDueTime;
                            return(index == 1);
                        }
                    }
                }

                DeleteTimer(timer);
                InsertTimer(timer, newDueTime);
                return(true);
            }
コード例 #2
0
            public void DeleteTimer(IOThreadTimer timer)
            {
                int index = timer.index;

                IOThreadTimer[] tempTimers = this.timers;

                for (; ;)
                {
                    int parentIndex = index / 2;

                    if (parentIndex >= 1)
                    {
                        IOThreadTimer parentTimer = tempTimers[parentIndex];
                        tempTimers[index] = parentTimer;
                        parentTimer.index = index;
                    }
                    else
                    {
                        break;
                    }

                    index = parentIndex;
                }

                timer.index   = 0;
                timer.dueTime = 0;
                tempTimers[1] = null;
                DeleteMinTimerCore();
            }
コード例 #3
0
            public void DeleteMinTimer()
            {
                IOThreadTimer minTimer = this.MinTimer;

                DeleteMinTimerCore();
                minTimer.index   = 0;
                minTimer.dueTime = 0;
            }
コード例 #4
0
 public AsyncQueueWaiter(TimeSpan timeout, AsyncCallback callback, object state)
     : base(callback, state)
 {
     if (timeout != TimeSpan.MaxValue)
     {
         this.timer = new IOThreadTimer(timerCallback, this, false);
         this.timer.Set(timeout);
     }
 }
コード例 #5
0
 public AsyncQueueReader(InputQueue <T> inputQueue, TimeSpan timeout, AsyncCallback callback, object state)
     : base(callback, state)
 {
     if (inputQueue.AsyncCallbackGenerator != null)
     {
         this.VirtualCallback = new Action <AsyncCallback, IAsyncResult>(inputQueue.AsyncCallbackGenerator());
     }
     this.inputQueue = inputQueue;
     if (timeout != TimeSpan.MaxValue)
     {
         this.timer = new IOThreadTimer(timerCallback, this, false);
         this.timer.Set(timeout);
     }
 }
コード例 #6
0
            static void UpdateWaitableTimer(TimerGroup timerGroup)
            {
                WaitableTimer waitableTimer = timerGroup.WaitableTimer;
                IOThreadTimer minTimer      = timerGroup.TimerQueue.MinTimer;
                long          timeDiff      = waitableTimer.DueTime - minTimer.dueTime;

                if (timeDiff < 0)
                {
                    timeDiff = -timeDiff;
                }
                if (timeDiff > minTimer.maxSkew)
                {
                    waitableTimer.Set(minTimer.dueTime);
                }
            }
コード例 #7
0
            public bool InsertTimer(IOThreadTimer timer, long dueTime)
            {
                IOThreadTimer[] tempTimers = this.timers;

                int index = this.count + 1;

                if (index == tempTimers.Length)
                {
                    tempTimers = new IOThreadTimer[tempTimers.Length * 2];
                    Array.Copy(this.timers, tempTimers, this.timers.Length);
                    this.timers = tempTimers;
                }

                this.count = index;

                if (index > 1)
                {
                    for (; ;)
                    {
                        int parentIndex = index / 2;

                        if (parentIndex == 0)
                        {
                            break;
                        }

                        IOThreadTimer parent = tempTimers[parentIndex];

                        if (parent.dueTime > dueTime)
                        {
                            tempTimers[index] = parent;
                            parent.index      = index;
                            index             = parentIndex;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                tempTimers[index] = timer;
                timer.index       = index;
                timer.dueTime     = dueTime;
                return(index == 1);
            }
コード例 #8
0
            static void ScheduleElapsedTimers(TimerGroup timerGroup, long now)
            {
                TimerQueue timerQueue = timerGroup.TimerQueue;

                while (timerQueue.Count > 0)
                {
                    IOThreadTimer timer    = timerQueue.MinTimer;
                    long          timeDiff = timer.dueTime - now;
                    if (timeDiff <= timer.maxSkew)
                    {
                        timerQueue.DeleteMinTimer();
                        IOThreadScheduler.ScheduleCallbackNoFlow(timer.callback, timer.callbackState);
                    }
                    else
                    {
                        break;
                    }
                }
            }
コード例 #9
0
            public bool Cancel(IOThreadTimer timer)
            {
                lock (ThisLock)
                {
                    if (timer.index > 0)
                    {
                        TimerGroup timerGroup = timer.timerGroup;
                        TimerQueue timerQueue = timerGroup.TimerQueue;

                        timerQueue.DeleteTimer(timer);

                        if (timerQueue.Count > 0)
                        {
                            UpdateWaitableTimer(timerGroup);
                        }
                        else
                        {
                            TimerGroup otherTimerGroup = GetOtherTimerGroup(timerGroup);
                            if (otherTimerGroup.TimerQueue.Count == 0)
                            {
                                long now = Ticks.Now;
                                long thisGroupRemainingTime  = timerGroup.WaitableTimer.DueTime - now;
                                long otherGroupRemainingTime = otherTimerGroup.WaitableTimer.DueTime - now;
                                if (thisGroupRemainingTime > maxTimeToWaitForMoreTimers &&
                                    otherGroupRemainingTime > maxTimeToWaitForMoreTimers)
                                {
                                    timerGroup.WaitableTimer.Set(Ticks.Add(now, maxTimeToWaitForMoreTimers));
                                }
                            }
                        }

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
コード例 #10
0
            public void Set(IOThreadTimer timer, long dueTime)
            {
                long timeDiff = dueTime - timer.dueTime;

                if (timeDiff < 0)
                {
                    timeDiff = -timeDiff;
                }

                if (timeDiff > timer.maxSkew)
                {
                    lock (ThisLock)
                    {
                        TimerGroup timerGroup = timer.timerGroup;
                        TimerQueue timerQueue = timerGroup.TimerQueue;

                        if (timer.index > 0)
                        {
                            if (timerQueue.UpdateTimer(timer, dueTime))
                            {
                                UpdateWaitableTimer(timerGroup);
                            }
                        }
                        else
                        {
                            if (timerQueue.InsertTimer(timer, dueTime))
                            {
                                UpdateWaitableTimer(timerGroup);

                                if (timerQueue.Count == 1)
                                {
                                    EnsureWaitScheduled();
                                }
                            }
                        }
                    }
                }
            }
コード例 #11
0
 public SleepAsyncResult(TimeSpan amount, AsyncCallback callback, object state)
     : base(callback, state)
 {
     this.timer = new IOThreadTimer(onTimer, this, false);
     this.timer.Set(amount);
 }
コード例 #12
0
            void DeleteMinTimerCore()
            {
                int currentCount = this.count;

                if (currentCount == 1)
                {
                    this.count     = 0;
                    this.timers[1] = null;
                }
                else
                {
                    IOThreadTimer[] tempTimers = this.timers;
                    IOThreadTimer   lastTimer  = tempTimers[currentCount];
                    this.count = --currentCount;

                    int index = 1;
                    for (; ;)
                    {
                        int leftChildIndex = index * 2;

                        if (leftChildIndex > currentCount)
                        {
                            break;
                        }

                        int           childIndex;
                        IOThreadTimer child;

                        if (leftChildIndex < currentCount)
                        {
                            IOThreadTimer leftChild       = tempTimers[leftChildIndex];
                            int           rightChildIndex = leftChildIndex + 1;
                            IOThreadTimer rightChild      = tempTimers[rightChildIndex];

                            if (rightChild.dueTime < leftChild.dueTime)
                            {
                                child      = rightChild;
                                childIndex = rightChildIndex;
                            }
                            else
                            {
                                child      = leftChild;
                                childIndex = leftChildIndex;
                            }
                        }
                        else
                        {
                            childIndex = leftChildIndex;
                            child      = tempTimers[childIndex];
                        }

                        if (lastTimer.dueTime > child.dueTime)
                        {
                            tempTimers[index] = child;
                            child.index       = index;
                        }
                        else
                        {
                            break;
                        }

                        index = childIndex;

                        if (leftChildIndex >= currentCount)
                        {
                            break;
                        }
                    }

                    tempTimers[index]            = lastTimer;
                    lastTimer.index              = index;
                    tempTimers[currentCount + 1] = null;
                }
            }
コード例 #13
0
ファイル: IOThreadTimer.cs プロジェクト: TerenceLewis/SignalR
            public bool UpdateTimer(IOThreadTimer timer, long newDueTime)
            {
                int index = timer.index;

                IOThreadTimer[] tempTimers = this.timers;
                int tempCount = this.count;

                int parentIndex = index / 2;
                if (parentIndex == 0 ||
                    tempTimers[parentIndex].dueTime <= newDueTime)
                {
                    int leftChildIndex = index * 2;
                    if (leftChildIndex > tempCount ||
                        tempTimers[leftChildIndex].dueTime >= newDueTime)
                    {
                        int rightChildIndex = leftChildIndex + 1;
                        if (rightChildIndex > tempCount ||
                            tempTimers[rightChildIndex].dueTime >= newDueTime)
                        {
                            timer.dueTime = newDueTime;
                            return index == 1;
                        }
                    }
                }

                DeleteTimer(timer);
                InsertTimer(timer, newDueTime);
                return true;
            }
コード例 #14
0
ファイル: IOThreadTimer.cs プロジェクト: TerenceLewis/SignalR
            public bool InsertTimer(IOThreadTimer timer, long dueTime)
            {
                IOThreadTimer[] tempTimers = this.timers;

                int index = this.count + 1;

                if (index == tempTimers.Length)
                {
                    tempTimers = new IOThreadTimer[tempTimers.Length * 2];
                    Array.Copy(this.timers, tempTimers, this.timers.Length);
                    this.timers = tempTimers;
                }

                this.count = index;

                if (index > 1)
                {
                    for (; ; )
                    {
                        int parentIndex = index / 2;

                        if (parentIndex == 0)
                        {
                            break;
                        }

                        IOThreadTimer parent = tempTimers[parentIndex];

                        if (parent.dueTime > dueTime)
                        {
                            tempTimers[index] = parent;
                            parent.index = index;
                            index = parentIndex;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                tempTimers[index] = timer;
                timer.index = index;
                timer.dueTime = dueTime;
                return index == 1;
            }
コード例 #15
0
ファイル: IOThreadTimer.cs プロジェクト: TerenceLewis/SignalR
            public void DeleteTimer(IOThreadTimer timer)
            {
                int index = timer.index;

                IOThreadTimer[] tempTimers = this.timers;

                for (; ; )
                {
                    int parentIndex = index / 2;

                    if (parentIndex >= 1)
                    {
                        IOThreadTimer parentTimer = tempTimers[parentIndex];
                        tempTimers[index] = parentTimer;
                        parentTimer.index = index;
                    }
                    else
                    {
                        break;
                    }

                    index = parentIndex;
                }

                timer.index = 0;
                timer.dueTime = 0;
                tempTimers[1] = null;
                DeleteMinTimerCore();
            }
コード例 #16
0
ファイル: IOThreadTimer.cs プロジェクト: TerenceLewis/SignalR
            public void Set(IOThreadTimer timer, long dueTime)
            {
                long timeDiff = dueTime - timer.dueTime;
                if (timeDiff < 0)
                {
                    timeDiff = -timeDiff;
                }

                if (timeDiff > timer.maxSkew)
                {
                    lock (ThisLock)
                    {
                        TimerGroup timerGroup = timer.timerGroup;
                        TimerQueue timerQueue = timerGroup.TimerQueue;

                        if (timer.index > 0)
                        {
                            if (timerQueue.UpdateTimer(timer, dueTime))
                            {
                                UpdateWaitableTimer(timerGroup);
                            }
                        }
                        else
                        {
                            if (timerQueue.InsertTimer(timer, dueTime))
                            {
                                UpdateWaitableTimer(timerGroup);

                                if (timerQueue.Count == 1)
                                {
                                    EnsureWaitScheduled();
                                }
                            }
                        }
                    }
                }
            }
コード例 #17
0
ファイル: IOThreadTimer.cs プロジェクト: TerenceLewis/SignalR
            public bool Cancel(IOThreadTimer timer)
            {
                lock (ThisLock)
                {
                    if (timer.index > 0)
                    {
                        TimerGroup timerGroup = timer.timerGroup;
                        TimerQueue timerQueue = timerGroup.TimerQueue;

                        timerQueue.DeleteTimer(timer);

                        if (timerQueue.Count > 0)
                        {
                            UpdateWaitableTimer(timerGroup);
                        }
                        else
                        {
                            TimerGroup otherTimerGroup = GetOtherTimerGroup(timerGroup);
                            if (otherTimerGroup.TimerQueue.Count == 0)
                            {
                                long now = Ticks.Now;
                                long thisGroupRemainingTime = timerGroup.WaitableTimer.DueTime - now;
                                long otherGroupRemainingTime = otherTimerGroup.WaitableTimer.DueTime - now;
                                if (thisGroupRemainingTime > maxTimeToWaitForMoreTimers &&
                                    otherGroupRemainingTime > maxTimeToWaitForMoreTimers)
                                {
                                    timerGroup.WaitableTimer.Set(Ticks.Add(now, maxTimeToWaitForMoreTimers));
                                }
                            }
                        }

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }