ACE之定时器队列类

基础组件

ACE_Abstract_Timer_Queue

模板类,定时器队列的抽象类,其模板参数表示定时器回调类型,默认是ACE_Event_Handler

ACE_Abstract_Timer_Queue<TYPE>

方法都为纯虚函数

virtual bool is_empty (void) const = 0;
virtual const ACE_Time_Value &earliest_time (void) const = 0;
virtual long schedule (const TYPE &type,const void *act,const ACE_Time_Value &future_time,const ACE_Time_Value &interval = ACE_Time_Value::zero) = 0;
virtual int expire (const ACE_Time_Value &current_time) = 0;
virtual int expire (void) = 0;
virtual int expire_single(ACE_Command_Base & pre_dispatch_command) = 0;
virtual int reset_interval (long timer_id,const ACE_Time_Value &interval) = 0;
virtual int cancel (const TYPE &type,int dont_call_handle_close = 1) = 0;
virtual int cancel (long timer_id,const void **act = 0,int dont_call_handle_close = 1) = 0;
virtual int close (void) = 0;
virtual ACE_Time_Value gettimeofday (void) = 0;
virtual void gettimeofday (ACE_Time_Value (*gettimeofday)(void)) = 0;
virtual ACE_Time_Value *calculate_timeout (ACE_Time_Value *max) = 0;
virtual ACE_Time_Value *calculate_timeout (ACE_Time_Value *max,ACE_Time_Value *the_timeout) = 0;
virtual ACE_Time_Value current_time() = 0;
virtual ITERATOR & iter (void) = 0;
virtual ACE_Timer_Node_T<TYPE> *remove_first (void) = 0;
virtual ACE_Timer_Node_T<TYPE> *get_first (void) = 0;
virtual void dump (void) const = 0;

ACE_Timer_Queue_Upcall_Base

模板类,继承ACE_Abstract_Timer_Queue,提供回调功能

ACE_Timer_Queue_Upcall_Base<TYPE, FUNCTOR>
#FUNCTOR *upcall_functor_
#bool const delete_upcall_functor_
+FUNCTOR & upcall_functor(void)
ACE_Abstract_Timer_Queue<TYPE>

FUNCTOR:是超时的处理函数,ACE_Timer_Hash_UpcallACE_Event_Handler_Handle_Timeout_Upcall是其中的一个类型,其需要包含下面的方法

int registration (ACE_Timer_Queue &timer_queue,ACE_Event_Handler *handler,const void *arg);
int preinvoke (ACE_Timer_Queue &timer_queue,ACE_Event_Handler *handler,const void *arg,int recurring_timer,const ACE_Time_Value &cur_time,const void *&upcall_act);
int timeout (ACE_Timer_Queue &timer_queue,ACE_Event_Handler *handler,const void *arg,int recurring_timer,const ACE_Time_Value &cur_time);
int postinvoke (ACE_Timer_Queue &timer_queue,ACE_Event_Handler *handler,const void *arg,int recurring_timer,const ACE_Time_Value &cur_time,const void *upcall_act);
int cancel_type (ACE_Timer_Queue &timer_queue,ACE_Event_Handler *handler,int dont_call,int &requires_reference_counting);
int cancel_timer (ACE_Timer_Queue &timer_queue,ACE_Event_Handler *handler,int dont_call,int requires_reference_counting);
int deletion (ACE_Timer_Queue &timer_queue,ACE_Event_Handler *handler,const void *arg);

ACE_Timer_Queue_T

模板类,继承ACE_Timer_Queue_Upcall_Base,是其它定时器队列的实现基类。增加了同步机制,以及获取时间策略(主要是获取当前时间)

ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>
#ACE_LOCK mutex_
#ACE_Free_List > *free_list_
#TIME_POLICY time_policy_
#bool const delete_free_list_
- ACE_Time_Value timeout_
- ACE_Time_Value timer_skew_
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>
ACE_Timer_Hash_T<TYPE, FUNCTOR, ACE_LOCK, BUCKET, TIME_POLICY>
ACE_Timer_List_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>
ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>
ACE_Timer_Queue_Upcall_Base

schedule_i 和reschedule纯虚函数,由具体的定时器队列子类实现

virtual long schedule_i (const TYPE &type,const void *act,const ACE_Time_Value &future_time,const ACE_Time_Value &interval) = 0;
virtual void reschedule (ACE_Timer_Node_T<TYPE> *) = 0;

ACE_Command_Base

ACE_Timer_Queue_T的方法expire_single依赖ACE_Command_Base

«abstract»
ACE_Command_Base
+int execute(void *arg)
ACE_Member_Function_Command<Receiver>
- Receiver &receiver_
- PTMF ptmf_
ACE_Command_Callback<Receiver, Action>
- Receiver &receiver_
- Action action_

添加定时器

schedule:添加定时器节点,会调用schedule_i,以及调用this->upcall_functor ().registration (*this,type,act);
包含的子类有

  • ACE_Timer_Heap_T:堆实现的定时器队列
  • ACE_Timer_Hash_T:哈希表实现的定时器队列
  • ACE_Timer_List_T:链表实现的定时器队列
  • ACE_Timer_Wheel_T:时间轮实现的定时器队列

定时器超时

expire,expire_single是处理定时器超时
在expire方法中,会调用dispatch_info_i方法判断是否有超时的定时器,通过earliest_time看最早的时间是否小于当前时间,如果小于,则删除最早的节点,如果定时器是周期性的,则计算定时器的下一次超时时间,重新添加到队列中,如果不是周期性的,则直接释放定时器

