sgpemv2/src/simulation.cc
tchernobog 56534edb6c - Make libpyloader a loadable plugin (warning: won't work
on Window$ unless you change the string "libpyloader" to
"pyloader" into main.cc)
- Fix Makefile to support module creation and loading
- 2DO: 
	- Add a class into backend to load and manage plugins
	- Install plugins into separate directory
	- Remove hardcoded paths


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@458 3ecf2c5c-341e-0410-92b4-d18e462d057c
2006-02-25 12:21:30 +00:00

182 lines
3.5 KiB
C++

// src/backend/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 "simulation.hh"
using namespace std;
using namespace sgpem;
using namespace memory;
using Glib::usleep;
Simulation::Simulation(): _state(state_paused), _mode(true), _timer_interval(1000)
{
}
void
Simulation::set_timer(const int& t)
{
_timer_interval = t;
}
int
Simulation::get_timer() const
{
return _timer_interval;
}
void
Simulation::set_mode(const bool& b)
{
_mode = b;
}
bool
Simulation::get_mode() const
{
return _mode;
}
void
Simulation::pause()
{
_state = state_paused;
}
void
Simulation::stop()
{
_state = state_stopped;
}
void
Simulation::reset()
{
_state = state_paused;
History::get_instance().truncate_at(0);
}
void
Simulation::run()
{
History& h = History::get_instance();
if (_state == state_stopped)
h.truncate_at(0);
_state = state_running;
//******* CONTINUOUS TIME
if (_mode)
{
loop:
// chech for termination
bool all_term = true;
smart_ptr<SchedulableList> left = h.get_simulation_status_at(h.get_current_time());
for(uint i = 0; i < left->size(); i++)
if (left->get_item_at(i)->get_state() != SchedulableStatus::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_paused;
return;
}
//step forward
Scheduler::get_instance().step_forward();
//sleep
Glib::usleep(_timer_interval*1000);
//check the state
if (_state == state_stopped || _state == state_paused)
return;
goto loop;
}
//******* STEP by STEP
else
{
// chech for termination
bool all_term = true;
smart_ptr<SchedulableList> left = h.get_simulation_status_at(h.get_current_time());
for(uint i = 0; i < left->size(); i++)
if (left->get_item_at(i)->get_state() != SchedulableStatus::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
//step forward
Scheduler::get_instance().step_forward();
}
}
void
Simulation::jump_to(const uint& where)
{
//jump to position 0
reset();
bool old = _mode;
_mode = false;
// executes "where" steps
for (uint i=0; i < where; i++)
run();
_state = state_paused;
_mode = old;
}
/*void
Simulation::set_policy(Policy* p)
{
Scheduler::get_instance().set_policy(p);
}*/
Policy*
Simulation::get_policy()
{
return &Scheduler::get_instance().get_policy();
}
vector<Policy*>
Simulation::get_avaiable_policies()
{
vector<Policy*> v;
v.push_back(&Scheduler::get_instance().get_policy());
return v;
}