- Added full-featured jumpto command.
- Minor fixes on the gui. git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@901 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
parent
d8694b2f5b
commit
df4b32f1ba
9 changed files with 339 additions and 68 deletions
|
@ -19,6 +19,7 @@
|
|||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
#include "concrete_simulation.hh"
|
||||
#include "simulation_observer.hh"
|
||||
#include "scheduler.hh"
|
||||
#include "cpu_policies_gatekeeper.hh"
|
||||
#include <glibmm/timer.h>
|
||||
|
@ -27,6 +28,11 @@
|
|||
|
||||
#include "smartp.tcc"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace sgpem;
|
||||
using namespace memory;
|
||||
|
@ -34,7 +40,10 @@ using Glib::usleep;
|
|||
|
||||
ConcreteSimulation::ConcreteSimulation() :
|
||||
_state(state_stopped), _mode(true), _timer_interval(1000), _policy(NULL)
|
||||
{}
|
||||
{
|
||||
_notify = false;
|
||||
_front = 0;
|
||||
}
|
||||
|
||||
void
|
||||
ConcreteSimulation::set_timer(unsigned int t)
|
||||
|
@ -60,6 +69,37 @@ ConcreteSimulation::get_mode() const
|
|||
return _mode;
|
||||
}
|
||||
|
||||
void
|
||||
ConcreteSimulation::jump_to(History::position p)
|
||||
{
|
||||
|
||||
switch (_state)
|
||||
{
|
||||
case state_running:
|
||||
pause();
|
||||
break;
|
||||
case state_stopped:
|
||||
_history.reset(true);
|
||||
_front = 0;
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
pause();
|
||||
|
||||
bool yet_to_finish = true;
|
||||
while (yet_to_finish && p > _front)
|
||||
yet_to_finish = step();
|
||||
|
||||
if (!yet_to_finish)
|
||||
stop();
|
||||
_history.get_size() << std::endl;
|
||||
_front = p < _front ? p : _front;
|
||||
notify_change();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ConcreteSimulation::pause()
|
||||
{
|
||||
|
@ -81,6 +121,7 @@ ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, Mal
|
|||
return;
|
||||
case state_stopped:
|
||||
_history.reset(true);
|
||||
_front = 0;
|
||||
break;
|
||||
default:
|
||||
;
|
||||
|
@ -102,8 +143,12 @@ ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, Mal
|
|||
try
|
||||
{
|
||||
//step forward
|
||||
bool yet_to_finish = Scheduler::get_instance().step_forward(_history, *get_policy());
|
||||
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);
|
||||
|
@ -128,7 +173,11 @@ ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, Mal
|
|||
{
|
||||
assert(get_policy() != NULL);
|
||||
//step forward
|
||||
bool yet_to_finish = Scheduler::get_instance().step_forward(_history, *get_policy());
|
||||
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
|
||||
|
@ -142,6 +191,34 @@ ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, Mal
|
|||
}
|
||||
}
|
||||
|
||||
bool
|
||||
ConcreteSimulation::step()
|
||||
throw(UserInterruptException, NullPolicyException, MalformedPolicyException)
|
||||
{
|
||||
if (get_policy() == NULL)
|
||||
{
|
||||
stop();
|
||||
throw NullPolicyException("no policy selected");
|
||||
}
|
||||
|
||||
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++;
|
||||
return yet_to_finish;
|
||||
}
|
||||
catch (const CPUPolicyException& e)
|
||||
{
|
||||
stop();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Simulation::state
|
||||
ConcreteSimulation::get_state() const
|
||||
{
|
||||
|
@ -154,6 +231,13 @@ ConcreteSimulation::get_history()
|
|||
return _history;
|
||||
}
|
||||
|
||||
const ConcreteHistory&
|
||||
ConcreteSimulation::get_history() const
|
||||
{
|
||||
return _history;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ConcreteSimulation::set_policy(CPUPolicy* p) throw(CPUPolicyException)
|
||||
{
|
||||
|
|
|
@ -21,9 +21,16 @@
|
|||
#ifndef CONCRETE_SIMULATION_HH
|
||||
#define CONCRETE_SIMULATION_HH 1
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "simulation.hh"
|
||||
#include "concrete_history.hh"
|
||||
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace sgpem
|
||||
{
|
||||
class ConcreteSimulation;
|
||||
|
@ -37,8 +44,12 @@ namespace sgpem
|
|||
|
||||
void pause();
|
||||
|
||||
void jump_to(History::position p);
|
||||
|
||||
void stop();
|
||||
|
||||
bool step() throw(UserInterruptException, NullPolicyException, MalformedPolicyException);
|
||||
|
||||
void set_timer(const unsigned int);
|
||||
|
||||
int get_timer() const;
|
||||
|
@ -56,8 +67,11 @@ namespace sgpem
|
|||
|
||||
ConcreteHistory& get_history();
|
||||
|
||||
const ConcreteHistory& get_history() const;
|
||||
|
||||
CPUPolicy* get_policy();
|
||||
|
||||
|
||||
private:
|
||||
state _state;
|
||||
bool _mode;
|
||||
|
|
|
@ -18,11 +18,18 @@
|
|||
// along with SGPEMv2; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "simulation.hh"
|
||||
#include "simulation_observer.hh"
|
||||
#include "concrete_simulation.hh"
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
||||
// Do not include in header file:
|
||||
#include "singleton.tcc"
|
||||
|
||||
using namespace sgpem;
|
||||
|
||||
// Explicit template instantiation to allow to export symbols from the DSO.
|
||||
|
@ -37,3 +44,47 @@ Simulation::get_instance()
|
|||
{
|
||||
return Singleton<ConcreteSimulation>::get_instance();
|
||||
}
|
||||
|
||||
void
|
||||
Simulation::attach(SimulationObserver& observer)
|
||||
{
|
||||
_observers.push_back(&observer);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Simulation::detach(const SimulationObserver& observer)
|
||||
{
|
||||
_observers.erase(std::find(_observers.begin(),
|
||||
_observers.end(),
|
||||
&observer));
|
||||
}
|
||||
|
||||
unsigned int Simulation::get_front() const
|
||||
{
|
||||
return _front;
|
||||
}
|
||||
|
||||
void
|
||||
Simulation::notify_change()
|
||||
{
|
||||
//if (!_notify) return; // what's the purpose of this?
|
||||
|
||||
for (RegisteredObservers::iterator it = _observers.begin();
|
||||
it != _observers.end(); it++)
|
||||
(*it)->update(*this);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
Simulation::set_notify_enabled(bool enabled)
|
||||
{
|
||||
bool old_value = _notify;
|
||||
_notify = enabled;
|
||||
|
||||
// Force notify if we re-enable it
|
||||
if (old_value == false && _notify == true)
|
||||
notify_change();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
|
|
@ -25,15 +25,17 @@ namespace sgpem
|
|||
{
|
||||
class ConcreteSimulation;
|
||||
class CPUPolicy;
|
||||
class History;
|
||||
class SimulationObserver;
|
||||
}
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "history.hh"
|
||||
#include "singleton.hh"
|
||||
#include "user_interrupt_exception.hh"
|
||||
#include "null_policy_exception.hh"
|
||||
#include "malformed_policy_exception.hh"
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace sgpem
|
||||
|
@ -97,6 +99,17 @@ namespace sgpem
|
|||
*/
|
||||
virtual void stop() = 0;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
\brief Jumps the simulation to the specified instant
|
||||
|
||||
Pauses the simulation and jumps to the specified instant
|
||||
*/
|
||||
virtual void jump_to(History::position p) = 0;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
\brief Setter for the attribute timer_interval.
|
||||
|
||||
|
@ -142,12 +155,44 @@ namespace sgpem
|
|||
|
||||
virtual History& get_history() = 0;
|
||||
|
||||
virtual const History& get_history() const = 0;
|
||||
|
||||
virtual unsigned int get_front() const;
|
||||
|
||||
/**
|
||||
* Small kludge to avoid the need for declaration of ConcreteSimulation
|
||||
* by the calling code of Simulation::get_instance()
|
||||
*/
|
||||
static Simulation& get_instance();
|
||||
};
|
||||
|
||||
|
||||
|
||||
virtual void attach(SimulationObserver& observer);
|
||||
virtual void detach(const SimulationObserver& observer);
|
||||
|
||||
/** \brief Enable/disable notifications to registered observers
|
||||
*
|
||||
* This is quite useful to disable momentarily notification while you
|
||||
* do a bunch of insertions and/or deletions in one go, in order to
|
||||
* speed up things.
|
||||
*
|
||||
* \return The old value
|
||||
*/
|
||||
virtual bool set_notify_enabled(bool enabled = true);
|
||||
|
||||
protected:
|
||||
typedef std::vector<SimulationObserver*> RegisteredObservers;
|
||||
RegisteredObservers _observers;
|
||||
|
||||
// since no constructor is available, these fields must be defined in concrete subclasses.
|
||||
bool _notify;
|
||||
History::position _front;
|
||||
|
||||
virtual void notify_change();
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue