00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00016 #ifndef _CLOCK_H_
00017 #define _CLOCK_H_
00018
00019 #include "ace/Singleton.h"
00020 #include "ace/Time_Value.h"
00021 #include "ace/OS_NS_sys_time.h"
00022 #include "ace/Timer_Queue_Adapters.h"
00023 #include "ace/Timer_Heap.h"
00024 #include "ace/Recursive_Thread_Mutex.h"
00025 #include "ace/Thread_Semaphore.h"
00026
00027
00028
00029 namespace channel {
00030
00031 class ActiveTimer : public ACE_Thread_Timer_Queue_Adapter<ACE_Timer_Heap>
00032 {
00033 public:
00034 ACE_Thread_Semaphore exit_sema_;
00035 ActiveTimer() : exit_sema_(0) {}
00036 virtual int close(u_long flag=0) {
00037 ACE_UNUSED_ARG (flag);
00038 ACE_DEBUG((LM_DEBUG, "(%t) timer thread exit...\n"));
00039 exit_sema_.release();
00040 return 0;
00041 }
00042 void wait_timer_thr_exit(void) {
00043 ACE_DEBUG((LM_DEBUG, "(%t) wait for timer thread exit...\n"));
00044 exit_sema_.acquire();
00045 }
00046 };
00047
00048 class ActiveTimerPointer {
00049 friend class ACE_Singleton<ActiveTimerPointer, ACE_Recursive_Thread_Mutex>;
00050 public:
00051
00052
00053 long schedule (ACE_Event_Handler *handler,
00054 const void *act,
00055 const ACE_Time_Value &future_time,
00056 const ACE_Time_Value &interval = ACE_Time_Value::zero)
00057 {
00058 return actimer->schedule(handler,act,future_time,interval);
00059 }
00060 int cancel (long timer_id, const void **act = 0)
00061 {
00062 return actimer->cancel(timer_id, act);
00063 }
00064 void stop(void)
00065 {
00066 if(actimer != NULL) {
00067 actimer->timer_queue()->expire();
00068 actimer->deactivate();
00069 actimer->wait_timer_thr_exit();
00070 ACE_OS::sleep(1);
00071 delete actimer;
00072 actimer = NULL;
00073 }
00074 }
00075 private:
00076 ActiveTimerPointer() { actimer = new ActiveTimer(); actimer->activate(); }
00077 ~ActiveTimerPointer()
00078 {
00079 if(actimer != NULL) {
00080 actimer->timer_queue()->expire();
00081 actimer->deactivate();
00082 actimer->wait_timer_thr_exit();
00083 ACE_OS::sleep(1);
00084 delete actimer;
00085 actimer = NULL;
00086 }
00087 }
00088 ActiveTimer *actimer;
00089 };
00090
00091 typedef ACE_Singleton<ActiveTimerPointer, ACE_Recursive_Thread_Mutex>
00092 Clock;
00093
00094 };
00095
00096
00097 #endif
00098