ACE定时器

class CTimer: public ACE_Task_Base
{
public:
    virtual int open(void *args = 0);
	
    virtual int svc(void);

    void wait_for_event(void);

    long schedule(ACE_Event_Handler *cb, void *arg, const ACE_Time_Value &abs_time, const ACE_Time_Value &interval);

    int cancel(ACE_Event_Handler *cb, int dont_call_handle_close = 1);

    int cancel (long timer_id, const void **act = 0, int dont_call_handle_close = 1);
	
    int reset_interval(long timer_id, const ACE_Time_Value &interval);

    void set(ACE_Timer_Queue *timer_queue);

private:
    ACE_Timer_Queue *m_timer_queue;
    ACE_Event m_timer;
};

inline int CTimer::open(void *args /* = 0 */)
{
    // setup timer implement
    ACE_Timer_Queue *timer_queue;
    ACE_NEW_RETURN (timer_queue, ACE_Timer_Heap, -1);
    //ACE_NEW_RETURN (timer_queue, ACE_Timer_Hash, -1);
    //ACE_NEW_RETURN (timer_queue, ACE_Timer_Wheel, -1);
    //ACE_NEW_RETURN (timer_queue, ACE_Timer_List, -1);
    this->set(timer_queue);
	
    return this->activate(THR_NEW_LWP, 1);
}

inline int CTimer::svc()
{
    wait_for_event();
    return 0;
}

inline void CTimer::wait_for_event (void)
{
    printf("Timer run ......\n");
    ACE_Time_Value tv(1, 0);

    while (1)
    {
        ACE_Time_Value max_tv = m_timer_queue->gettimeofday ();

        ACE_Time_Value *this_timeout =	this->m_timer_queue->calculate_timeout (&max_tv);

        if (*this_timeout == ACE_Time_Value::zero)
        {
            this->m_timer_queue->expire ();
        }
        else if (*this_timeout == max_tv)
        {
            ACE_Time_Value next_timeout = *this_timeout + tv;
            if (this->m_timer.wait (&next_timeout) == -1 )
                this->m_timer_queue->expire ();
        }
        else
        {
            ACE_Time_Value next_timeout = m_timer_queue->gettimeofday ();
            next_timeout += *this_timeout;
            if (this->m_timer.wait (&next_timeout) == -1 )
            this->m_timer_queue->expire ();
        }
    }
}

inline long CTimer::schedule (ACE_Event_Handler *cb, void *arg,	const ACE_Time_Value &abs_time,	const ACE_Time_Value &interval)
{
    return this->m_timer_queue->schedule(cb, arg, abs_time, interval);
}

inline int CTimer::cancel (ACE_Event_Handler *cb, int dont_call_handle_close)
{
    return m_timer_queue->cancel (cb, dont_call_handle_close);
}

inline int CTimer::cancel(long timer_id, const void **act /* = 0 */, int dont_call_handle_close /* = 1 */)
{
    return m_timer_queue->cancel(timer_id, act, dont_call_handle_close);
}

inline void CTimer::set (ACE_Timer_Queue *timer_queue)
{
    m_timer_queue = timer_queue;
}

inline int CTimer::reset_interval (long timer_id, const ACE_Time_Value &interval)
{
    return m_timer_queue->reset_interval(timer_id, interval);
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值