1、定时器单元模板类定义,实现对定时器参数的管理。
.h文件
/*************************************************************************************
*filename: timer_node_t.h
*
*to do: 定义定时器单元模版,实现对定时器参数的管理
*Create on: 2012-04
*Author: zerok
*check list:
*************************************************************************************/
#ifndef __TIMER_NODE_T_H
#define __TIMER_NODE_T_H
#include "revolver/base_namespace.h"
#include "revolver/base_typedef.h"
BASE_NAMESPACE_BEGIN_DECL
template<class HANDLER>
class BaseTimerDispathInfo_T
{
public:
HANDLER handler_; //触发句柄
const void* act_; //定时器参数
int32_t recurring_; //是否循环定时
};
template <class HANDLER>
class BaseTimerNode_T
{
public:
BaseTimerNode_T();
~BaseTimerNode_T();
void set(HANDLER handler, const void* act, uint32_t timeout_stamp, uint32_t intval, uint32_t timer_id);
HANDLER& get_handler()
{
return handler_;
}
void set_handler(HANDLER& handler)
{
handler_ = handler;
}
const void* get_act() const
{
return act_;
}
void set_act(void* act)
{
act_ = act;
}
uint32_t get_internal() const
{
return internal_;
}
void set_internal(uint32_t internal)
{
internal_ = internal;
}
uint32_t get_timer_id() const
{
return timer_id_;
}
void set_timer_id(uint32_t timer_id)
{
timer_id_ = timer_id;
}
void set_time_stamp(uint32_t stamp)
{
timeout_stamp_ = stamp;
}
uint32_t get_time_stamp() const
{
return timeout_stamp_;
}
void get_dispatch_info(BaseTimerDispathInfo_T<HANDLER> &info);
//获取轮转的位置
void get_revolver_pos(uint8_t& first, uint8_t &second, uint8_t& third, uint8_t& fourth) const;
private:
HANDLER handler_;
const void* act_;
uint32_t timeout_stamp_; //超时时间戳,MS为单位刻度
uint32_t internal_; //循环定时的时间间隔
uint32_t timer_id_; //定时器ID
};
BASE_NAMESPACE_END_DECL
#include "timer_node_t.inl"
#endif
/*************************************************************************************/
.cpp文件
#include <revolver/base_namespace.h>
#include <cstdlib>
BASE_NAMESPACE_BEGIN_DECL
/*****************************************
* 16777216 =256*256*256
* 65536 =256*256
****************************************/
#define FIRST_ROUND 16777216
#define SECOND_ROUND 65536
#define THIRD_ROUND 256
template <class HANDLER>
BaseTimerNode_T<HANDLER>::BaseTimerNode_T(void)
: act_(NULL), timer_id_(0xffffffff)
, timeout_stamp_(0), internal_(0), handler_(0)
{
}
template <class HANDLER>
BaseTimerNode_T<HANDLER>::~BaseTimerNode_T(void)
{
}
template <class HANDLER>
void BaseTimerNode_T<HANDLER>::set(HANDLER handler, const void* act,
uint32_t timeout_stamp, uint32_t intval, uint32_t timer_id)
{
handler_ = handler;
act_ = act;
timeout_stamp_ = timeout_stamp;
internal_ = intval;
timer_id_ = timer_id;
}
template <class HANDLER>
void BaseTimerNode_T<HANDLER>::get_dispatch_info(BaseTimerDispathInfo_T<HANDLER> &info)
{
info.act_ = act_;
info.handler_ = handler_;
info.recurring_ = internal_ > 0 ? true : false;
}
template <class HANDLER>
void BaseTimerNode_T<HANDLER>::get_revolver_pos(uint8_t& first, uint8_t &second, uint8_t& third, uint8_t& fourth) const
{
first = (uint8_t)(timeout_stamp_ / FIRST_ROUND);
second = (uint8_t)((timeout_stamp_ % FIRST_ROUND) / SECOND_ROUND);
third = (uint8_t)((timeout_stamp_ % SECOND_ROUND) / THIRD_ROUND);
fourth = (uint8_t) (timeout_stamp_ % THIRD_ROUND);
}
BASE_NAMESPACE_END_DECL
2、定义定时器轮状类,实现刻度推进与轮切换。
.h文件
/*************************************************************************************
*filename: timer_ring.h
*
*to do: 定义定时器轮转类,实现刻度推进和轮切换
*Create on: 2012-04
*Author: zerok
*check list:
*************************************************************************************/
#ifndef __TIMER_RING_H
#define __TIMER_RING_H
#include "revolver/base_namespace.h"
#include "revolver/base_typedef.h"
#include <set>
#include <vector>
BASE_NAMESPACE_BEGIN_DECL
using namespace std;
typedef std::set<uint32_t> ElementSet;
typedef std::vector<ElementSet> RingVector;
class IRingEvent
{
public:
//轮的原子处理事件
virtual void ring_event(uint8_t ring_id, uint32_t timer_id) = 0;
};
//时间计数环对象,默认是256个轮单元,
class CTimerRing
{
public:
CTimerRing(uint8_t ring_id = 0);
~CTimerRing();
void reset();
bool add_element(uint8_t pos, uint32_t timer_id);
void delete_element(uint8_t pos, uint32_t timer_id);
//scale是轮转的刻度,ring_handler是原子处理句柄,如果返回值为TRUE,说明本轮到了最末端,需要切换轮
bool cycle(uint32_t& scale, IRingEvent* ring_handler);
uint32_t get_pos() const {return pos_;}
void set_pos(uint32_t pos) {pos_ = pos;}
void set_ring_id(uint8_t id) {ring_id_ = id;}
uint8_t get_ring_id() const {return ring_id_;}
private:
void clear();
private:
uint8_t ring_id_; //轮子ID
uint32_t pos_; //轮子指针位置
RingVector ring_; //轮子
};
BASE_NAMESPACE_END_DECL
#endif
/*************************************************************************************/
#include <stdlib.h>
#include "timer_ring.h"
BASE_NAMESPACE_BEGIN_DECL
#define RING_MAX_SIZE 256
CTimerRing::CTimerRing(uint8_t ring_id) : ring_id_(ring_id)
{
for(uint16_t i = 0; i < RING_MAX_SIZE; i ++)
{
ElementSet element_set;
ring_.push_back(element_set);
}
pos_ = 0;
}
CTimerRing::~CTimerRing()
{
clear();
}
void CTimerRing::clear()
{
for(uint16_t i = 0; i < RING_MAX_SIZE; i ++)
{
ring_[i].clear();
}
}
void CTimerRing::reset()
{
pos_ = 0;
}
bool CTimerRing::add_element(uint8_t pos, uint32_t timer_id)
{
bool ret = false;
if(pos != pos_ || ring_id_ == 0)
{
ring_[pos].insert(ElementSet::value_type(timer_id));
ret = true;
}
return ret;
}
void CTimerRing::delete_element(uint8_t pos, uint32_t timer_id)
{
ring_[pos].erase(timer_id);
}
bool CTimerRing::cycle(uint32_t& scale, IRingEvent* ring_handler)
{
if(ring_handler == NULL)
return false;
bool ret = false;
uint32_t new_pos = scale + pos_;
if(new_pos >= RING_MAX_SIZE)
{
new_pos = RING_MAX_SIZE;
ret = true;
scale = scale + pos_ - new_pos;
}
else
scale = 0;
ElementSet::iterator it;
for(uint16_t pos = pos_; pos < new_pos; pos ++)
{
for(it = ring_[pos].begin(); it != ring_[pos].end(); ++ it) //触发上一刻度的超时,有可能是中途插入的定时器
{
ring_handler->ring_event(ring_id_, *it);
}
ring_[pos].clear();
pos_ ++;
if(pos_ >= RING_MAX_SIZE)
{
pos_ = pos_ % RING_MAX_SIZE;
}
for(it = ring_[pos_].begin(); it != ring_[pos_].end(); ++ it)
{
ring_handler->ring_event(ring_id_, *it);
}
ring_[pos_].clear();
}
return ret;
}
BASE_NAMESPACE_END_DECL
.cpp文件
#include <stdlib.h>
#include "timer_ring.h"
BASE_NAMESPACE_BEGIN_DECL
#define RING_MAX_SIZE 256
CTimerRing::CTimerRing(uint8_t ring_id) : ring_id_(ring_id)
{
for(uint16_t i = 0; i < RING_MAX_SIZE; i ++)
{
ElementSet element_set;
ring_.push_back(element_set);
}
pos_ = 0;
}
CTimerRing::~CTimerRing()
{
clear();
}
void CTimerRing::clear()
{
for(uint16_t i = 0; i < RING_MAX_SIZE; i ++)
{
ring_[i].clear();
}
}
void CTimerRing::reset()
{
pos_ = 0;
}
bool CTimerRing::add_element(uint8_t pos, uint32_t timer_id)
{
bool ret = false;
if(pos != pos_ || ring_id_ == 0)
{
ring_[pos].insert(ElementSet::value_type(timer_id));
ret = true;
}
return ret;
}
void CTimerRing::delete_element(uint8_t pos, uint32_t timer_id)
{
ring_[pos].erase(timer_id);
}
bool CTimerRing::cycle(uint32_t& scale, IRingEvent* ring_handler)
{
if(ring_handler == NULL)
return false;
bool ret = false;
uint32_t new_pos = scale + pos_;
if(new_pos >= RING_MAX_SIZE)
{
new_pos = RING_MAX_SIZE;
ret = true;
scale = scale + pos_ - new_pos;
}
else
scale = 0;
ElementSet::iterator it;
for(uint16_t pos = pos_; pos < new_pos; pos ++)
{
for(it = ring_[pos].begin(); it != ring_[pos].end(); ++ it) //触发上一刻度的超时,有可能是中途插入的定时器
{
ring_handler->ring_event(ring_id_, *it);
}
ring_[pos].clear();
pos_ ++;
if(pos_ >= RING_MAX_SIZE)
{
pos_ = pos_ % RING_MAX_SIZE;
}
for(it = ring_[pos_].begin(); it != ring_[pos_].end(); ++ it)
{
ring_handler->ring_event(ring_id_, *it);
}
ring_[pos_].clear();
}
return ret;
}
BASE_NAMESPACE_END_DECL
源码解析:
分析数据成员可以知道,ring_是一个std::vector<std::set<uint32_t>>的类型。根据宏定义,ring_中共有256个std::set<uint32_t>>。
功能:
<1>、插入元素。向定时器轮子中加入一个元素时,首先判断要插入的轮子指针位置(即std::set在std::vector中的位置)是否与当前轮子指针位置相同,若不同,则直接插入;若相同,则判断当前轮子的ID是否是0,若是,则直接插入,否则,插入元素失败。
<2>、删除元素。直接删除对应位置的std::set里面的对应ID的定时器。
<3>、轮子轮转。函数原型。bool cycle(uint32_t& scale,IRingEvent* ring_handler),scale是轮转的刻度,ring_handler是原子处理句柄,如果返回值为TRUE,说明本轮到了最末端,需要切换轮。
轮询步骤:
1>计算当前std::set的位置加上轮转刻度后的值new_pos,作为轮询的std::set的范围的终点。若new_pos>=256,即大于std::vector设置的尺寸,则new_pos=256,且轮转刻度变为计算new_pos时超出256的部分;若new_pos<256,就以new_pos作为轮询的终点。对于位置在[pos_,new_pos_)间的每一个std::set,遍历其中的每一个元素。调用ring_handler->ring_event(ring_id_,*it)来处理事件。
3、定义轮转定时器。实现一个4轮、刻度转换的定时器轮序列,精确的最小的单位为毫秒。
.h文件
/*************************************************************************************
*filename: timer_queue_t.h
*
*to do: 定义轮转定时器,实现一个4轮刻度转换的定时器轮序列,精确的最小单位为毫秒
*Create on: 2012-04
*Author: zerok
*check list:
*************************************************************************************/
#ifndef __TIMER_QUEUE_T_H
#define __TIMER_QUEUE_T_H
#include "revolver/timer_ring.h"
#include "revolver/object_pool.h"
#include "revolver/timer_node_t.h"
#include "revolver/base_timer_value.h"
#include "revolver/base_event_handler.h"
#include <list>
#include <assert.h>
BASE_NAMESPACE_BEGIN_DECL
#define RINGS_SIZE 4
typedef std::list<uint32_t> FreeTimerIDList;
template<class HANDLER, class FUNCTOR, class LOCK>
class CTimerQueue_T : public IRingEvent
{
public:
typedef vector<BaseTimerNode_T<HANDLER>*> TimerNodeArray;
CTimerQueue_T(FUNCTOR* functor, size_t heap_size = TIMER_POOL_SIZE);
~CTimerQueue_T();
//插入一个定时器
uint32_t schedule(HANDLER handler, const void* act, uint32_t delay, uint32_t interval = 0);
//取消一个定时器
void cancel_timer(uint32_t timer_id, const void **act);
//重置一个定时器
uint32_t reset_timer(uint32_t timer_id, uint32_t delay, uint32_t interval = 0);
//扫描超时,返回下一个超时间间隔,最小为10MS,最大为50MS
virtual uint32_t expire();
//原子处理事务
void ring_event(uint8_t ring_id, uint32_t timer_id);
//判断定时器堆是否满
bool full() const;
FUNCTOR &upcall_functor (void);
#if _DEBUG
//用于触发临界点,0,255,255,255
void set_ring_id();
#endif
protected:
void dispatch_info(BaseTimerDispathInfo_T<HANDLER>& info, uint32_t timer_id);
//获取一个空闲的Timer node序号
uint32_t get_free_node();
void clear();
void alloc_heap(size_t size);
void insert_node(BaseTimerNode_T<HANDLER>* node);
void delete_node(BaseTimerNode_T<HANDLER>* node);
uint32_t revolver(uint32_t scale);
protected:
ObjectPool<BaseTimerNode_T<HANDLER>, TIMER_POOL_SIZE> node_pool_;
//定时器堆
TimerNodeArray heap_;
size_t heap_size_;
//已经占用的HEAP个数
uint32_t used_num_;
uint32_t cur_heap_pos_;
FreeTimerIDList freeTimers_;
//轮序列
CTimerRing rings_[RINGS_SIZE]; //0表示低位轮,3表示高位轮,一个轮表示256个刻度,4个轮正好是32位整形数描述
//初始时刻
CBaseTimeValue start_time_;
//上一次expire处理的时刻
CBaseTimeValue prev_time_;
LOCK mutex_;
FUNCTOR* functor_;
};
//定义functor
class CTimerFunctor
{
public:
CTimerFunctor()
{
}
~CTimerFunctor()
{
}
void registration(CEventHandler* handler, uint32_t id)
{
handler->add_timer_event(id);
}
void cancel_timer(CEventHandler *handler, uint32_t id)
{
handler->del_timer_event(id);
}
void timer_out(CEventHandler *event_handler,
const void *act,
uint32_t timer_id)
{
event_handler->del_timer_event(timer_id);
event_handler->handle_timeout(act, timer_id);
}
};
BASE_NAMESPACE_END_DECL
#include "timer_queue_t.inl"
#endif
/*************************************************************************************/
.cpp
/
#include "revolver/timer_queue_t.h"
#include "revolver/base_guard.h"
#include <iostream>
BASE_NAMESPACE_BEGIN_DECL
#define UNINT32_MAX 4294967296
#define SELECT_DELAY 5
/
template<class HANDLER, class FUNCTOR, class LOCK>
CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::CTimerQueue_T(FUNCTOR* functor, size_t heap_size /* = 10240 */)
{
heap_size_ = 0;
alloc_heap(heap_size);
used_num_ = 0;
cur_heap_pos_ = 0;
//获得起始时间
start_time_ = CBaseTimeValue::get_time_value();
prev_time_ = start_time_;
functor_ = functor;
for(uint8_t index = 0; index < RINGS_SIZE; index ++)
{
rings_[index].set_ring_id(index);
}
}
template<class HANDLER, class FUNCTOR, class LOCK>
CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::~CTimerQueue_T()
{
clear();
}
template<class HANDLER, class FUNCTOR, class LOCK>
void CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::clear()
{
BASE_GUARD(LOCK, cf_mon, mutex_);
for(uint32_t i = 0; i < heap_size_; i ++)
{
if(heap_[i] != NULL)
{
node_pool_.push_obj(heap_[i]);
heap_[i] = NULL;
freeTimers_.push_back(i);
}
}
used_num_ = 0;
cur_heap_pos_ = 0;
}
template<class HANDLER, class FUNCTOR, class LOCK>
bool CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::full() const
{
return used_num_ >= heap_size_ - 1;
}
template<class HANDLER, class FUNCTOR, class LOCK>
void CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::alloc_heap(size_t size)
{
heap_.resize(size + heap_size_);
for(uint32_t i = 0; i < size; i ++)
{
heap_[heap_size_] = NULL;
if(heap_size_ > 0)
freeTimers_.push_back(heap_size_);
heap_size_ ++;
}
}
template<class HANDLER, class FUNCTOR, class LOCK>
uint32_t CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::get_free_node()
{
if(full())
{
alloc_heap(heap_size_ + 1);
}
uint32_t ret = freeTimers_.front();
freeTimers_.pop_front();
if(heap_[ret] != NULL)
assert(0);
return ret;
}
template<class HANDLER, class FUNCTOR, class LOCK>
FUNCTOR &CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::upcall_functor()
{
return *(this->functor_);
}
template<class HANDLER, class FUNCTOR, class LOCK>
uint32_t CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::schedule(HANDLER handler,
const void *act,
uint32_t delay,
uint32_t interval)
{
BASE_GUARD_RETURN(LOCK, cf_mon, mutex_, 0);
BaseTimerNode_T<HANDLER>* timer_obj = node_pool_.pop_obj();
if(timer_obj != NULL)
{
uint32_t timer_id = get_free_node();
CBaseTimeValue cur_timer = CBaseTimeValue::get_time_value();
uint64_t distance = delay; //直接以当前时间作为坐标,相差一个扫描间隔20MS
if(cur_timer > start_time_)
distance = (cur_timer.msec() - start_time_.msec() + delay);// SELECT_DELAY;
distance = distance % (UNINT32_MAX);
timer_obj->set(handler, act, (uint32_t)(core_max(distance, 1)), interval, timer_id);
heap_[timer_id] = timer_obj;
used_num_ ++;
insert_node(timer_obj);
upcall_functor().registration(timer_obj->get_handler(), timer_id);
return timer_id;
}
return 0;
}
template<class HANDLER, class FUNCTOR, class LOCK>
void CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::cancel_timer(uint32_t timer_id, const void **act)
{
BASE_GUARD(LOCK, cf_mon, mutex_);
if(timer_id < heap_size_ && heap_[timer_id] != NULL)
{
BaseTimerNode_T<HANDLER>* timer_obj = heap_[timer_id];
delete_node(timer_obj);
heap_[timer_id] = NULL;
if(used_num_ > 0)
used_num_ --;
freeTimers_.push_back(timer_id);
*act = timer_obj->get_act();
upcall_functor().cancel_timer(timer_obj->get_handler(), timer_id);
node_pool_.push_obj(timer_obj);
}
}
template<class HANDLER, class FUNCTOR, class LOCK>
uint32_t CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::reset_timer(uint32_t timer_id, uint32_t delay, uint32_t interval /* = 0 */)
{
BASE_GUARD_RETURN(LOCK, cf_mon, mutex_, 0);
if(timer_id < heap_size_ && heap_[timer_id] != NULL)
{
BaseTimerNode_T<HANDLER>* timer_obj = heap_[timer_id];
delete_node(timer_obj);
timer_obj->set_internal(interval);
CBaseTimeValue cur_timer = CBaseTimeValue::get_time_value();
uint64_t distance = delay; // SELECT_DELAY; //直接以当前时间作为坐标,相差一个扫描间隔20MS
if(cur_timer > start_time_)
distance = (cur_timer.msec() - start_time_.msec() + delay); // SELECT_DELAY;
distance = distance % (UNINT32_MAX);
timer_obj->set_time_stamp(core_max(distance, 1));
insert_node(timer_obj);
return timer_id;
}
else
{
return 0;
}
}
template<class HANDLER, class FUNCTOR, class LOCK>
uint32_t CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::expire()
{
BASE_GUARD_RETURN(LOCK, cf_mon, mutex_, 0);
uint32_t ret = SELECT_DELAY; //默认20MS
CBaseTimeValue cur_timer = CBaseTimeValue::get_time_value();
if(cur_timer > prev_time_)
{
uint32_t scale = static_cast<uint32_t>((cur_timer.msec() - prev_time_.msec()));// SELECT_DELAY);
if(scale > 0)
{
ret = revolver(scale);
prev_time_ = cur_timer;
}
}
return ret;
}
template<class HANDLER, class FUNCTOR, class LOCK>
uint32_t CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::revolver(uint32_t scale)
{
uint32_t ret = SELECT_DELAY;
uint8_t index = 0;
uint32_t rewind_scale = scale;
while(rewind_scale > 0)
{
index = 0;
if(rings_[index].cycle(rewind_scale, this)) //扫描第一轮
{
index ++;
uint32_t sc = 1;
while(rings_[index].cycle(sc, this))//扫描下一轮,刻度只往前推进1格
{
sc = 1;
index ++;
if(index >= RINGS_SIZE)
{
start_time_ = CBaseTimeValue::get_time_value();
break;
}
}
}
}
return ret;
}
template<class HANDLER, class FUNCTOR, class LOCK>
void CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::ring_event(uint8_t ring_id, uint32_t timer_id)
{
if(heap_[timer_id] == NULL)
return;
BaseTimerNode_T<HANDLER>* timer_obj = heap_[timer_id];
if(ring_id == 0)
{
//超时通知
BaseTimerDispathInfo_T<HANDLER> info;
if(timer_obj != NULL)
{
timer_obj->get_dispatch_info(info);
this->dispatch_info(info, timer_id);
node_pool_.push_obj(timer_obj);
}
heap_[timer_id] = NULL;
if(used_num_ > 0)
used_num_ --;
freeTimers_.push_back(timer_id);
}
else if(ring_id < RINGS_SIZE)
{
//移动RING的位置,例如,第二轮子的某个单元的所有定时器映射到第一轮子上进行重新分布
insert_node(timer_obj);
}
}
template<class HANDLER, class FUNCTOR, class LOCK>
void CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::insert_node(BaseTimerNode_T<HANDLER>* node)
{
uint32_t timer_id = node->get_timer_id();
uint8_t poss[RINGS_SIZE] = {0};
node->get_revolver_pos(poss[RINGS_SIZE - 1], poss[RINGS_SIZE - 2], poss[RINGS_SIZE - 3], poss[RINGS_SIZE - 4]);
uint8_t index = RINGS_SIZE - 1;
/*
bool CTimerRing::add_element(uint8_t pos, uint32_t timer_id)
{
bool ret = false;
if(pos != pos_ || ring_id_ == 0)
{
ring_[pos].insert(ElementSet::value_type(timer_id));
ret = true;
}
return ret;
}
*/
while(!rings_[index].add_element(poss[index], timer_id))
{
if(index == 0)
break ;
index --;
}
}
template<class HANDLER, class FUNCTOR, class LOCK>
void CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::delete_node(BaseTimerNode_T<HANDLER>* node)
{
uint32_t timer_id = node->get_timer_id();
uint8_t poss[RINGS_SIZE] = {0};
node->get_revolver_pos(poss[RINGS_SIZE - 1], poss[RINGS_SIZE - 2], poss[RINGS_SIZE - 3], poss[RINGS_SIZE - 4]);
for(uint8_t index = 0; index < RINGS_SIZE; index ++) //在每个轮上进行删除
{
rings_[index].delete_element(poss[index], timer_id);
}
}
template<class HANDLER, class FUNCTOR, class LOCK>
void CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::dispatch_info(BaseTimerDispathInfo_T<HANDLER>& info, uint32_t timer_id)
{
}
#if _DEBUG
template<class HANDLER, class FUNCTOR, class LOCK>
void CTimerQueue_T<HANDLER, FUNCTOR, LOCK>::set_ring_id()
{
rings_[1].set_pos(255);
rings_[2].set_pos(0);
rings_[3].set_pos(0);
}
#endif
BASE_NAMESPACE_END_DECL
/
功能解析:
<1>、插入一个定时器节点。
首先获取定时器节点的ID,计算超时时间戳在各个轮子上的位置。并尝试在各个轮子上插入定时器。
<2>、删除定时器节点。
首先获取定时器节点ID,计算超时时间戳在各个轮子上的位置。并在每个轮子上进行删除。
<3>、调度。
首先从定时器对象池中取出一个定时器对象,再找出一个空闲的定时器节点编号。然后设置取出的定时器对象的触发句柄、定时器参数、超时时间戳、循坏定时的时间间隔、定时器ID,再在定时器对象堆上插入定时器对象。在轮转轮子里插入定时器对象,最后事件器注册事件。
<4>、删除一个定时器。
<5>、取消一个定时器。
<6>、重置定时器。
<7>、到期定时器事件的处理。