- Move code to manage step_by_step or continuous simulation from
ConcreteSimulation to TextSimulation git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@946 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
parent
1087d45628
commit
645156e62c
7 changed files with 73 additions and 130 deletions
|
@ -22,7 +22,6 @@
|
|||
#include "simulation_observer.hh"
|
||||
#include "scheduler.hh"
|
||||
#include "cpu_policies_gatekeeper.hh"
|
||||
#include <glibmm/timer.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
@ -36,34 +35,21 @@
|
|||
using namespace std;
|
||||
using namespace sgpem;
|
||||
using namespace memory;
|
||||
using Glib::usleep;
|
||||
|
||||
ConcreteSimulation::ConcreteSimulation() :
|
||||
_state(state_stopped), _mode(true), _timer_interval(1000), _policy(NULL)
|
||||
_state(state_stopped), _mode(mode_continuous), _policy(NULL)
|
||||
{
|
||||
_notify = false;
|
||||
_front = 0;
|
||||
}
|
||||
|
||||
void
|
||||
ConcreteSimulation::set_timer(unsigned int t)
|
||||
ConcreteSimulation::set_mode(const mode new_mode)
|
||||
{
|
||||
_timer_interval = t;
|
||||
_mode = new_mode;
|
||||
}
|
||||
|
||||
int
|
||||
ConcreteSimulation::get_timer() const
|
||||
{
|
||||
return _timer_interval;
|
||||
}
|
||||
|
||||
void
|
||||
ConcreteSimulation::set_mode(const bool& b)
|
||||
{
|
||||
_mode = b;
|
||||
}
|
||||
|
||||
bool
|
||||
Simulation::mode
|
||||
ConcreteSimulation::get_mode() const
|
||||
{
|
||||
return _mode;
|
||||
|
@ -72,7 +58,6 @@ ConcreteSimulation::get_mode() const
|
|||
void
|
||||
ConcreteSimulation::jump_to(History::position p)
|
||||
{
|
||||
|
||||
switch (_state)
|
||||
{
|
||||
case state_running:
|
||||
|
@ -88,26 +73,36 @@ ConcreteSimulation::jump_to(History::position p)
|
|||
|
||||
pause();
|
||||
|
||||
// Disable updates to registered observers
|
||||
_history.set_notify_enabled(false);
|
||||
|
||||
bool yet_to_finish = true;
|
||||
while (yet_to_finish && p > _front)
|
||||
yet_to_finish = step();
|
||||
|
||||
if (!yet_to_finish)
|
||||
stop();
|
||||
_front = p < _front ? p : _front;
|
||||
notify_change();
|
||||
_front = std::min(p, _front);
|
||||
|
||||
// Reenables updates to registered observers.
|
||||
// Calls _history.notify_change() on reactivation.
|
||||
_history.set_notify_enabled(true);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ConcreteSimulation::pause()
|
||||
{
|
||||
if(_state != state_paused)
|
||||
notify_change();
|
||||
_state = state_paused;
|
||||
}
|
||||
|
||||
void
|
||||
ConcreteSimulation::stop()
|
||||
{
|
||||
if(_state != state_stopped)
|
||||
notify_change();
|
||||
_state = state_stopped;
|
||||
}
|
||||
|
||||
|
@ -128,66 +123,12 @@ ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, Mal
|
|||
|
||||
_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 = true;
|
||||
if (_front == get_history().get_size() - 1)
|
||||
yet_to_finish = Scheduler::get_instance().step_forward(_history, *get_policy());
|
||||
if (!yet_to_finish) stop();
|
||||
_front++;
|
||||
notify_change();
|
||||
|
||||
//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
|
||||
//step forward
|
||||
bool yet_to_finish = step();
|
||||
if (yet_to_finish)
|
||||
pause();
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
assert(get_policy() != NULL);
|
||||
//step forward
|
||||
bool yet_to_finish = true;
|
||||
if (_front == get_history().get_size() - 1)
|
||||
yet_to_finish = Scheduler::get_instance().step_forward(_history, *get_policy());
|
||||
_front++;
|
||||
notify_change();
|
||||
if (yet_to_finish)
|
||||
pause();
|
||||
else
|
||||
stop();
|
||||
}
|
||||
catch (const CPUPolicyException& e)
|
||||
{
|
||||
stop();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
stop();
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -195,14 +136,13 @@ ConcreteSimulation::step()
|
|||
throw(UserInterruptException, NullPolicyException, MalformedPolicyException)
|
||||
{
|
||||
if (get_policy() == NULL)
|
||||
{
|
||||
stop();
|
||||
throw NullPolicyException("no policy selected");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
assert(get_policy() != NULL);
|
||||
stop();
|
||||
throw NullPolicyException("no policy selected");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
//step forward
|
||||
bool yet_to_finish = true;
|
||||
if (_front == get_history().get_size() - 1)
|
||||
|
@ -210,7 +150,7 @@ ConcreteSimulation::step()
|
|||
_front++;
|
||||
return yet_to_finish;
|
||||
}
|
||||
catch (const CPUPolicyException& e)
|
||||
catch (const CPUPolicyException& e)
|
||||
{
|
||||
stop();
|
||||
throw;
|
||||
|
|
|
@ -50,13 +50,9 @@ namespace sgpem
|
|||
|
||||
bool step() throw(UserInterruptException, NullPolicyException, MalformedPolicyException);
|
||||
|
||||
void set_timer(const unsigned int);
|
||||
void set_mode(mode new_mode);
|
||||
|
||||
int get_timer() const;
|
||||
|
||||
void set_mode(const bool&);
|
||||
|
||||
bool get_mode() const;
|
||||
mode get_mode() const;
|
||||
|
||||
state get_state() const;
|
||||
|
||||
|
@ -74,8 +70,7 @@ namespace sgpem
|
|||
|
||||
private:
|
||||
state _state;
|
||||
bool _mode;
|
||||
int _timer_interval;
|
||||
mode _mode;
|
||||
ConcreteHistory _history;
|
||||
CPUPolicy* _policy;
|
||||
};
|
||||
|
|
|
@ -72,6 +72,12 @@ namespace sgpem
|
|||
state_stopped
|
||||
};
|
||||
|
||||
enum mode
|
||||
{
|
||||
mode_step_by_step = 0,
|
||||
mode_continuous = 1
|
||||
};
|
||||
|
||||
virtual ~Simulation();
|
||||
|
||||
/**
|
||||
|
@ -109,21 +115,6 @@ namespace sgpem
|
|||
virtual void jump_to(History::position p) = 0;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
\brief Setter for the attribute timer_interval.
|
||||
|
||||
This method is used to define how a single time unit is to be
|
||||
interpreted when the simulation advancement mode is continue.
|
||||
The input value is in milliseconds, and it must be in range [0, 10000].
|
||||
*/
|
||||
virtual void set_timer(unsigned int) = 0;
|
||||
|
||||
/**
|
||||
\see set_timer()
|
||||
*/
|
||||
virtual int get_timer() const = 0;
|
||||
|
||||
/**
|
||||
\brief This methods allows to change the way the simulation progresses.
|
||||
|
||||
|
@ -133,13 +124,13 @@ namespace sgpem
|
|||
waiting the time defined with set_timer() between each step, until all
|
||||
processes have terminated, or some error happens.
|
||||
*/
|
||||
virtual void set_mode(const bool&) = 0;
|
||||
virtual void set_mode(const mode) = 0;
|
||||
|
||||
/**
|
||||
\return The simulation advancement mode: 0 if step-to-step, 1 if
|
||||
continue.
|
||||
*/
|
||||
virtual bool get_mode() const = 0;
|
||||
virtual mode get_mode() const = 0;
|
||||
|
||||
virtual state get_state() const = 0;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue