- Big swing of untested code, all for you verifiers :-)

- Fix ReadyQueue constructor
- Change DynamicSubRequest to take an int as a parameter
- Implement ConcreteEnvironment::get_request_queue() (my word, it's ugly!)
- Please note that it still doesn't compile right: ConcreteHistory
and Scheduler need to be radically changed


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@692 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-07-02 17:38:30 +00:00
parent 55c6b23d31
commit 787d24964b
7 changed files with 142 additions and 110 deletions

View File

@ -1,4 +1,4 @@
// src/backend/resource.cc - Copyright 2005, 2006, University // src/backend/concrete_environment.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied // of Padova, dept. of Pure and Applied
// Mathematics // Mathematics
// //
@ -19,11 +19,18 @@
// 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 "concrete_environment.hh" #include "concrete_environment.hh"
#include "dynamic_process.hh"
#include "dynamic_resource.hh"
#include "sub_request.hh"
#include "thread.hh"
#include <algorithm>
#include <cassert>
#include <functional>
#include <iterator> #include <iterator>
using namespace sgpem; using namespace sgpem;
using namespace std;
ConcreteEnvironment::ConcreteEnvironment() ConcreteEnvironment::ConcreteEnvironment()
{ {
@ -31,43 +38,43 @@ ConcreteEnvironment::ConcreteEnvironment()
} }
ConcreteEnvironment::ConcreteEnvironment(const ConcreteEnvironment& ce) :
ConcreteEnvironment::ConcreteEnvironment(const ConcreteEnvironment & c) : Environment(ce), _resources(ce._resources), _processes(), _sched_queue()
Environment(c), _sched_queue(c._sched_queue)
{ {
// DynamicRequest objects never change, so there is no need to keep // The ReadyQueue won't be copied. Pointers to objects contained into
// a separate copy for each instant of the simulation, therefore // the ready queue _will_ have changed in the new one. The ready queue
// we simply copy the map. // needs to be reset: it is Scheduler that builds it again from time to time.
// Actually we may even avoid copiying the map, but this would not
// be worth the effort (too much code to change) // Update resource pointers in a way you won't like :-)
// Anyway, this causes much trouble when coming to destruction, see. // (for Marco -> optimization is the root of all evil! We have to
_resources = c._resources; // copy DynamicResource too; this make things simpler (and
// future code modifications to DynamicResource easier))
{
for(Resources::iterator it = _resources.begin(); it != _resources.end(); it++)
it->second = new DynamicResource(dynamic_cast<const DynamicResource&>(*it->second));
}
// DynamicProcess object need to be copied. // DynamicProcess object need to be copied.
// The deep copy is guaranteed by the DynamicProcess copy constructor // The deep copy is guaranteed by the DynamicProcess copy constructor
std::vector<Process *>::const_iterator iter = c._processes.begin(); {
while (iter != c._processes.end()) const Processes& ce_proc = ce._processes;
{ insert_iterator<Processes> dest(_processes, _processes.begin());
DynamicProcess * current = dynamic_cast<DynamicProcess *>(*iter++); for(Processes::const_iterator orig = ce_proc.begin(); orig != ce_proc.end(); orig++)
_processes.push_back(new DynamicProcess(*current)); *dest++ = new DynamicProcess(dynamic_cast<const DynamicProcess&>(**orig));
} }
// The ready queue needs to be copied, too.
// This implementation relies on the copy constructor
// which is explicit in the initialization list
} }
const std::vector<const Process*> const Environment::ConstProcesses
ConcreteEnvironment::get_processes() const ConcreteEnvironment::get_processes() const
{ {
return std::vector<const Process*>(_processes.begin(), _processes.end()); return ConstProcesses(_processes.begin(), _processes.end());
} }
std::vector<Process*>& ConcreteEnvironment::Processes&
ConcreteEnvironment::get_processes() ConcreteEnvironment::get_processes()
{ {
return _processes; return _processes;
@ -75,15 +82,15 @@ ConcreteEnvironment::get_processes()
const std::map<int, const Resource*> const Environment::ConstResources
ConcreteEnvironment::get_resources() const ConcreteEnvironment::get_resources() const
{ {
return std::map<int, const Resource*>(_resources.begin(), _resources.end()); return map<int, const Resource*>(_resources.begin(), _resources.end());
} }
std::map<int, Resource*>& ConcreteEnvironment::Resources&
ConcreteEnvironment::get_resources() ConcreteEnvironment::get_resources()
{ {
return _resources; return _resources;
@ -91,12 +98,44 @@ ConcreteEnvironment::get_resources()
const std::vector<const Request*> const Environment::ConstRequests
ConcreteEnvironment::get_request_queue(Resource* resource) const ConcreteEnvironment::get_request_queue(resource_key_t resource_key) const
{ {
std::vector<const Request *> request_queue; ConstRequests request_queue;
// TODO: fill that vector, walking over the classes, looking for
// those no-more-valid requests. typedef Processes::const_iterator it1_t;
typedef std::vector<Thread*> v2_t;
typedef v2_t::const_iterator it2_t;
typedef std::vector<Request*> v3_t;
typedef v3_t::const_iterator it3_t;
typedef std::vector<SubRequest*> v4_t;
typedef v4_t::const_iterator it4_t;
// Cyclomatic complexity will go nuts here. Feel the love. _ALL_ of it.
for(it1_t it1 = _processes.begin(); it1 != _processes.end(); it1++)
{
const v2_t& threads = (*it1)->get_threads();
for(it2_t it2 = threads.begin(); it2 != threads.end(); it2++)
{
const v3_t& reqs = (*it2)->get_requests();
for(it3_t it3 = reqs.begin(); it3 != reqs.end(); it3++)
{
const v4_t& subr = (*it3)->get_subrequests();
for(it4_t it4 = subr.begin(); it4 != subr.end(); it4++)
{
if((*it4)->get_resource() == resource_key)
{
request_queue.push_back(*it3);
break;
}
}
}
}
}
return request_queue; return request_queue;
} }
@ -120,24 +159,17 @@ ConcreteEnvironment::get_sorted_queue()
ConcreteEnvironment::~ConcreteEnvironment() ConcreteEnvironment::~ConcreteEnvironment()
{ {
std::vector<Process *>::iterator iter = _processes.begin(); // This call will invoke the DynamicProcess virtual destructor
while (iter != _processes.end()) // Which will delete on cascade all DynamicThreads and so on.
{ for_each(_processes.begin(), _processes.end(), ptr_fun(operator delete));
// This call will invoke the DynamicProcess virtual destructor
// Which will delete on cascade all DynamicThreads and so on. // We do the same with Resources.
delete *iter++; for(Resources::iterator it = _resources.begin(); it != _resources.end(); it++)
// The iterator is still valid, since the pointed object is not, delete it->second;
// but the pointer is.
} // After this, the destructor of _sched_queue is invoked (only invalid pointers)
// After this, the destructor of _sched_queue is invoked // After that, the destructor of _processes is invoked (only invalid pointers)
// After that, the destructor of _processes is invoked // After that, the destructor of _resources is invoked (only invalid pointers)
// After that, the destructor of _resources is invoked
// The current implementation does not destroy the DynamicResource
// objects, as they are shared among all the DynamicEnvironment
// objects. This means that if none deletes them when deleting the
// last DynamicEnvironment object, we have a memory leak.
// And we do have one, since none by now cares for this. Maybe
// History will. Anyway, this is not nice, so please FIXME.
} }

View File

@ -1,4 +1,4 @@
// src/backend/resource.hh - Copyright 2005, 2006, University // src/backend/concrete_environment.hh - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied // of Padova, dept. of Pure and Applied
// Mathematics // Mathematics
// //
@ -47,18 +47,18 @@ namespace sgpem
{ {
public: public:
typedef std::vector<Process*> Processes;
typedef std::map<resource_key_t, Resource*> Resources;
/// \brief Standard constructor. /// \brief Standard constructor.
/// Builds an empty environment. /// Builds an empty environment.
ConcreteEnvironment(); ConcreteEnvironment();
/// \brief Copy constructor. /// \brief Copy constructor.
/// Performs a deep copy of all structures. /// Performs a deep copy of all structures.
ConcreteEnvironment(const ConcreteEnvironment & c); ConcreteEnvironment(const ConcreteEnvironment & c);
/// \brief Returns an indexed set of snapshots of the processes /// \brief Returns an indexed set of snapshots of the processes
/// Returns a standard vector of Process objects describing /// Returns a standard vector of Process objects describing
/// all the processes of the simulated environment at the /// all the processes of the simulated environment at the
@ -69,8 +69,8 @@ namespace sgpem
/// always safe. /// always safe.
/// ///
/// \return a constant set of snapshots of processes /// \return a constant set of snapshots of processes
virtual const std::vector<const Process*> virtual const ConstProcesses
get_processes() const; get_processes() const;
@ -78,8 +78,8 @@ namespace sgpem
/// ///
/// \return a set of snapshots of processes /// \return a set of snapshots of processes
/// \see get_processes() /// \see get_processes()
virtual std::vector<Process*>& virtual Processes&
get_processes(); get_processes();
@ -101,8 +101,8 @@ namespace sgpem
/// ///
/// \return a indexed constant set of snapshot of resources. /// \return a indexed constant set of snapshot of resources.
/// \see DynamicSybrequest::get_resource() /// \see DynamicSybrequest::get_resource()
virtual const std::map<int, const Resource*> virtual const ConstResources
get_resources() const; get_resources() const;
@ -110,8 +110,8 @@ namespace sgpem
/// ///
/// \return an indexed set of snapshots of resources /// \return an indexed set of snapshots of resources
/// \see get_resources() /// \see get_resources()
virtual std::map<int, Resource*>& virtual Resources&
get_resources(); get_resources();
@ -126,8 +126,8 @@ namespace sgpem
/// ///
/// \param resource The resource the requests are for /// \param resource The resource the requests are for
/// \return The current ready requests queue. /// \return The current ready requests queue.
virtual const std::vector<const Request*> virtual const ConstRequests
get_request_queue(Resource * resource) const; get_request_queue(resource_key_t resource_key) const;
@ -139,7 +139,7 @@ namespace sgpem
/// ///
/// \return the current ready queue (constant). /// \return the current ready queue (constant).
virtual const ReadyQueue& virtual const ReadyQueue&
get_sorted_queue() const; get_sorted_queue() const;
@ -148,46 +148,36 @@ namespace sgpem
/// \return the current ready queue. /// \return the current ready queue.
/// \see get_sorted_queue() /// \see get_sorted_queue()
virtual ReadyQueue& virtual ReadyQueue&
get_sorted_queue(); get_sorted_queue();
/// \brief The standard virtual destructor. /// \brief The standard virtual destructor.
/// The standard virtual destructor. /// The standard virtual destructor.
virtual virtual
~ConcreteEnvironment(); ~ConcreteEnvironment();
/// \brief Serializes the whole environment. /// \brief Serializes the whole environment.
/// \see SerializeVisitor /// \see SerializeVisitor
virtual void virtual void
serialize(SerializeVisitor& translator) const; serialize(SerializeVisitor& translator) const;
private: private:
/// \brief The container of all Resource objecs. /// \brief The container of all Resource objecs.
/// Actually contains only DynamicResource objects. /// Actually contains only DynamicResource objects.
// resources come before processes because of // resources come before processes because of
// destruction order. See destructor implementation // destruction order. See destructor implementation
std::map<int, Resource *> Resources _resources;
_resources;
/// \brief The container of all Process objecs. /// \brief The container of all Process objecs.
/// Actually contains only DynamicProcess objects. /// Actually contains only DynamicProcess objects.
std::vector<Process *> Processes _processes;
_processes;
/// \brief The queue of the ready schedulables /// \brief The queue of the ready schedulables
/// Does not contain the running process. /// Does not contain the running process.
ReadyQueue ReadyQueue _sched_queue;
_sched_queue;
}; //~ class ConcreteEnvironment }; //~ class ConcreteEnvironment

View File

@ -26,13 +26,11 @@
using namespace sgpem; using namespace sgpem;
DynamicSubRequest::DynamicSubRequest(StaticSubRequest* core, DynamicSubRequest::DynamicSubRequest(StaticSubRequest* core, resource_key_t key) :
DynamicResource* resource) : _static_subrequest(core), _resource_key(key),
_static_subrequest(core), _dynamic_resource(resource), _queue_position(-1)
_queue_position(-1)
{ {
assert(core != NULL); assert(core != NULL);
assert(resource != NULL);
} }
bool bool
@ -43,10 +41,10 @@ DynamicSubRequest::operator==(const SubRequest& op2) const
} }
DynamicResource& SubRequest::resource_key_t
DynamicSubRequest::get_resource() DynamicSubRequest::get_resource_key() const
{ {
return *_dynamic_resource; return _resource_key;
} }
unsigned int unsigned int

View File

@ -38,11 +38,11 @@ namespace sgpem
class SG_DLLLOCAL DynamicSubRequest : public SubRequest class SG_DLLLOCAL DynamicSubRequest : public SubRequest
{ {
public: public:
DynamicSubRequest(StaticSubRequest* core, DynamicResource* resource); DynamicSubRequest(StaticSubRequest* core, resource_key_t resource);
virtual bool operator==(const SubRequest& op2) const; virtual bool operator==(const SubRequest& op2) const;
DynamicResource& get_resource(); resource_key_t get_resource_key() const;
unsigned int get_places() const; unsigned int get_places() const;
@ -55,8 +55,8 @@ namespace sgpem
private: private:
memory::smart_ptr<StaticSubRequest> _static_subrequest; memory::smart_ptr<StaticSubRequest> _static_subrequest;
DynamicResource* _dynamic_resource; resource_key_t _resource_key;
int _queue_position; int _queue_position;
}; };
} }

View File

@ -22,8 +22,12 @@
#define ENVIRONMENT_HH 1 #define ENVIRONMENT_HH 1
#include "config.h" #include "config.h"
#include <vector>
#include "sub_request.hh"
#include <map> #include <map>
#include <utility>
#include <vector>
namespace sgpem namespace sgpem
{ {
@ -54,6 +58,11 @@ namespace sgpem
class SG_DLLEXPORT Environment class SG_DLLEXPORT Environment
{ {
public: public:
typedef SubRequest::resource_key_t resource_key_t;
typedef std::vector<const Process*> ConstProcesses;
typedef std::map<resource_key_t, const Resource*> ConstResources;
typedef std::vector<const Request*> ConstRequests;
/// \brief Returns an indexed set of snapshots of the processes /// \brief Returns an indexed set of snapshots of the processes
/// Returns a standard vector of Process objects describing /// Returns a standard vector of Process objects describing
@ -62,8 +71,8 @@ namespace sgpem
/// ///
/// \return a constant set of snapshots of processes /// \return a constant set of snapshots of processes
virtual const std::vector<const Process*> virtual const ConstProcesses
get_processes() const = 0; get_processes() const = 0;
/// \brief Returns an indexed set of snapshots of the resources /// \brief Returns an indexed set of snapshots of the resources
@ -80,8 +89,8 @@ namespace sgpem
/// ///
/// \return an indexed constant set of snapshot of resources. /// \return an indexed constant set of snapshot of resources.
virtual const std::map<int, const Resource*> virtual const ConstResources
get_resources() const = 0; get_resources() const = 0;
/// \brief Returns a snapshot of the current request queue for a resource. /// \brief Returns a snapshot of the current request queue for a resource.
@ -91,8 +100,8 @@ namespace sgpem
/// \param resource the resource the requests are for /// \param resource the resource the requests are for
/// \return the current ready requests queue (constant). /// \return the current ready requests queue (constant).
virtual const std::vector<const Request*> virtual const ConstRequests
get_request_queue(const Resource * resource) const = 0; get_request_queue(resource_key_t resource_key) const = 0;
/// \brief Returns a snapshot of the current scheduler's ready queue. /// \brief Returns a snapshot of the current scheduler's ready queue.
@ -103,7 +112,7 @@ namespace sgpem
/// \return the current ready queue (constant). /// \return the current ready queue (constant).
virtual const ReadyQueue& virtual const ReadyQueue&
get_sorted_queue() const = 0; get_sorted_queue() const = 0;
/// \brief The standard virtual destructor. /// \brief The standard virtual destructor.

View File

@ -59,7 +59,8 @@ namespace sgpem
typedef unsigned int size_t; typedef unsigned int size_t;
typedef unsigned int time_t; typedef unsigned int time_t;
typedef int prio_t; typedef int prio_t;
typedef int resource_key_t;
typedef Environment::resource_key_t resource_key_t;
typedef const std::pair<resource_key_t, Resource&> ResourcePair; typedef const std::pair<resource_key_t, Resource&> ResourcePair;
virtual ~History() = 0; virtual ~History() = 0;

View File

@ -32,15 +32,17 @@ namespace sgpem
class SG_DLLEXPORT SubRequest class SG_DLLEXPORT SubRequest
{ {
public: public:
typedef int resource_key_t;
virtual ~SubRequest(); virtual ~SubRequest();
virtual bool operator==(const SubRequest& op2) const = 0; virtual bool operator==(const SubRequest& op2) const = 0;
virtual Resource& get_resource() = 0; virtual resource_key_t get_resource() = 0;
virtual unsigned int get_places() const = 0; virtual unsigned int get_places() const = 0;
virtual unsigned int get_length() const = 0; virtual unsigned int get_length() const = 0;
virtual int get_queue_position() const = 0; virtual int get_queue_position() const = 0;