sgpemv2/src/backend/concrete_simulation.cc
elvez 17ca8156d9 - Completed policy-related error handling code
git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@839 3ecf2c5c-341e-0410-92b4-d18e462d057c
2006-08-10 22:59:00 +00:00

171 lines
3.2 KiB
C++

// src/backend/concrete_simulation.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "concrete_simulation.hh"
#include "scheduler.hh"
#include "cpu_policies_gatekeeper.hh"
#include <glibmm/timer.h>
#include <cassert>
#include "smartp.tcc"
using namespace std;
using namespace sgpem;
using namespace memory;
using Glib::usleep;
ConcreteSimulation::ConcreteSimulation() :
_state(state_stopped), _mode(true), _timer_interval(1000), _policy(NULL)
{}
void
ConcreteSimulation::set_timer(unsigned int t)
{
_timer_interval = t;
}
int
ConcreteSimulation::get_timer() const
{
return _timer_interval;
}
void
ConcreteSimulation::set_mode(const bool& b)
{
_mode = b;
}
bool
ConcreteSimulation::get_mode() const
{
return _mode;
}
void
ConcreteSimulation::pause()
{
_state = state_paused;
}
void
ConcreteSimulation::stop()
{
_state = state_stopped;
}
void
ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, MalformedPolicyException)
{
switch (_state)
{
case state_running:
return;
case state_stopped:
_history.reset(true);
break;
default:
;
}
_state = state_running;
if (get_policy() == NULL)
{
stop();
throw NullPolicyException("no policy selected");
}
//******* CONTINUOUS TIME
if (_mode)
{
do
{
try
{
//step forward
bool yet_to_finish = Scheduler::get_instance().step_forward(_history, *get_policy());
if (!yet_to_finish) stop();
//sleep
Glib::usleep(_timer_interval*1000);
}
catch (const CPUPolicyException& e)
{
stop();
throw;
}
//check the state
if (_state == state_stopped || _state == state_paused)
return;
}
while (true);
}
//******* STEP by STEP
else
{
try
{
assert(get_policy() != NULL);
//step forward
bool yet_to_finish = Scheduler::get_instance().step_forward(_history, *get_policy());
if (yet_to_finish)
pause();
else
stop();
}
catch (const CPUPolicyException& e)
{
stop();
throw;
}
}
}
Simulation::state
ConcreteSimulation::get_state() const
{
return _state;
}
ConcreteHistory&
ConcreteSimulation::get_history()
{
return _history;
}
void
ConcreteSimulation::set_policy(CPUPolicy* p)
{
_policy = p;
CPUPoliciesGatekeeper::get_instance().activate_policy(&_history, p);
}
CPUPolicy*
ConcreteSimulation::get_policy()
{
return _policy;
}