Esempio n. 1
0
            public void DeleteTimer(IOThreadTimer timer)
            {
                int index = timer.index;

                Fx.Assert(index > 0, "");
                Fx.Assert(index <= this.count, "");

                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();
            }
            public void DeleteTimer(IOThreadTimer timer)
            {
                int index = timer._index;

                Fx.Assert(index > 0, "");
                Fx.Assert(index <= Count, "");

                IOThreadTimer[] tempTimers = _timers;

                while (true)
                {
                    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();
            }
Esempio n. 3
0
            public bool UpdateTimer(IOThreadTimer timer, long newDueTime)
            {
                int index = timer.index;

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

                Fx.Assert(index > 0, "");
                Fx.Assert(index <= tempCount, "");

                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);
            }
 public IotHubConnection(IotHubConnectionString connectionString, AccessRights accessRights)
 {
     this.connectionString = connectionString;
     this.accessRights = accessRights;
     this.faultTolerantSession = new FaultTolerantAmqpObject<AmqpSession>(this.CreateSessionAsync, this.CloseConnection);
     this.refreshTokenTimer = new IOThreadTimer(s => ((IotHubConnection)s).OnRefreshToken(), this, false);
 }
            public void DeleteMinTimer()
            {
                IOThreadTimer minTimer = MinTimer;

                DeleteMinTimerCore();
                minTimer._index   = 0;
                minTimer._dueTime = 0;
            }
Esempio n. 6
0
            public void DeleteMinTimer()
            {
                IOThreadTimer minTimer = this.MinTimer;

                DeleteMinTimerCore();
                minTimer.index   = 0;
                minTimer.dueTime = 0;
            }
Esempio n. 7
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);
                }
            }
            public bool InsertTimer(IOThreadTimer timer, long dueTime)
            {
                Fx.Assert(timer._index == 0, "Timer should not have an index.");

                IOThreadTimer[] tempTimers = _timers;

                int index = Count + 1;

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

                Count = index;

                if (index > 1)
                {
                    while (true)
                    {
                        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);
            }
Esempio n. 9
0
            public bool InsertTimer(IOThreadTimer timer, long dueTime)
            {
                Fx.Assert(timer.index == 0, "Timer should not have an index.");

                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);
            }
Esempio n. 10
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();
                        ActionItem.Schedule(timer.callback, timer.callbackState);
                    }
                    else
                    {
                        break;
                    }
                }
            }
Esempio n. 11
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);
                    }
                }
            }
Esempio n. 12
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();
                                }
                            }
                        }
                    }
                }
            }
            private void DeleteMinTimerCore()
            {
                int currentCount = Count;

                if (currentCount == 1)
                {
                    Count      = 0;
                    _timers[1] = null;
                }
                else
                {
                    IOThreadTimer[] tempTimers = _timers;
                    IOThreadTimer   lastTimer  = tempTimers[currentCount];
                    Count = --currentCount;

                    int index = 1;
                    while (true)
                    {
                        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;
                }
            }
Esempio n. 14
0
            public void DeleteTimer(IOThreadTimer timer)
            {
                int index = timer.index;

                Fx.Assert(index > 0, "");
                Fx.Assert(index <= this.count, "");

                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();
            }
Esempio n. 15
0
            public bool InsertTimer(IOThreadTimer timer, long dueTime)
            {
                Fx.Assert(timer.index == 0, "Timer should not have an index.");

                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;
            }
Esempio n. 16
0
            public bool UpdateTimer(IOThreadTimer timer, long newDueTime)
            {
                int index = timer.index;

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

                Fx.Assert(index > 0, "");
                Fx.Assert(index <= tempCount, "");

                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;
            }
Esempio n. 17
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;
                    }
                }
            }
Esempio n. 18
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;
                }
            }
Esempio n. 19
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();
                                }
                            }
                        }
                    }
                }
            }