【深入瞭解cocos2d-x 3.x】定時器(scheduler)的使用和原理探究(2)

上篇說到定時器的使用方法,這篇主要分析它的實現原理。

1.哈希鏈表

cocos2dx封裝了一個結構體,叫做UT_hash_handle,只要在自定義的結構體中聲明這個結構體變量,就實現了哈希鏈表,並且能使用一系列的哈希鏈表專用的宏。這個結構體的具體實現如下:
typedef struct UT_hash_handle {
   struct UT_hash_table *tbl;
   void *prev;                       /* prev element in app order      */
   void *next;                       /* next element in app order      */
   struct UT_hash_handle *hh_prev;   /* previous hh in bucket order    */
   struct UT_hash_handle *hh_next;   /* next hh in bucket order        */
   void *key;                        /* ptr to enclosing struct's key  */
   unsigned keylen;                  /* enclosing struct's key len     */
   unsigned hashv;                   /* result of hash-fcn(key)        */
} UT_hash_handle;

這個結構體主要實現的是一個雙向鏈表,具體實現哈希驗證的還要看UT_hash_table 結構體
typedef struct UT_hash_table {
   UT_hash_bucket *buckets;
   unsigned num_buckets, log2_num_buckets;
   unsigned num_items;
   struct UT_hash_handle *tail; /* tail hh in app order, for fast append    */
   ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */

   /* in an ideal situation (all buckets used equally), no bucket would have
    * more than ceil(#items/#buckets) items. that's the ideal chain length. */
   unsigned ideal_chain_maxlen;

   /* nonideal_items is the number of items in the hash whose chain position
    * exceeds the ideal chain maxlen. these items pay the penalty for an uneven
    * hash distribution; reaching them in a chain traversal takes >ideal steps */
   unsigned nonideal_items;

   /* ineffective expands occur when a bucket doubling was performed, but 
    * afterward, more than half the items in the hash had nonideal chain
    * positions. If this happens on two consecutive expansions we inhibit any
    * further expansion, as it's not helping; this happens when the hash
    * function isn't a good fit for the key domain. When expansion is inhibited
    * the hash will still work, albeit no longer in constant time. */
   unsigned ineff_expands, noexpand;

   uint32_t signature; /* used only to find hash tables in external analysis */
#ifdef HASH_BLOOM
   uint32_t bloom_sig; /* used only to test bloom exists in external analysis */
   uint8_t *bloom_bv;
   char bloom_nbits;
#endif

} UT_hash_table;

然後看看與哈希鏈表相關的宏定義,使用這些宏能很方便的插入鏈表,刪除鏈表,查找鏈表。
/**
 * 查找元素
 * head:哈希鏈表的頭指針
 * findptr:要查找的元素指針
 * out:查找結果
 */
HASH_FIND_PTR(head,findptr,out) 
/**
 * 添加元素
 * head:哈希鏈表的頭指針
 * ptrfield:要添加的元素指針
 * add:要添加的哈希鏈表元素
 */
HASH_ADD_PTR(head,ptrfield,add) 
/**
 * 替換元素
 * head:哈希鏈表的頭指針
 * ptrfield:要替換的元素指針
 * add:要替換的哈希鏈表元素
 */
HASH_REPLACE_PTR(head,ptrfield,add)
/**
 * 刪除
 * head:哈希鏈表的頭指針
 * delptr:要刪除的元素指針
 */
HASH_DEL(head,delptr) 

以上是引擎中實現的哈希鏈表的相關知識,接下來再看看與定時器相關的哈希鏈表。定時器的實現中,將一個定時器存儲在哈希鏈表中,那麼在scheduler是如何實現以後哈希鏈表的結構體的呢?如下:
// 不同優先級的update定時器的雙向鏈表
typedef struct _listEntry
{
    struct _listEntry   *prev, *next;
    ccSchedulerFunc     callback;
    void                *target;
    int                 priority;
    bool                paused;
    bool                markedForDeletion; // selector will no longer be called and entry will be removed at end of the next tick
} tListEntry;
//內置的update定時器
typedef struct _hashUpdateEntry
{
    tListEntry          **list;        // Which list does it belong to ?
    tListEntry          *entry;        // entry in the list
    void                *target;
    ccSchedulerFunc     callback;
    UT_hash_handle      hh;
} tHashUpdateEntry;

// 自定義定時器
typedef struct _hashSelectorEntry
{
    ccArray             *timers;
    void                *target;
    int                 timerIndex;
    Timer               *currentTimer;
    bool                currentTimerSalvaged;
    bool                paused;
    UT_hash_handle      hh;
} tHashTimerEntry;

以上就是相關的哈希鏈表的知識,接下來從定義定時器的函數Node::schedule中一步一步的分析定時器是如何加入到哈希鏈表中的。

2.如何定義自定義定時器

