[Tinyos-2-commits] CVS: tinyos-2.x/tos/lib/timer VirtualizeTimerC.nc, 1.6, 1.7

David Gay idgay at users.sourceforge.net
Fri Aug 11 14:07:39 PDT 2006


Update of /cvsroot/tinyos/tinyos-2.x/tos/lib/timer
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv24244

Modified Files:
	VirtualizeTimerC.nc 
Log Message:
simplify logic


Index: VirtualizeTimerC.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/tos/lib/timer/VirtualizeTimerC.nc,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** VirtualizeTimerC.nc	10 Aug 2006 00:11:41 -0000	1.6
--- VirtualizeTimerC.nc	11 Aug 2006 21:07:37 -0000	1.7
***************
*** 56,79 ****
  
    Timer_t m_timers[NUM_TIMERS];
! 
!   enum {
!     /* bitmask */
!     S_TIMER_RUNNING = 1,
!     S_TIMER_CHANGED = 2
!   };
!   bool state;
  
    task void executeTimersNow();
  
!   void executeTimers(uint32_t then)
    {
!     int32_t min_remaining = (1UL<<31)-1; //max signed int32_t
      bool min_remaining_isset = FALSE;
!     int num;
  
!     /* We always come here with the timer stopped */
!     state = 0;
  
!     for(num=0; num<NUM_TIMERS; num++)
      {
        Timer_t* timer = &m_timers[num];
--- 56,78 ----
  
    Timer_t m_timers[NUM_TIMERS];
!   bool m_timers_changed;
  
    task void executeTimersNow();
  
!   void executeTimers()
    {
!     /* This code supports a maximum dt of MAXINT. If min_remaining and
!        remaining were switched to uint32_t, and the logic changed a
!        little, dt's up to 2^32-1 should work (but at a slightly higher
!        runtime cost). */
!     uint32_t now = call TimerFrom.getNow();
!     int32_t min_remaining = (1UL << 31) - 1; /* max int32_t */
      bool min_remaining_isset = FALSE;
!     uint8_t num;
  
!     call TimerFrom.stop();
!     m_timers_changed = FALSE;
  
!     for (num=0; num<NUM_TIMERS; num++)
      {
        Timer_t* timer = &m_timers[num];
***************
*** 81,94 ****
        if (timer->isrunning)
        {
! 	// Calculate "remaining" before the timer is fired.  If a timer
! 	// restarts itself in a fired event, then we 1) need a consistent
! 	// "remaining" value to work with, and no worries because 2) all
! 	// start commands post executeTimersNow, so the timer will be
! 	// recomputed later, anyway.
! 
! 	uint32_t elapsed = then - timer->t0;
! 	int32_t remaining = timer->dt - elapsed;
  
! 	if (remaining <= 0)
  	{
  	  if (timer->isoneshot)
--- 80,86 ----
        if (timer->isrunning)
        {
! 	uint32_t elapsed = now - timer->t0;
  
! 	if (elapsed >= timer->dt)
  	{
  	  if (timer->isoneshot)
***************
*** 98,105 ****
  	  else
  	  {
! 	    // The remaining time is non-positive (the timer had fired).
! 	    // So add dt to convert it to remaining for the next event.
  	    timer->t0 += timer->dt;
! 	    remaining += timer->dt; 
  	  }
  
--- 90,97 ----
  	  else
  	  {
! 	    // Update timer for next event
  	    timer->t0 += timer->dt;
! 	    // Update elapsed so we compute the time of the next event
! 	    elapsed -= timer->dt;
  	  }
  
***************
*** 107,134 ****
  	}
  
! 	// check isrunning in case the timer was stopped in the fired
  	// event or this was a one shot timer; note that a one shot
  	// timer that was restarted in its fired event will push us
! 	// through here with remaining <= 0, but we're already scheduled
! 	// an executeTimersTask in that case (and suppressed setting
! 	// TimerFrom)
  	if (timer->isrunning)
  	{
  	  if (remaining < min_remaining)
! 	    min_remaining = remaining;
! 	  min_remaining_isset = TRUE;
  	}
        }
      }
  
!     if (!(state & S_TIMER_CHANGED) && min_remaining_isset)
      {
        if (min_remaining <= 0)
  	post executeTimersNow();
        else
! 	{
! 	  call TimerFrom.startOneShotAt(then, min_remaining);
! 	  state |= S_TIMER_RUNNING;
! 	}
      }
    }
--- 99,127 ----
  	}
  
! 	// Check isrunning in case the timer was stopped in the fired
  	// event or this was a one shot timer; note that a one shot
  	// timer that was restarted in its fired event will push us
! 	// through here with a value of remaining <= 0. But we're
! 	// already scheduled an executeTimersNow in that case (and
! 	// suppressed setting TimerFrom)
  	if (timer->isrunning)
  	{
+ 	  int32_t remaining = timer->dt - elapsed;
+ 
  	  if (remaining < min_remaining)
! 	    {
! 	      min_remaining = remaining;
! 	      min_remaining_isset = TRUE;
! 	    }
  	}
        }
      }
  
!     if (!m_timers_changed && min_remaining_isset)
      {
        if (min_remaining <= 0)
  	post executeTimersNow();
        else
! 	call TimerFrom.startOneShotAt(now, min_remaining);
      }
    }
***************
*** 137,165 ****
    event void TimerFrom.fired()
    {
!     executeTimers(call TimerFrom.gett0() + call TimerFrom.getdt());
    }
  
    task void executeTimersNow()
    {
!     /* If we set a timer, try and stop it. But if it believes it's
!        not running, that means that the execution of its fired event
!        is pending, so we:
!        - don't need to call executeTimers (it will happen soon)
!        - we must not call executeTimers, because that would change the
!          timer's setting, confusing the pending call to TimerFrom.fired
! 	 (it would call executeTimers with an incorrect "then"
! 	 parameter)
!        We need to use an atomic section even though it's a "timer",
!        because it's based on an underlying alarm, whose transitions
!        are asynchronous (yuck?)
!     */
!     if (state & S_TIMER_RUNNING)
!       atomic
! 	{
! 	  if (!call TimerFrom.isRunning())
! 	    return;
! 	  call TimerFrom.stop();
! 	}
!     executeTimers(call TimerFrom.getNow());
    }
  
--- 130,139 ----
    event void TimerFrom.fired()
    {
!     executeTimers();
    }
  
    task void executeTimersNow()
    {
!     executeTimers();
    }
  
***************
*** 171,175 ****
      timer->isoneshot = isoneshot;
      timer->isrunning = TRUE;
!     state |= S_TIMER_CHANGED;
      post executeTimersNow();
    }
--- 145,149 ----
      timer->isoneshot = isoneshot;
      timer->isrunning = TRUE;
!     m_timers_changed = TRUE;
      post executeTimersNow();
    }



More information about the Tinyos-2-commits mailing list