- 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
// Mathematics
//
@ -19,11 +19,18 @@
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#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>
using namespace sgpem;
using namespace std;
ConcreteEnvironment::ConcreteEnvironment()
{
@ -31,43 +38,43 @@ ConcreteEnvironment::ConcreteEnvironment()
}
ConcreteEnvironment::ConcreteEnvironment(const ConcreteEnvironment & c) :
Environment(c), _sched_queue(c._sched_queue)
ConcreteEnvironment::ConcreteEnvironment(const ConcreteEnvironment& ce) :
Environment(ce), _resources(ce._resources), _processes(), _sched_queue()
{
// DynamicRequest objects never change, so there is no need to keep
// a separate copy for each instant of the simulation, therefore
// we simply copy the map.
// Actually we may even avoid copiying the map, but this would not
// be worth the effort (too much code to change)
// Anyway, this causes much trouble when coming to destruction, see.
_resources = c._resources;
// The ReadyQueue won't be copied. Pointers to objects contained into
// the ready queue _will_ have changed in the new one. The ready queue
// needs to be reset: it is Scheduler that builds it again from time to time.
// Update resource pointers in a way you won't like :-)
// (for Marco -> optimization is the root of all evil! We have to
// 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.
// The deep copy is guaranteed by the DynamicProcess copy constructor
std::vector<Process *>::const_iterator iter = c._processes.begin();
while (iter != c._processes.end())
{
DynamicProcess * current = dynamic_cast<DynamicProcess *>(*iter++);
_processes.push_back(new DynamicProcess(*current));
}
// The ready queue needs to be copied, too.
// This implementation relies on the copy constructor
// which is explicit in the initialization list
{
const Processes& ce_proc = ce._processes;
insert_iterator<Processes> dest(_processes, _processes.begin());
for(Processes::const_iterator orig = ce_proc.begin(); orig != ce_proc.end(); orig++)
*dest++ = new DynamicProcess(dynamic_cast<const DynamicProcess&>(**orig));
}
}
const std::vector<const Process*>
const Environment::ConstProcesses
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()
{
return _processes;
@ -75,15 +82,15 @@ ConcreteEnvironment::get_processes()
const std::map<int, const Resource*>
const Environment::ConstResources
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()
{
return _resources;
@ -91,12 +98,44 @@ ConcreteEnvironment::get_resources()
const std::vector<const Request*>
ConcreteEnvironment::get_request_queue(Resource* resource) const
const Environment::ConstRequests
ConcreteEnvironment::get_request_queue(resource_key_t resource_key) const
{
std::vector<const Request *> request_queue;
// TODO: fill that vector, walking over the classes, looking for
// those no-more-valid requests.
ConstRequests request_queue;
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;
}
@ -120,24 +159,17 @@ ConcreteEnvironment::get_sorted_queue()
ConcreteEnvironment::~ConcreteEnvironment()
{
std::vector<Process *>::iterator iter = _processes.begin();
while (iter != _processes.end())
{
// This call will invoke the DynamicProcess virtual destructor
// Which will delete on cascade all DynamicThreads and so on.
delete *iter++;
// The iterator is still valid, since the pointed object is not,
// but the pointer is.
}
// After this, the destructor of _sched_queue is invoked
// After that, the destructor of _processes is invoked
// 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.
// This call will invoke the DynamicProcess virtual destructor
// Which will delete on cascade all DynamicThreads and so on.
for_each(_processes.begin(), _processes.end(), ptr_fun(operator delete));
// We do the same with Resources.
for(Resources::iterator it = _resources.begin(); it != _resources.end(); it++)
delete it->second;
// After this, the destructor of _sched_queue is invoked (only invalid pointers)
// After that, the destructor of _processes is invoked (only invalid pointers)
// After that, the destructor of _resources is invoked (only invalid pointers)
}