00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00032
00033 #ifndef GROOVX_NUB_TIMER_CC_UTC20050626084019_DEFINED
00034 #define GROOVX_NUB_TIMER_CC_UTC20050626084019_DEFINED
00035
00036 #include "timer.h"
00037
00038 #include "rutz/error.h"
00039
00040 #include "rutz/trace.h"
00041 #include "rutz/debug.h"
00042 GVX_DBG_REGISTER
00043
00044 using rutz::shared_ptr;
00045
00046 nub::timer::timer(unsigned int msec, bool repeat)
00047 :
00048 sig_timeout(),
00049 m_scheduler(0),
00050 m_token(0),
00051 m_msec_delay(msec),
00052 m_is_repeating(repeat),
00053 m_stopwatch()
00054 {}
00055
00056 nub::timer::~timer()
00057 {
00058 cancel();
00059 }
00060
00061 void nub::timer::schedule(rutz::shared_ptr<nub::scheduler> scheduler)
00062 {
00063 GVX_TRACE("nub::timer::schedule");
00064
00065 GVX_PRECONDITION(scheduler.get() != 0);
00066
00067 if (m_msec_delay == 0 && m_is_repeating == true)
00068 {
00069 throw rutz::error("can't schedule a timer callback with "
00070 "delay=0 and repeating=true", SRC_POS);
00071 }
00072
00073
00074 cancel();
00075
00076
00077 m_stopwatch.restart();
00078
00079 dbg_eval_nl(3, m_msec_delay);
00080
00081 m_scheduler = scheduler;
00082
00083
00084
00085
00086 m_token = m_scheduler->schedule(m_msec_delay,
00087 dummy_callback,
00088 static_cast<void*>(this));
00089 }
00090
00091 void nub::timer::cancel()
00092 {
00093 GVX_TRACE("nub::timer::cancel");
00094
00095 m_token.reset(0);
00096 }
00097
00098 void nub::timer::dummy_callback(void* clientdata)
00099 {
00100 GVX_TRACE("nub::timer::dummy_callback");
00101 nub::timer* timer = static_cast<nub::timer*>(clientdata);
00102
00103 GVX_ASSERT(timer != 0);
00104
00105 timer->m_token.reset(0);
00106
00107 dbg_eval_nl(3, timer->m_stopwatch.elapsed().msec());
00108
00109
00110
00111
00112
00113 if (timer->m_is_repeating)
00114 {
00115
00116
00117
00118 GVX_ASSERT(timer->m_msec_delay != 0);
00119
00120 if (timer->m_scheduler.get() != 0)
00121 timer->schedule(timer->m_scheduler);
00122 }
00123
00124 timer->sig_timeout.emit();
00125 }
00126
00127 static const char __attribute__((used)) vcid_groovx_nub_timer_cc_utc20050626084019[] = "$Id: timer.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00128 #endif // !GROOVX_NUB_TIMER_CC_UTC20050626084019_DEFINED