首先,上一篇文章中說到了很多個自定義定時器的函數,但是最終會調用的函數只有兩個,分別是
    /**
     * 定義一個自定義的定時器
	 * selector:回調函數
	 * interval:重複間隔時間,重複執行間隔的時間,如果傳入0,則表示每幀調用
	 * repeat:重複運行次數,如果傳入CC_REPEAT_FOREVER則表示無限循環
	 * delay:延時秒數,延遲delay秒開始執行第一次回調
     */
    void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
	
    /**
     * 使用lambda函數定義一個自定義定時器
     * callback:lambda函數
	 * interval:重複間隔時間,重複執行間隔的時間,如果傳入0,則表示每幀調用
	 * repeat:重複運行次數,如果傳入CC_REPEAT_FOREVER則表示無限循環
	 * delay:延時秒數,延遲delay秒開始執行第一次回調
     * key:lambda函數的Key,用於取消定時器
     * @lua NA
     */
    void schedule(const std::function<void(float)>& callback, float interval, unsigned int repeat, float delay, const std::string &key);

本文從傳統的定義定時器的方法入手,也就是第一個方法。接下來看看這個方法的實現:
void Node::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
{
    CCASSERT( selector, "Argument must be non-nil");
    CCASSERT( interval >=0, "Argument must be positive");

    _scheduler->schedule(selector, this, interval , repeat, delay, !_running);
}

看到其實還是調用_scheduler的schedule方法,那麼_scheduler又是個什麼鬼?
Scheduler *_scheduler;          ///< scheduler used to schedule timers and updates
查看定義可以知道是一個Scheduler 的指針,但是這個指針從哪裏來?在構造函數中有真相
Node::Node(void)
{
    // set default scheduler and actionManager
    _director = Director::getInstance();
    _scheduler = _director->getScheduler();
    _scheduler->retain();
}

是從導演類中引用的。這一塊暫時我們不管,接下來深入到_scheduler->schedule函數中分析,如下是函數的具體實現
void Scheduler::schedule(SEL_SCHEDULE selector, Ref *target, float interval, unsigned int repeat, float delay, bool paused)
{
    CCASSERT(target, "Argument target must be non-nullptr");
    
	//定義並且查找鏈表元素
    tHashTimerEntry *element = nullptr;
    HASH_FIND_PTR(_hashForTimers, &target, element);
    
	//沒找到
    if (! element)
    {
		//創建一個鏈表元素
        element = (tHashTimerEntry *)calloc(sizeof(*element), 1);
        element->target = target;
        
		//添加到哈希鏈表中
        HASH_ADD_PTR(_hashForTimers, target, element);
        
        // Is this the 1st element ? Then set the pause level to all the selectors of this target
        element->paused = paused;
    }
    else
    {
        CCASSERT(element->paused == paused, "");
    }
    
	//檢查這個元素的定時器數組,如果數組爲空 則new 10個數組出來備用
    if (element->timers == nullptr)
    {
        element->timers = ccArrayNew(10);
    }
    else
    {
		//循環查找定時器數組,看看是不是曾經定義過相同的定時器,如果定義過,則只需要修改定時器的間隔時間
        for (int i = 0; i < element->timers->num; ++i)
        {
            TimerTargetSelector *timer = dynamic_cast<TimerTargetSelector*>(element->timers->arr[i]);
            
            if (timer && selector == timer->getSelector())
            {
                CCLOG("CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: %.4f to %.4f", timer->getInterval(), interval);
                timer->setInterval(interval);
                return;
            }
        }
		//擴展1個定時器數組
        ccArrayEnsureExtraCapacity(element->timers, 1);
    }
    
	//創建一個定時器,並且將定時器加入到當前鏈表指針的定時器數組中
    TimerTargetSelector *timer = new (std::nothrow) TimerTargetSelector();
    timer->initWithSelector(this, selector, target, interval, repeat, delay);
    ccArrayAppendObject(element->timers, timer);
    timer->release();
}


這一段代碼具體分析瞭如何將自定義定時器加入到鏈表中,並且在鏈表中的存儲結構是怎麼樣的,接下來看看內置的Update定時器。

3.如何定義Update定時器

Update定時器的開啓方法有兩個,分別是:
    /**
     * 開啓自帶的update方法,這個方法會每幀執行一次,默認優先級爲0,並且在所有自定義方法執行之前執行
     */
    void scheduleUpdate(void);

    /**
     * 開啓自帶的update方法,這個方法會每幀執行一次,設定的優先級越小,越優先執行
     */
    void scheduleUpdateWithPriority(int priority);
第一個方法實際上是直接調用第二個方法,並且把優先級設置爲0,我們直接看第二個方法就可以了。

void Node::scheduleUpdateWithPriority(int priority)
{
    _scheduler->scheduleUpdate(this, priority, !_running);
}
具體調用還是要進入到_scheduler->scheduleUpdate。
/** Schedules the 'update' selector for a given target with a given priority.
     The 'update' selector will be called every frame.
     The lower the priority, the earlier it is called.
     @since v3.0
     @lua NA
     */
    template <class T>
    void scheduleUpdate(T *target, int priority, bool paused)
    {
        this->schedulePerFrame([target](float dt){
            target->update(dt);
        }, target, priority, paused);
    }

