Files
unison/src/core/model/timer.cc

211 lines
3.8 KiB
C++
Raw Normal View History

2007-10-08 15:50:25 +02:00
/*
* Copyright (c) 2007 INRIA
2007-10-08 15:50:25 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
2007-09-27 12:51:17 +02:00
#include "timer.h"
2022-10-07 20:08:35 +00:00
#include "log.h"
2022-10-07 20:08:35 +00:00
#include "simulation-singleton.h"
#include "simulator.h"
2014-12-18 15:12:35 -08:00
/**
* \file
* \ingroup timer
* ns3::Timer implementation.
*/
2022-10-07 20:08:35 +00:00
namespace ns3
{
2007-09-27 12:51:17 +02:00
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("Timer");
2022-10-07 20:08:35 +00:00
Timer::Timer()
: m_flags(CHECK_ON_DESTROY),
m_delay(FemtoSeconds(0)),
m_event(),
m_impl(nullptr)
2010-04-08 13:39:07 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
2010-04-08 13:39:07 +02:00
}
2007-09-27 12:51:17 +02:00
2022-10-07 20:08:35 +00:00
Timer::Timer(enum DestroyPolicy destroyPolicy)
: m_flags(destroyPolicy),
m_delay(FemtoSeconds(0)),
m_event(),
m_impl(nullptr)
2010-04-08 13:39:07 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this << destroyPolicy);
2010-04-08 13:39:07 +02:00
}
2007-09-27 12:51:17 +02:00
2022-10-07 20:08:35 +00:00
Timer::~Timer()
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
if (m_flags & CHECK_ON_DESTROY)
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
if (m_event.IsRunning())
2010-04-08 13:39:07 +02:00
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("Event is still running while destroying.");
2010-04-08 13:39:07 +02:00
}
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
else if (m_flags & CANCEL_ON_DESTROY)
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
m_event.Cancel();
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
else if (m_flags & REMOVE_ON_DESTROY)
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
m_event.Remove();
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
delete m_impl;
2007-09-27 12:51:17 +02:00
}
2010-04-08 13:39:07 +02:00
void
2022-10-07 20:08:35 +00:00
Timer::SetDelay(const Time& time)
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this << time);
m_delay = time;
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
2010-04-08 13:39:07 +02:00
Time
2022-10-07 20:08:35 +00:00
Timer::GetDelay() const
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
return m_delay;
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
2007-10-04 09:22:05 +02:00
Time
2022-10-07 20:08:35 +00:00
Timer::GetDelayLeft() const
2007-10-04 09:22:05 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
switch (GetState())
2010-04-08 13:39:07 +02:00
{
case Timer::RUNNING:
2022-10-07 20:08:35 +00:00
return Simulator::GetDelayLeft(m_event);
break;
2010-04-08 13:39:07 +02:00
case Timer::EXPIRED:
2022-10-07 20:08:35 +00:00
return TimeStep(0);
break;
2010-04-08 13:39:07 +02:00
case Timer::SUSPENDED:
2022-10-07 20:08:35 +00:00
return m_delayLeft;
break;
2010-04-08 13:39:07 +02:00
default:
2022-10-07 20:08:35 +00:00
NS_ASSERT(false);
return TimeStep(0);
break;
2010-04-08 13:39:07 +02:00
}
2007-10-04 09:22:05 +02:00
}
2007-09-27 12:51:17 +02:00
2010-04-08 13:39:07 +02:00
void
2022-10-07 20:08:35 +00:00
Timer::Cancel()
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
m_event.Cancel();
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
2010-04-08 13:39:07 +02:00
void
2022-10-07 20:08:35 +00:00
Timer::Remove()
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
m_event.Remove();
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
2010-04-08 13:39:07 +02:00
bool
2022-10-07 20:08:35 +00:00
Timer::IsExpired() const
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
return !IsSuspended() && m_event.IsExpired();
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
2010-04-08 13:39:07 +02:00
bool
2022-10-07 20:08:35 +00:00
Timer::IsRunning() const
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
return !IsSuspended() && m_event.IsRunning();
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
2007-10-03 13:01:29 +02:00
bool
2022-10-07 20:08:35 +00:00
Timer::IsSuspended() const
2007-10-03 13:01:29 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
return (m_flags & TIMER_SUSPENDED) == TIMER_SUSPENDED;
2007-10-03 13:01:29 +02:00
}
2022-10-07 20:08:35 +00:00
2010-04-08 13:39:07 +02:00
enum Timer::State
2022-10-07 20:08:35 +00:00
Timer::GetState() const
2007-10-04 09:14:16 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
if (IsRunning())
2007-10-04 09:14:16 +02:00
{
2022-10-07 20:08:35 +00:00
return Timer::RUNNING;
2007-10-04 09:14:16 +02:00
}
2022-10-07 20:08:35 +00:00
else if (IsExpired())
2007-10-04 09:14:16 +02:00
{
2022-10-07 20:08:35 +00:00
return Timer::EXPIRED;
2007-10-04 09:14:16 +02:00
}
2022-10-07 20:08:35 +00:00
else
2007-10-04 09:14:16 +02:00
{
2022-10-07 20:08:35 +00:00
NS_ASSERT(IsSuspended());
return Timer::SUSPENDED;
2007-10-04 09:14:16 +02:00
}
}
2007-09-27 12:51:17 +02:00
2010-04-08 13:39:07 +02:00
void
2022-10-07 20:08:35 +00:00
Timer::Schedule()
2007-09-28 16:14:16 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
Schedule(m_delay);
2007-09-28 16:14:16 +02:00
}
2010-04-08 13:39:07 +02:00
void
2022-10-07 20:08:35 +00:00
Timer::Schedule(Time delay)
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this << delay);
NS_ASSERT(m_impl != nullptr);
if (m_event.IsRunning())
2007-09-27 12:51:17 +02:00
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("Event is still running while re-scheduling.");
2007-09-27 12:51:17 +02:00
}
2022-10-07 20:08:35 +00:00
m_event = m_impl->Schedule(delay);
2007-09-27 12:51:17 +02:00
}
2007-10-03 13:01:29 +02:00
void
2022-10-07 20:08:35 +00:00
Timer::Suspend()
2007-10-03 13:01:29 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
NS_ASSERT(IsRunning());
m_delayLeft = Simulator::GetDelayLeft(m_event);
if (m_flags & CANCEL_ON_DESTROY)
{
2022-10-07 20:08:35 +00:00
m_event.Cancel();
}
2022-10-07 20:08:35 +00:00
else if (m_flags & REMOVE_ON_DESTROY)
{
2022-10-07 20:08:35 +00:00
m_event.Remove();
}
2022-10-07 20:08:35 +00:00
m_flags |= TIMER_SUSPENDED;
2007-10-03 13:01:29 +02:00
}
void
2022-10-07 20:08:35 +00:00
Timer::Resume()
2007-10-03 13:01:29 +02:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
NS_ASSERT(m_flags & TIMER_SUSPENDED);
m_event = m_impl->Schedule(m_delayLeft);
m_flags &= ~TIMER_SUSPENDED;
2007-10-03 13:01:29 +02:00
}
2007-09-27 12:51:17 +02:00
} // namespace ns3