- Added ConcreteSimulation, but not coded it`s behaviour

- Made Simulation a singleton

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@771 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-07-15 17:35:24 +00:00
parent 1506c46287
commit 4ab7123ced
7 changed files with 316 additions and 231 deletions

View File

@ -265,6 +265,7 @@ sgpemv2_LDADD = \
# Please keep this in sorted order: # Please keep this in sorted order:
sgpemv2_SOURCES = \ sgpemv2_SOURCES = \
src/concrete_simulation.cc \
src/graphical_terminal_io.cc \ src/graphical_terminal_io.cc \
src/gui_builder.cc \ src/gui_builder.cc \
src/main.cc \ src/main.cc \
@ -277,6 +278,7 @@ sgpemv2_SOURCES = \
src/text_simulation.cc src/text_simulation.cc
noinst_HEADERS += \ noinst_HEADERS += \
src/concrete_simulation.hh \
src/graphical_simulation.hh \ src/graphical_simulation.hh \
src/graphical_terminal_io.hh \ src/graphical_terminal_io.hh \
src/gui_builder.hh \ src/gui_builder.hh \

199
src/concrete_simulation.cc Normal file
View File

@ -0,0 +1,199 @@
// 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 <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)
{}
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::reset()
{
_state = state_paused;
//History::get_instance().truncate_at(0);
}
void
ConcreteSimulation::run() throw(UserInterruptException)
{
// History& h = History::get_instance();
//
// switch(_state)
// {
// case state_running:
// // FIXME: write out something, or just ignore user input?
// return;
// case state_stopped:
// h.truncate_at(0);
// break;
// default:
// break;
// }
//
// _state = state_running;
//
// //******* CONTINUOUS TIME
//
// if (_mode)
// {
// do
// {
// // chech for termination
// bool all_term = true;
// smart_ptr<ReadyQueue> 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() != DynamicSchedulable::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
// {
// //step forward
// Scheduler::get_instance().step_forward();
//
// //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
// {
// // chech for termination
// bool all_term = true;
// smart_ptr<ReadyQueue> 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() != DynamicSchedulable::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
// {
// //step forward
// Scheduler::get_instance().step_forward();
// }
// catch(UserInterruptException e)
// {
// throw;
// }
// }
// }
}
void
ConcreteSimulation::set_policy(Policy* p)
{
// Scheduler::get_instance().set_policy(p);
}
Policy*
ConcreteSimulation::get_policy()
{
//return &Scheduler::get_instance().get_policy();
return NULL;
}
vector<Policy*>
ConcreteSimulation::get_avaiable_policies()
{
vector<Policy*> v;
//v.push_back(&Scheduler::get_instance().get_policy());
return v;
}

View File

@ -0,0 +1,69 @@
// src/frontend/concrete_simulation.hh - 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
#ifndef CONCRETE_SIMULATION_HH
#define CONCRETE_SIMULATION_HH 1
#include "simulation.hh"
#include "backend/concrete_history.hh"
namespace sgpem
{
class ConcreteSimulation;
class ConcreteSimulation : public Simulation
{
public:
ConcreteSimulation();
void run() throw(UserInterruptException);
void pause();
void stop();
void reset();
void set_timer(const int&);
int get_timer() const;
void set_mode(const bool&);
bool get_mode() const;
void set_policy(Policy*);
Policy* get_policy();
std::vector<Policy*> get_avaiable_policies();
private:
state _state;
bool _mode;
int _timer_interval;
ConcreteHistory _history;
};
}
#endif

View File

@ -19,204 +19,22 @@
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "simulation.hh" #include "simulation.hh"
#include "concrete_simulation.hh"
#include "smartp.tcc" // Do not include in header file:
#include "singleton.tcc"
using namespace std;
using namespace sgpem; using namespace sgpem;
using namespace memory;
using Glib::usleep;
Simulation::Simulation(): _state(state_paused), _mode(true), _timer_interval(1000) // Explicit template instantiation to allow to export symbols from the DSO.
{} template class SG_DLLEXPORT Singleton<ConcreteSimulation>;
void
Simulation::set_timer(const int& t) Simulation::~Simulation()
{ {
_timer_interval = t;
} }
int Simulation&
Simulation::get_timer() const Simulation::get_instance()
{ {
return _timer_interval; return Singleton<ConcreteSimulation>::get_instance();
}
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() throw(UserInterruptException)
{
// History& h = History::get_instance();
//
// switch(_state)
// {
// case state_running:
// // FIXME: write out something, or just ignore user input?
// return;
// case state_stopped:
// h.truncate_at(0);
// break;
// default:
// break;
// }
//
// _state = state_running;
//
// //******* CONTINUOUS TIME
//
// if (_mode)
// {
// do
// {
// // chech for termination
// bool all_term = true;
// smart_ptr<ReadyQueue> 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() != DynamicSchedulable::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
// {
// //step forward
// Scheduler::get_instance().step_forward();
//
// //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
// {
// // chech for termination
// bool all_term = true;
// smart_ptr<ReadyQueue> 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() != DynamicSchedulable::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
// {
// //step forward
// Scheduler::get_instance().step_forward();
// }
// catch(UserInterruptException e)
// {
// throw;
// }
// }
// }
}
void
Simulation::jump_to(const uint& where) throw(UserInterruptException)
{
//jump to position 0
reset();
bool old = _mode;
_mode = false;
try
{
// executes "where" steps
for (uint i = 0; i < where; i++)
run();
}
catch(UserInterruptException e)
{
_mode = old;
throw;
}
_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();
return NULL;
}
vector<Policy*>
Simulation::get_avaiable_policies()
{
vector<Policy*> v;
//v.push_back(&Scheduler::get_instance().get_policy());
return v;
} }

View File

@ -21,14 +21,17 @@
#ifndef SIMULATION_HH #ifndef SIMULATION_HH
#define SIMULATION_HH 1 #define SIMULATION_HH 1
namespace sgpem
{
class ConcreteSimulation;
class Policy;
}
#include "config.h" #include "config.h"
#include <glibmm/timer.h> #include "singleton.hh"
#include "backend/user_interrupt_exception.hh"
#include "observer.hh" #include <vector>
#include "backend/policy.hh"
#include "backend/history.hh"
#include "backend/scheduler.hh"
namespace sgpem namespace sgpem
{ {
@ -54,7 +57,7 @@ namespace sgpem
the Backend layers. the Backend layers.
*/ */
class SG_DLLEXPORT Simulation : public Observer class SG_DLLEXPORT Simulation : public Singleton<ConcreteSimulation>
{ {
public: public:
enum state enum state
@ -64,7 +67,7 @@ namespace sgpem
state_stopped state_stopped
}; };
Simulation(); virtual ~Simulation();
/** /**
\brief Runs the simulation. \brief Runs the simulation.
@ -72,7 +75,7 @@ namespace sgpem
Advances the simulation by one or more steps, depending on the Advances the simulation by one or more steps, depending on the
actual state and on the value set with set_mode(). actual state and on the value set with set_mode().
*/ */
void run() throw(UserInterruptException); virtual void run() throw(UserInterruptException) = 0;
/** /**
\brief Pauses a running simulation. \brief Pauses a running simulation.
@ -81,7 +84,7 @@ namespace sgpem
Calling again run() will cause the simulation to start from the current Calling again run() will cause the simulation to start from the current
simulation step. simulation step.
*/ */
void pause(); virtual void pause() = 0;
/** /**
\brief Stops the simulation. \brief Stops the simulation.
@ -89,7 +92,7 @@ namespace sgpem
Behaves in the same way as pause(), except that the next call to run() Behaves in the same way as pause(), except that the next call to run()
will cause the simulation to start from the beginning. will cause the simulation to start from the beginning.
*/ */
void stop(); virtual void stop() = 0;
/** /**
\brief Reset the simulation. \brief Reset the simulation.
@ -98,12 +101,7 @@ namespace sgpem
residual or temporary data to ensure the simulation has reached a residual or temporary data to ensure the simulation has reached a
clean and stable state. clean and stable state.
*/ */
void reset(); virtual void reset() = 0;
/**
\brief Causes the simulation to jump to the given time unit.
*/
void jump_to(const uint&) throw(UserInterruptException);
/** /**
\brief Setter for the attribute timer_interval. \brief Setter for the attribute timer_interval.
@ -112,12 +110,12 @@ namespace sgpem
interpreted when the simulation advancement mode is continue. interpreted when the simulation advancement mode is continue.
The input value is in milliseconds, and it must be in range [0, 10000]. The input value is in milliseconds, and it must be in range [0, 10000].
*/ */
void set_timer(const int&); virtual void set_timer(const int&) = 0;
/** /**
\see set_timer() \see set_timer()
*/ */
int get_timer() const; virtual int get_timer() const = 0;
/** /**
\brief This methods allows to change the way the simulation progresses. \brief This methods allows to change the way the simulation progresses.
@ -128,38 +126,37 @@ namespace sgpem
waiting the time defined with set_timer() between each step, until all waiting the time defined with set_timer() between each step, until all
processes have terminated, or some error happens. processes have terminated, or some error happens.
*/ */
void set_mode(const bool&); virtual void set_mode(const bool&) = 0;
/** /**
\return The simulation advancement mode: 0 if step-to-step, 1 if \return The simulation advancement mode: 0 if step-to-step, 1 if
continue. continue.
*/ */
bool get_mode() const; virtual bool get_mode() const = 0;
/** /**
\brief Setup the policy to be used by the system. \brief Setup the policy to be used by the system.
The input pointer must be one of those returned by get_avaiable_policies(). The input pointer must be one of those returned by get_avaiable_policies().
*/ */
void set_policy(Policy*); virtual void set_policy(Policy*) = 0;
/** /**
\return The policy currently in use. \return The policy currently in use.
*/ */
Policy* get_policy(); virtual Policy* get_policy() = 0;
/** /**
\return A collection of policies (scheduling algorithms), from which a user \return A collection of policies (scheduling algorithms), from which a user
may choose. may choose.
*/ */
std::vector<Policy*> get_avaiable_policies(); virtual std::vector<Policy*> get_avaiable_policies() = 0;
private:
state _state;
bool _mode;
int _timer_interval;
/**
* Small kludge to avoid the need for declaration of ConcreteSimulation
* by the calling code of Simulation::get_instance()
*/
static Simulation& get_instance();
}; };
} }

View File

@ -320,7 +320,7 @@ TextSimulation::on_run(const Tokens& arguments)
try try
{ {
run(); Simulation::get_instance().run();
} }
catch(UserInterruptException e) catch(UserInterruptException e)
{ {
@ -335,7 +335,7 @@ TextSimulation::on_pause(const Tokens& arguments)
{ {
check_arguments_num(arguments, 0); check_arguments_num(arguments, 0);
pause(); Simulation::get_instance().pause();
} }
void void
@ -343,7 +343,7 @@ TextSimulation::on_stop(const Tokens& arguments)
{ {
check_arguments_num(arguments, 0); check_arguments_num(arguments, 0);
stop(); Simulation::get_instance().stop();
} }
void void
@ -642,7 +642,7 @@ TextSimulation::on_get(const Tokens& arguments)
if(attr == "SIMULATION_TICK") if(attr == "SIMULATION_TICK")
{ {
ostringstream oss; ostringstream oss;
oss << "\nsimulation_tick = " << get_timer() << "ms" << endl; oss << "\nsimulation_tick = " << Simulation::get_instance().get_timer() << "ms" << endl;
p_stdout(oss.str()); p_stdout(oss.str());
} }
else else
@ -672,7 +672,7 @@ TextSimulation::on_set(const Tokens& arguments)
{ {
try try
{ {
set_timer(string_to<int>(value)); Simulation::get_instance().set_timer(string_to<int>(value));
} }
catch(domain_error e) catch(domain_error e)
{ {

View File

@ -55,7 +55,7 @@ namespace sgpem
Any object returned after the call to Simulation will be returned to the output Any object returned after the call to Simulation will be returned to the output
devices(s) in a human-readable format. devices(s) in a human-readable format.
*/ */
class SG_DLLEXPORT TextSimulation : public Simulation, public sigc::trackable class SG_DLLEXPORT TextSimulation : public sigc::trackable
{ {
public: public:
~TextSimulation(); ~TextSimulation();