timer.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2004-2007 University of Southern California
00006 // Rob Peters <rjpeters at usc dot edu>
00007 //
00008 // created: Thu Oct 14 10:56:13 2004
00009 // commit: $Id: timer.cc 10065 2007-04-12 05:54:56Z rjpeters $
00010 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/nub/timer.cc $
00011 //
00012 // --------------------------------------------------------------------
00013 //
00014 // This file is part of GroovX.
00015 //   [http://ilab.usc.edu/rjpeters/groovx/]
00016 //
00017 // GroovX is free software; you can redistribute it and/or modify it
00018 // under the terms of the GNU General Public License as published by
00019 // the Free Software Foundation; either version 2 of the License, or
00020 // (at your option) any later version.
00021 //
00022 // GroovX is distributed in the hope that it will be useful, but
00023 // WITHOUT ANY WARRANTY; without even the implied warranty of
00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025 // General Public License for more details.
00026 //
00027 // You should have received a copy of the GNU General Public License
00028 // along with GroovX; if not, write to the Free Software Foundation,
00029 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
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   // Cancel any possible previously pending invocation.
00074   cancel();
00075 
00076   // Note the time when the current scheduling request was made.
00077   m_stopwatch.restart();
00078 
00079   dbg_eval_nl(3, m_msec_delay);
00080 
00081   m_scheduler = scheduler;
00082 
00083   // Note that the returned token might be null for one reason or
00084   // another (e.g. if the scheduler decides to run the callback
00085   // immediately rather than scheduling a deferred callback).
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   // NOTE: make sure we re-schedule a repeating event BEFORE we
00110   // emit the signal and trigger the callbacks; this way, it's
00111   // possible for code inside the callback to cancel() this timer
00112   // callback and end the repeating.
00113   if (timer->m_is_repeating)
00114     {
00115       // can't allow a timer callback that is both repeating and
00116       // immediate (delay == 0), otherwise we fall into an
00117       // infinite loop
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

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:40 2008 by Doxygen version 1.5.5.