- Moved Simulation and ConcreteSimulation to the backend
> - Completed ConcreteSimulation, i think it is all we need at the moment... git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@773 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
parent
cab84a0e3d
commit
a092f3dc7b
7 changed files with 196 additions and 225 deletions
188
src/backend/concrete_simulation.cc
Normal file
188
src/backend/concrete_simulation.cc
Normal file
|
@ -0,0 +1,188 @@
|
|||
// 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 "policies_gatekeeper.hh"
|
||||
#include <glibmm/timer.h>
|
||||
|
||||
#include "smartp.tcc"
|
||||
|
||||
using namespace std;
|
||||
using namespace sgpem;
|
||||
using namespace memory;
|
||||
using Glib::usleep;
|
||||
|
||||
ConcreteSimulation::ConcreteSimulation() :
|
||||
_state(state_paused), _mode(true), _timer_interval(1000), _policy(NULL)
|
||||
{}
|
||||
|
||||
void
|
||||
ConcreteSimulation::set_timer(const 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)
|
||||
{
|
||||
switch(_state)
|
||||
{
|
||||
case state_running:
|
||||
return;
|
||||
case state_stopped:
|
||||
_history.reset(true);
|
||||
break;
|
||||
}
|
||||
|
||||
_state = state_running;
|
||||
|
||||
|
||||
// chech for termination
|
||||
const Environment &env = _history.get_last_environment();
|
||||
const Environment::Processes& processes = env.get_processes();
|
||||
typedef Environment::Processes::const_iterator ProcessesIt;
|
||||
|
||||
//******* CONTINUOUS TIME
|
||||
|
||||
if (_mode)
|
||||
{
|
||||
do
|
||||
{
|
||||
|
||||
bool all_term = true;
|
||||
for(ProcessesIt it = processes.begin(); it != processes.end(); ++it)
|
||||
if((*it)->get_state() != Schedulable::state_terminated)
|
||||
{
|
||||
all_term = false;
|
||||
break;
|
||||
}
|
||||
|
||||
//if there are no processes left the termination message has already been notified
|
||||
//by the last execution of upadate()
|
||||
if (all_term)
|
||||
{
|
||||
_state = state_stopped;
|
||||
return; // Exit from loop
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
assert(get_policy() != NULL);
|
||||
//step forward
|
||||
Scheduler::get_instance().step_forward(_history, *get_policy());
|
||||
|
||||
//sleep
|
||||
Glib::usleep(_timer_interval*1000);
|
||||
|
||||
}
|
||||
catch(UserInterruptException e)
|
||||
{
|
||||
stop();
|
||||
throw;
|
||||
}
|
||||
|
||||
//check the state
|
||||
if (_state == state_stopped || _state == state_paused)
|
||||
return;
|
||||
|
||||
}
|
||||
while(true);
|
||||
}
|
||||
|
||||
//******* STEP by STEP
|
||||
else
|
||||
{
|
||||
bool all_term = true;
|
||||
for(ProcessesIt it = processes.begin(); it != processes.end(); ++it)
|
||||
if((*it)->get_state() != Schedulable::state_terminated)
|
||||
{
|
||||
all_term = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (all_term)
|
||||
//if there are no processes left the termination message has already been notified
|
||||
//by the last execution of upadate()
|
||||
_state = state_paused;
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
assert(get_policy() != NULL);
|
||||
//step forward
|
||||
Scheduler::get_instance().step_forward(_history, *get_policy());
|
||||
}
|
||||
catch(UserInterruptException e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
ConcreteSimulation::set_policy(Policy* p)
|
||||
{
|
||||
_policy = p;
|
||||
|
||||
if(p != NULL)
|
||||
PoliciesGatekeeper::get_instance().activate_policy(&_history, p);
|
||||
}
|
||||
|
||||
|
||||
Policy*
|
||||
ConcreteSimulation::get_policy()
|
||||
{
|
||||
return _policy;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue