Exemple #1
0
 //-------------------------------------------------------------------------
 // 将队列移到新的队列中
 // 相当于原来的list复制到新的list,原来的恢复初始值
 public void moveList(EbDoubleLinkList <TObject> pnew_list)
 {
     if (!pnew_list.empty())
     {
         pnew_list.init();
         pnew_list.mpHead.next = mpHead.next;
         pnew_list.mpHead.prev = mpHead.prev;
         init();
     }
 }
        //---------------------------------------------------------------------
        public TimerShaft()
        {
            MapEventNode = new Dictionary <EbTimeEvent, EbDoubleLinkNode <EbTimeEvent> >();
            ExcuteList   = new EbDoubleLinkList <EbTimeEvent>();

            mWheel1 = new EbTimeWheel((int)MAX_WHEEL_SIZE);
            mWheel2 = new EbTimeWheel((int)MIN_WHEEL_SIZE);
            mWheel3 = new EbTimeWheel((int)MIN_WHEEL_SIZE);
            mWheel4 = new EbTimeWheel((int)MIN_WHEEL_SIZE);
            mWheel5 = new EbTimeWheel((int)MIN_WHEEL_SIZE);
        }
        //-------------------------------------------------------------------------
        public EbTimeWheel(int spoke_count)
        {
            mLastSpokeIndex = -1;
            mSpokeCount     = spoke_count;
            mListTimerSpoke = new List <EbDoubleLinkList <EbTimeEvent> >(spoke_count);

            for (int i = 0; i < spoke_count; ++i)
            {
                EbDoubleLinkList <EbTimeEvent> list_event = new EbDoubleLinkList <EbTimeEvent>();
                mListTimerSpoke.Add(list_event);
            }
        }
        //---------------------------------------------------------------------
        // 添加事件
        void _addTimer(EbDoubleLinkNode <EbTimeEvent> timer)
        {
            ulong expires = timer.mObject.mExpires;
            ulong idx     = expires - mTimeJeffies;

            EbDoubleLinkList <EbTimeEvent> list_timer_spoke = null;

            if (idx < MAX_WHEEL_SIZE)
            {
                // 第1个轮子
                ulong i = expires & MAX_WHEEL_MASK;
                list_timer_spoke = mWheel1.mListTimerSpoke[(int)i];
            }
            else if (idx < (ulong)1 << Convert.ToInt32(MAX_WHEEL + MIN_WHEEL))
            {
                // 第2个轮子
                ulong i = expires >> Convert.ToInt32(MAX_WHEEL) & MIN_WHEEL_MASK;
                list_timer_spoke = mWheel2.mListTimerSpoke[(int)i];
            }
            else if (idx < (ulong)1 << Convert.ToInt32(MAX_WHEEL + 2 * MIN_WHEEL))
            {
                // 第3个轮子
                ulong i = expires >> Convert.ToInt32(MAX_WHEEL + MIN_WHEEL) & MIN_WHEEL_MASK;
                list_timer_spoke = mWheel3.mListTimerSpoke[(int)i];
            }
            else if (idx < (ulong)1 << Convert.ToInt32(MAX_WHEEL + 3 * MIN_WHEEL))
            {
                // 第4个轮子
                ulong i = expires >> Convert.ToInt32(MAX_WHEEL + 2 * MIN_WHEEL) & MIN_WHEEL_MASK;
                list_timer_spoke = mWheel4.mListTimerSpoke[(int)i];
            }
            else if ((long)idx < 0)
            {
                // Can happen if you add a timer with expires == jiffies,
                // or you set a timer to go off in the past
                list_timer_spoke = mWheel1.mListTimerSpoke[(int)(mTimeJeffies & MAX_WHEEL_MASK)];
            }
            else
            {
                // If the timeout is larger than 0xffffffff on 64-bit
                // architectures then we use the maximum timeout:
                if (idx > 0xffffffffUL)
                {
                    idx     = 0xffffffffUL;
                    expires = idx + mTimeJeffies;
                }
                ulong i = expires >> Convert.ToInt32(MAX_WHEEL + 3 * MIN_WHEEL) & MIN_WHEEL_MASK;
                list_timer_spoke = mWheel5.mListTimerSpoke[(int)i];
            }

            // Timers are FIFO:
            list_timer_spoke.AddTailNode(timer);
        }
Exemple #5
0
        //-------------------------------------------------------------------------
        // 添加队列到本队列(队尾)
        public void addTailList(EbDoubleLinkList <TObject> plist)
        {
            if (!plist.empty())
            {
                EbDoubleLinkNode <TObject> add_list_first = plist.firstNode();
                EbDoubleLinkNode <TObject> add_list_last  = plist.lastNode();
                plist.init();

                EbDoubleLinkNode <TObject> list_last = lastNode();
                list_last.next      = add_list_first;
                add_list_first.prev = list_last;
                add_list_last.next  = mpHead;
                mpHead.prev         = add_list_last;
            }
        }
        //-------------------------------------------------------------------------
        // 根据时间,执行时间事件
        public void processTimer(ulong jeffies)
        {
            EbDoubleLinkList <EbTimeEvent> excute_time = new EbDoubleLinkList <EbTimeEvent>();
            {
                while (jeffies >= mTimeJeffies)
                {
                    ulong index = mTimeJeffies & MAX_WHEEL_MASK;

                    if (index == 0 && _cascadeTimer(mWheel2, 0) == 0 && _cascadeTimer(mWheel3, 1) == 0 && _cascadeTimer(mWheel4, 2) == 0)
                    {
                        _cascadeTimer(mWheel5, 3);
                    }
                    mTimeJeffies++;

                    excute_time.addTailList(mWheel1.getSpoke((int)index));
                }
            }
            EbDoubleLinkNode <EbTimeEvent> head, curr, next;

            head = excute_time.head();
            curr = head.next;

            while (curr != head)
            {
                next = curr.next;
                _delTimer(curr);

                // 调用委托
                if (curr.mObject.onTime != null)
                {
                    curr.mObject.onTime(curr.mObject.mData);
                }

                curr = next;
            }
        }
        //-------------------------------------------------------------------------
        // 删除事件
        int _delTimer(EbDoubleLinkNode <EbTimeEvent> timer)
        {
            EbDoubleLinkList <EbTimeEvent> .delNode(timer);

            return(0);
        }