template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_POLICY> int
ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>::dispatch_info_i (const ACE_Time_Value &cur_time,
                                                             ACE_Timer_Node_Dispatch_Info_T<TYPE> &info)
{
  ACE_TRACE ("ACE_Timer_Queue_T::dispatch_info_i");

  if (this->is_empty ())
    return 0;

  ACE_Timer_Node_T<TYPE> *expired = 0;

  if (this->earliest_time () <= cur_time)
    {
      expired = this->remove_first ();

      // Get the dispatch info
      expired->get_dispatch_info (info);

      // Check if this is an interval timer.
      if (expired->get_interval () > ACE_Time_Value::zero)
        {
          // Make sure that we skip past values that have already
          // "expired".
          this->recompute_next_abs_interval_time (expired, cur_time);

          // Since this is an interval timer, we need to reschedule
          // it.
          this->reschedule (expired);
        }
      else
        {
          // Call the factory method to free up the node.
          this->free_node (expired);
        }

      return 1;
    }

  return 0;
}

如果有会调用preinvoke,upcallpostinvoke方法,内部会调用functor的preinvoke,timeoutpostinvoke方法

template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_POLICY> ACE_INLINE void
ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>::upcall (ACE_Timer_Node_Dispatch_Info_T<TYPE> &info,
                                                    const ACE_Time_Value &cur_time)
{
  this->upcall_functor ().timeout (*this,
                                   info.type_,
                                   info.act_,
                                   info.recurring_timer_,
                                   cur_time);
}

而ACE_Event_Handler_Handle_Timeout_Upcall的timeout方法会调用ACE_Event_Handler的handle_timeout方法触发超时操作

int
ACE_Event_Handler_Handle_Timeout_Upcall::
timeout (ACE_Timer_Queue &timer_queue,
        ACE_Event_Handler *event_handler,
        const void *act,
        int recurring_timer,
        const ACE_Time_Value &cur_time)
{
  int requires_reference_counting = 0;

  if (!recurring_timer)
    {
      requires_reference_counting =
        event_handler->reference_counting_policy ().value () ==
        ACE_Event_Handler::Reference_Counting_Policy::ENABLED;
    }

  // Upcall to the <handler>s handle_timeout method.
  if (event_handler->handle_timeout (cur_time, act) == -1)
    {
      if (event_handler->reactor_timer_interface ())
        event_handler->reactor_timer_interface ()->cancel_timer (event_handler, 0);
      else
        timer_queue.cancel (event_handler, 0); // 0 means "call handle_close()".
    }

  if (!recurring_timer &&
      requires_reference_counting)
    {
      event_handler->remove_reference ();
    }

  return 0;
}

定时器的取消

cancel:是在其子类中实现,会调用父类ACE_Timer_Queue_Upcall_Base的functor的cancel_typecancel_timer

close

是在其子类中实现,会调用父类ACE_Timer_Queue_Upcall_Base的functor的deletion

ACE_Timer_Heap_T

内部使用堆数据结构来管理定时器节点
ACE_Timer_Node_T<TYPE> **heap_存储定时器节点
ssize_t *timer_ids_定时器id数组,数组下标表示定时器id,存储的是定时器节点在heap中的索引
扩容策略为当定时器数达到堆大小时,会扩大到原来的2倍

ACE_Timer_Wheel_T

使用时间轮来管理定时器
ACE_Timer_Node_T<TYPE>** spokes_表示时间轮的轮幅
u_int spoke_count_表示时间轮幅的大小
int spoke_bits_表示轮幅所在的位大小,最小3位,最大占12位
int res_bits_表示时间分辨率所占的位数,最小1位,最大20位
其生成的timer_id为long类型,其中低位spoke_bits_表示属于哪个spoke,高位(32-spoke_bits_)表示定时器的id
初始化时每个轮幅的第一个即spokes[i]的设置为

root->set(0, 0, ACE_Time_Value::zero, ACE_Time_Value::zero, root, root, 0);

即timer_id为0

ACE_Timer_Node_T定时器节点

用于描述单个定时器

ACE_Timer_Node_T<TYPE>
- TYPE type_
- const void *act_
- ACE_Time_Value timer_value_
- ACE_Time_Value interval_
- ACE_Timer_Node_T<TYPE> *prev_
- ACE_Timer_Node_T<TYPE> *next_
- long timer_id_

type_:表示定时器的回调处理器类型
act_:定时器的异步完成token
timer_value_:表示定时器的超时时间
interval_:表示定时器是周期性时的周期时间
prev_:表示前一个定时器
next_:表示下一个定时器
timer_id_:定时器的id,用于取消定时器

使用

在Reactor的实现类使用,其作为实现类的成员

ACE_Timer_Queue *timer_queue_;

ACE_Timer_Queue是ACE_Abstract_Timer_Queue<ACE_Event_Handler*>的别名

typedef ACE_Abstract_Timer_Queue<ACE_Event_Handler*> ACE_Timer_Queue;

Reactor的实现类有

  • ACE_TP_Reactor
  • ACE_Dev_Poll_Reactor
  • ACE_Select_Reactor
  • ACE_Msg_WFMO_Reactor
  • ACE_WFMO_Reactor
    在其对应的open方法中创建,默认是使用ACE_Timer_Heap
if (result != -1 && this->timer_queue_ == 0)
    {
      ACE_NEW_RETURN (this->timer_queue_,
                      ACE_Timer_Heap,
                      -1);

      this->delete_timer_queue_ = true;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值