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); }
ACE定时器
最新推荐文章于 2024-04-27 10:47:34 发布