可以看到這裏主要還是調用了一個schedulePerFrame函數,並且傳入了一個lambda函數。這個函數實際上調用的是target->update,接下來走進schedulePerFrame看看它的實現:
void Scheduler::schedulePerFrame(const ccSchedulerFunc& callback, void *target, int priority, bool paused)
{
	//定義並且查找鏈表元素
    tHashUpdateEntry *hashElement = nullptr;
    HASH_FIND_PTR(_hashForUpdates, &target, hashElement);
	
	//如果找到,就直接改優先級
    if (hashElement)
    {
        // 檢查優先級是否改變
        if ((*hashElement->list)->priority != priority)
        {
			//檢查是否被鎖定
            if (_updateHashLocked)
            {
                CCLOG("warning: you CANNOT change update priority in scheduled function");
                hashElement->entry->markedForDeletion = false;
                hashElement->entry->paused = paused;
                return;
            }
            else
            {
            	// 在這裏先停止到update,後面會加回來 
                unscheduleUpdate(target);
            }
        }
        else
        {
            hashElement->entry->markedForDeletion = false;
            hashElement->entry->paused = paused;
            return;
        }
    }

    // 優先級爲0,加入到_updates0List鏈表中,並且加入到_hashForUpdates表中
    if (priority == 0)
    {
        appendIn(&_updates0List, callback, target, paused);
    }
	// 優先級小於0,加入到_updatesNegList鏈表中,並且加入到_hashForUpdates表中
    else if (priority < 0)
    {
        priorityIn(&_updatesNegList, callback, target, priority, paused);
    }
	// 優先級大於0,加入到_updatesPosList鏈表中,並且加入到_hashForUpdates表中
    else
    {
        // priority > 0
        priorityIn(&_updatesPosList, callback, target, priority, paused);
    }
}
在這裏看上去邏輯還是很清晰的,有兩個函數要重點分析一下,分別是
void Scheduler::appendIn(_listEntry **list, const ccSchedulerFunc& callback, void *target, bool paused)
void Scheduler::priorityIn(tListEntry **list, const ccSchedulerFunc& callback, void *target, int priority, bool paused)

第一個用於添加默認優先級,第二個函數用於添加指定優先級的。首先看添加默認優先級的。
void Scheduler::appendIn(_listEntry **list, const ccSchedulerFunc& callback, void *target, bool paused)
{
	//創建一個鏈表元素
    tListEntry *listElement = new tListEntry();

    listElement->callback = callback;
    listElement->target = target;
    listElement->paused = paused;
    listElement->priority = 0;
    listElement->markedForDeletion = false;
	
	//添加到雙向鏈表中
    DL_APPEND(*list, listElement);

    //創建一個哈希鏈表元素
    tHashUpdateEntry *hashElement = (tHashUpdateEntry *)calloc(sizeof(*hashElement), 1);
    hashElement->target = target;
    hashElement->list = list;
    hashElement->entry = listElement;
	//添加到哈希鏈表中
    HASH_ADD_PTR(_hashForUpdates, target, hashElement);
}

接下來看另一個函數
void Scheduler::priorityIn(tListEntry **list, const ccSchedulerFunc& callback, void *target, int priority, bool paused)
{
	//同上一個函數
    tListEntry *listElement = new tListEntry();

    listElement->callback = callback;
    listElement->target = target;
    listElement->priority = priority;
    listElement->paused = paused;
    listElement->next = listElement->prev = nullptr;
    listElement->markedForDeletion = false;

    //如果鏈表爲空
    if (! *list)
    {
        DL_APPEND(*list, listElement);
    }
    else
    {
        bool added = false;
		//保證鏈表有序
        for (tListEntry *element = *list; element; element = element->next)
        {
			// 如果優先級小於當前元素的優先級,就在這個元素前面插入
            if (priority < element->priority)
            {
                if (element == *list)
                {
                    DL_PREPEND(*list, listElement);
                }
                else
                {
                    listElement->next = element;
                    listElement->prev = element->prev;

                    element->prev->next = listElement;
                    element->prev = listElement;
                }

                added = true;
                break;
            }
        }

        //如果新加入的優先級最低,則加入到鏈表的最後
        if (! added)
        {
            DL_APPEND(*list, listElement);
        }
    }

    //同上一個函數
    tHashUpdateEntry *hashElement = (tHashUpdateEntry *)calloc(sizeof(*hashElement), 1);
    hashElement->target = target;
    hashElement->list = list;
    hashElement->entry = listElement;
    HASH_ADD_PTR(_hashForUpdates, target, hashElement);
}

本文簡單的分析了哈希鏈表以及定時器的存儲和添加,下一篇文章將分析定時器是如何運轉起來的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章