- "You got another thing comin'" -- Judas Priest

- Add get_request() method to (Dynamic)SubRequest.
- Implement most of ConcreteHistory. It is a fairly complex class,
with some real evilness in it. It should be thouroughly documented 
ASAP.


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@699 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-07-03 18:52:50 +00:00
parent 68b92db976
commit cb8e8dabc7
13 changed files with 212 additions and 104 deletions

View File

@ -66,10 +66,10 @@ ConcreteEnvironment::ConcreteEnvironment(const ConcreteEnvironment& ce) :
const Environment::ConstProcesses const Environment::Processes&
ConcreteEnvironment::get_processes() const ConcreteEnvironment::get_processes() const
{ {
return ConstProcesses(_processes.begin(), _processes.end()); return _processes;
} }
@ -82,10 +82,10 @@ ConcreteEnvironment::get_processes()
const Environment::ConstResources const Environment::Resources&
ConcreteEnvironment::get_resources() const ConcreteEnvironment::get_resources() const
{ {
return map<int, const Resource*>(_resources.begin(), _resources.end()); return _resources;
} }
@ -98,10 +98,10 @@ ConcreteEnvironment::get_resources()
const Environment::ConstRequests const Environment::Requests
ConcreteEnvironment::get_request_queue(resource_key_t resource_key) const ConcreteEnvironment::get_request_queue(resource_key_t resource_key) const
{ {
ConstRequests request_queue; Requests request_queue;
typedef Processes::const_iterator it1_t; typedef Processes::const_iterator it1_t;
@ -126,7 +126,7 @@ ConcreteEnvironment::get_request_queue(resource_key_t resource_key) const
const v4_t& subr = (*it3)->get_subrequests(); const v4_t& subr = (*it3)->get_subrequests();
for(it4_t it4 = subr.begin(); it4 != subr.end(); it4++) for(it4_t it4 = subr.begin(); it4 != subr.end(); it4++)
{ {
if((*it4)->get_resource() == resource_key) if((*it4)->get_resource_key() == resource_key)
{ {
request_queue.push_back(*it3); request_queue.push_back(*it3);
break; break;

View File

@ -47,9 +47,6 @@ 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();
@ -69,7 +66,7 @@ namespace sgpem
/// always safe. /// always safe.
/// ///
/// \return a constant set of snapshots of processes /// \return a constant set of snapshots of processes
virtual const ConstProcesses virtual const Processes&
get_processes() const; get_processes() const;
@ -101,7 +98,7 @@ 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 ConstResources virtual const Resources&
get_resources() const; get_resources() const;
@ -126,7 +123,7 @@ 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 ConstRequests virtual const Requests
get_request_queue(resource_key_t resource_key) const; get_request_queue(resource_key_t resource_key) const;

View File

@ -22,6 +22,12 @@
#include "concrete_history.hh" #include "concrete_history.hh"
#include "dynamic_process.hh"
#include "dynamic_thread.hh"
#include "dynamic_resource.hh"
#include "dynamic_request.hh"
#include "dynamic_sub_request.hh"
#include "static_process.hh" #include "static_process.hh"
#include "static_thread.hh" #include "static_thread.hh"
#include "static_resource.hh" #include "static_resource.hh"
@ -38,6 +44,40 @@ using namespace sgpem;
using namespace std; using namespace std;
using memory::smart_ptr; using memory::smart_ptr;
// ---------------
// For all your evil-doers on Earth, this is your punishment!
template<typename T>
static bool deep_remove(std::vector<T*>& v, const T& obj)
{
typedef typename std::vector<T*> Vector;
for(typename Vector::iterator it = v.begin(); it != v.end(); it++)
if(**it == obj)
{
delete *it;
v.erase(it);
return true;
}
return false;
}
template<typename T>
static T* deep_find(const std::vector<T*>& v, const T& obj)
{
typedef typename std::vector<T*> Vector;
for(typename Vector::const_iterator it = v.begin(); it != v.end(); it++)
if(**it == obj)
{
return *it;
}
return NULL;
}
// -----------------
ConcreteHistory::ConcreteHistory() ConcreteHistory::ConcreteHistory()
: History(), _snapshots() : History(), _snapshots()
{ {
@ -85,23 +125,18 @@ ConcreteHistory::get_environment_at(position index) const
void void
ConcreteHistory::remove(resource_key_t resource_key) ConcreteHistory::remove(resource_key_t resource_key)
{ {
// Pay attention that initial isn't deleted by reset()
ConcreteEnvironment& initial = *_snapshots.front(); ConcreteEnvironment& initial = *_snapshots.front();
ConcreteEnvironment::Resources& resources = initial.get_resources(); ConcreteEnvironment::Resources& resources = initial.get_resources();
ConcreteEnvironment::Resources::iterator found = resources.find(resource_key); ConcreteEnvironment::Resources::iterator found = resources.find(resource_key);
if(found == resources.end()) if(found == resources.end())
{ return;
notify_change();
return; // not found, just return. Can we do this better (without a reset)?
}
reset(false); reset(false);
// FIXME: I'm really tired, check that the next two lines do
// what they're meant to do!
delete found->second; delete found->second;
resources.erase(found); resources.erase(found);
#warning "write me!" #warning "write me!"
// FIXME write me : check for subrequests to remove // FIXME write me : check for subrequests to remove
@ -110,47 +145,92 @@ ConcreteHistory::remove(resource_key_t resource_key)
void void
ConcreteHistory::remove(const Process& process) ConcreteHistory::remove(Process& process)
{ {
// Pay attention that initial isn't deleted by reset()
reset(false);
ConcreteEnvironment& initial = *_snapshots.front(); ConcreteEnvironment& initial = *_snapshots.front();
ConcreteEnvironment::Processes& processes = initial.get_processes(); ConcreteEnvironment::Processes& processes = initial.get_processes();
ConcreteEnvironment::Processes::iterator found = find(processes.begin(), processes.end(), &process); bool found = deep_remove<Process>(processes, process);
if(found == processes.end()) if(found)
{ reset(true);
notify_change();
return; // not found, just return. Can we do this better (without a reset)?
}
// FIXME: I'm really tired, check that the next two lines do
// what they're meant to do!
delete *found;
processes.erase(found);
notify_change();
} }
void void
ConcreteHistory::remove(const Thread& thread) ConcreteHistory::remove(Thread& thread)
{ {
#warning "write me!" DynamicThread& dyn_thr = dynamic_cast<DynamicThread&>(thread);
// Pay attention that initial isn't deleted by reset()
ConcreteEnvironment& initial = *_snapshots.front();
ConcreteEnvironment::Processes& processes = initial.get_processes();
Process* found = deep_find<Process>(processes, dyn_thr.get_process());
if(found == NULL)
return; // not found, just return.
DynamicProcess& dynamic_found = dynamic_cast<DynamicProcess&>(*found);
bool removed = deep_remove<DynamicThread>(dynamic_found.get_dynamic_threads(), dyn_thr);
if(removed)
reset(true);
} }
void void
ConcreteHistory::remove(const Request& request) ConcreteHistory::remove(Request& request)
{ {
#warning "write me!" DynamicRequest& dyn_req = dynamic_cast<DynamicRequest&>(request);
DynamicThread& dyn_thr = dyn_req.get_thread();
DynamicProcess& dyn_proc = dyn_thr.get_process();
// Pay attention that initial isn't deleted by reset()
ConcreteEnvironment& initial = *_snapshots.front();
ConcreteEnvironment::Processes& processes = initial.get_processes();
Process* proc_ref = deep_find<Process>(processes, dyn_proc);
DynamicProcess* dyn_proc_ref = dynamic_cast<DynamicProcess*>(proc_ref);
if(dyn_proc_ref == NULL)
return; // not found, just return.
DynamicThread* thr_ref = deep_find<DynamicThread>(dyn_proc_ref->get_dynamic_threads(), dyn_thr);
if(thr_ref == NULL)
return; // not found, just return.
bool removed = deep_remove<DynamicRequest>(thr_ref->get_dynamic_requests(), dyn_req);
if(removed)
reset(true);
} }
void void
ConcreteHistory::remove(const SubRequest& subrequest) ConcreteHistory::remove(SubRequest& subrequest)
{ {
#warning "write me!" DynamicSubRequest& dyn_sub = dynamic_cast<DynamicSubRequest&>(subrequest);
DynamicRequest& dyn_req = dyn_sub.get_request();
DynamicThread& dyn_thr = dyn_req.get_thread();
DynamicProcess& dyn_proc = dyn_thr.get_process();
// Pay attention that initial isn't deleted by reset()
ConcreteEnvironment& initial = *_snapshots.front();
ConcreteEnvironment::Processes& processes = initial.get_processes();
Process* proc_ref = deep_find<Process>(processes, dyn_proc);
DynamicProcess* dyn_proc_ref = dynamic_cast<DynamicProcess*>(proc_ref);
if(dyn_proc_ref == NULL)
return; // not found, just return.
DynamicThread* thr_ref = deep_find<DynamicThread>(dyn_proc_ref->get_dynamic_threads(), dyn_thr);
if(thr_ref == NULL)
return; // not found, just return.
DynamicRequest* req_ref = deep_find<DynamicRequest>(thr_ref->get_dynamic_requests(), dyn_req);
if(req_ref == NULL)
return; // not found, just return.
bool removed = deep_remove<DynamicSubRequest>(req_ref->get_dynamic_subrequests(), dyn_sub);
if(removed)
reset(true);
} }
@ -160,7 +240,17 @@ ConcreteHistory::add_resource(const Glib::ustring& name,
size_t places, size_t places,
size_t availability) size_t availability)
{ {
#warning "write me!" reset(false);
// And preemptable and availability?? FIXME!
StaticResource* core = new StaticResource(name, places);
DynamicResource* resource = new DynamicResource(core);
#warning "write me! insert into map and get iterator in an efficient way."
notify_change();
} }
@ -178,6 +268,7 @@ ConcreteHistory::add_process(const Glib::ustring& name,
processes.push_back(proc); processes.push_back(proc);
notify_change(); notify_change();
return *proc;
} }
@ -197,6 +288,7 @@ ConcreteHistory::add_thread(const Glib::ustring& name,
DynamicThread* thread = new DynamicThread(core, &parent_process); DynamicThread* thread = new DynamicThread(core, &parent_process);
notify_change(); notify_change();
return *thread;
} }
@ -204,7 +296,18 @@ DynamicRequest&
ConcreteHistory::add_request(Thread& owner, ConcreteHistory::add_request(Thread& owner,
time_t instant) time_t instant)
{ {
#warning "write me!" reset(false);
DynamicThread& dyn_owner = dynamic_cast<DynamicThread&>(owner);
StaticThread& owner_core = dyn_owner.get_core();
StaticRequest* core = new StaticRequest(&owner_core, instant);
DynamicRequest* req = new DynamicRequest(core, &dyn_owner);
dyn_owner.get_requests().push_back(req);
notify_change();
return *req;
} }
@ -214,25 +317,30 @@ ConcreteHistory::add_subrequest(Request& request,
time_t duration, time_t duration,
size_t places) size_t places)
{ {
#warning "write me!" reset(false);
DynamicRequest& dyn_request = dynamic_cast<DynamicRequest&>(request);
StaticRequest& request_core = dyn_request.get_core();
StaticSubRequest* core = new StaticSubRequest(&request_core, resource_key, duration, places);
DynamicSubRequest* subreq = new DynamicSubRequest(core, &dyn_request);
dyn_request.get_subrequests().push_back(subreq);
notify_change();
return *subreq;
} }
void void
ConcreteHistory::reset(bool notify) ConcreteHistory::reset(bool notify)
{ {
assert(_snapshots.size() > 0);
Snapshots::iterator it = _snapshots.begin(); Snapshots::iterator it = _snapshots.begin();
ConcreteEnvironment* model = *it;
it++; // Skip first environment that we saved it++; // Skip first environment that we saved
for_each(it, _snapshots.end(), ptr_fun(operator delete)); for_each(it, _snapshots.end(), ptr_fun(operator delete));
_snapshots.clear(); _snapshots.resize(1); // Truncate to keep only our "model"
_snapshots.push_back(new ConcreteEnvironment());
// FIXME write code to copy processes and threads and subrequests......
#warning "write me!"
delete model;
if(notify) if(notify)
notify_change(); notify_change();

View File

@ -57,10 +57,10 @@ namespace sgpem
virtual const ConcreteEnvironment& get_environment_at(position index) const throw(std::out_of_range); virtual const ConcreteEnvironment& get_environment_at(position index) const throw(std::out_of_range);
virtual void remove(resource_key_t resource_key); virtual void remove(resource_key_t resource_key);
virtual void remove(const Process& process); virtual void remove(Process& process);
virtual void remove(const Thread& thread); virtual void remove(Thread& thread);
virtual void remove(const Request& request); virtual void remove(Request& request);
virtual void remove(const SubRequest& subrequest); virtual void remove(SubRequest& subrequest);
virtual ResourcePair& add_resource(const Glib::ustring& name, virtual ResourcePair& add_resource(const Glib::ustring& name,

View File

@ -45,10 +45,6 @@ DynamicRequest::DynamicRequest(StaticRequest *core,
DynamicRequest::~DynamicRequest() DynamicRequest::~DynamicRequest()
{ {
typedef std::vector<DynamicRequest*> Requests;
Requests& siblings = _dynamic_thread->get_dynamic_requests();
siblings.erase(find(siblings.begin(), siblings.end(), this));
for_each(_dynamic_subrequests.begin(), _dynamic_subrequests.end(), ptr_fun(operator delete)); for_each(_dynamic_subrequests.begin(), _dynamic_subrequests.end(), ptr_fun(operator delete));
} }

View File

@ -29,9 +29,8 @@
using namespace sgpem; using namespace sgpem;
DynamicSubRequest::DynamicSubRequest(StaticSubRequest* core, DynamicSubRequest::DynamicSubRequest(StaticSubRequest* core,
DynamicRequest* owner, DynamicRequest* owner) :
resource_key_t key) : _static_subrequest(core), _owner(owner),
_static_subrequest(core), _owner(owner), _resource_key(key),
_queue_position(-1) _queue_position(-1)
{ {
assert(core != NULL); assert(core != NULL);
@ -41,9 +40,6 @@ DynamicSubRequest::DynamicSubRequest(StaticSubRequest* core,
DynamicSubRequest::~DynamicSubRequest() DynamicSubRequest::~DynamicSubRequest()
{ {
typedef std::vector<DynamicSubRequest*> SubRequests;
SubRequests& siblings = _owner->get_dynamic_subrequests();
siblings.erase(find(siblings.begin(), siblings.end(), this));
} }
@ -58,7 +54,7 @@ DynamicSubRequest::operator==(const SubRequest& op2) const
SubRequest::resource_key_t SubRequest::resource_key_t
DynamicSubRequest::get_resource_key() const DynamicSubRequest::get_resource_key() const
{ {
return _resource_key; return _static_subrequest->get_resource_key();
} }
unsigned int unsigned int
@ -85,6 +81,14 @@ DynamicSubRequest::set_queue_position(int position)
_queue_position = position; _queue_position = position;
} }
DynamicRequest&
DynamicSubRequest::get_request()
{
return *_owner;
}
void void
DynamicSubRequest::serialize(SerializeVisitor& translator) const DynamicSubRequest::serialize(SerializeVisitor& translator) const
{ {

View File

@ -23,6 +23,7 @@
#include "config.h" #include "config.h"
#include "sub_request.hh" #include "sub_request.hh"
#include "dynamic_request.hh"
#include "dynamic_resource.hh" #include "dynamic_resource.hh"
#include "static_sub_request.hh" #include "static_sub_request.hh"
@ -31,7 +32,6 @@
namespace sgpem namespace sgpem
{ {
class DynamicSubRequest; class DynamicSubRequest;
class DynamicRequest;
class SerializeVisitor; class SerializeVisitor;
class Resource; class Resource;
class StaticSubRequest; class StaticSubRequest;
@ -40,8 +40,7 @@ namespace sgpem
{ {
public: public:
DynamicSubRequest(StaticSubRequest* core, DynamicSubRequest(StaticSubRequest* core,
DynamicRequest* owner, DynamicRequest* owner);
resource_key_t resource);
virtual ~DynamicSubRequest(); virtual ~DynamicSubRequest();
@ -56,6 +55,8 @@ namespace sgpem
int get_queue_position() const; int get_queue_position() const;
void set_queue_position(int position); void set_queue_position(int position);
virtual DynamicRequest& get_request();
void serialize(SerializeVisitor& translator) const; void serialize(SerializeVisitor& translator) const;
StaticSubRequest& get_core(); StaticSubRequest& get_core();
@ -64,11 +65,9 @@ namespace sgpem
private: private:
memory::smart_ptr<StaticSubRequest> _static_subrequest; memory::smart_ptr<StaticSubRequest> _static_subrequest;
DynamicRequest* _owner; DynamicRequest* _owner;
resource_key_t _resource_key;
int _queue_position; int _queue_position;
}; };
} }
#endif #endif

View File

@ -54,10 +54,6 @@ DynamicThread::DynamicThread(const DynamicThread &other) :
DynamicThread::~DynamicThread() DynamicThread::~DynamicThread()
{ {
typedef std::vector<DynamicThread*> Threads;
Threads& siblings = _parent->get_dynamic_threads();
siblings.erase(find(siblings.begin(), siblings.end(), this));
for_each(_dynamic_requests.begin(), _dynamic_requests.end(), ptr_fun(operator delete)); for_each(_dynamic_requests.begin(), _dynamic_requests.end(), ptr_fun(operator delete));
} }

View File

@ -60,9 +60,9 @@ namespace sgpem
public: public:
typedef SubRequest::resource_key_t resource_key_t; typedef SubRequest::resource_key_t resource_key_t;
typedef std::vector<const Process*> ConstProcesses; typedef std::vector<Process*> Processes;
typedef std::map<resource_key_t, const Resource*> ConstResources; typedef std::map<resource_key_t, Resource*> Resources;
typedef std::vector<const Request*> ConstRequests; typedef std::vector<Request*> Requests;
/// \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
@ -71,7 +71,7 @@ namespace sgpem
/// ///
/// \return a constant set of snapshots of processes /// \return a constant set of snapshots of processes
virtual const ConstProcesses virtual const Processes&
get_processes() const = 0; get_processes() const = 0;
@ -89,7 +89,7 @@ namespace sgpem
/// ///
/// \return an indexed constant set of snapshot of resources. /// \return an indexed constant set of snapshot of resources.
virtual const ConstResources virtual const Resources&
get_resources() const = 0; get_resources() const = 0;
@ -100,7 +100,7 @@ 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 ConstRequests virtual const Requests
get_request_queue(resource_key_t resource_key) const = 0; get_request_queue(resource_key_t resource_key) const = 0;

View File

@ -71,10 +71,10 @@ namespace sgpem
virtual const Environment& get_environment_at() const throw(std::out_of_range) = 0; virtual const Environment& get_environment_at() const throw(std::out_of_range) = 0;
virtual void remove(resource_key_t resource_key) = 0; virtual void remove(resource_key_t resource_key) = 0;
virtual void remove(const Process& process) = 0; virtual void remove(Process& process) = 0;
virtual void remove(const Thread& thread) = 0; virtual void remove(Thread& thread) = 0;
virtual void remove(const Request& request) = 0; virtual void remove(Request& request) = 0;
virtual void remove(const SubRequest& subrequest) = 0; virtual void remove(SubRequest& subrequest) = 0;
virtual ResourcePair& add_resource(const Glib::ustring& name, virtual ResourcePair& add_resource(const Glib::ustring& name,

View File

@ -27,10 +27,10 @@
using namespace sgpem; using namespace sgpem;
StaticSubRequest::StaticSubRequest(StaticRequest* req, StaticSubRequest::StaticSubRequest(StaticRequest* req,
StaticResource* resource, resource_key_t resource_key,
unsigned int length, unsigned int length,
unsigned int places) : unsigned int places) :
_static_request(req), _static_resource(resource), _static_request(req), _resource_key(resource_key),
_length(length), _places(places) _length(length), _places(places)
{ {
assert(req != NULL && resource != NULL); assert(req != NULL && resource != NULL);
@ -42,13 +42,13 @@ StaticSubRequest::~StaticSubRequest()
{ {
typedef std::vector<StaticSubRequest*> SubRequests; typedef std::vector<StaticSubRequest*> SubRequests;
SubRequests siblings& = _static_request.get_subrequests(); SubRequests siblings& = _static_request.get_subrequests();
siblings.erase(find(siblings.begin(), siblings.end(), this); siblings.erase(find(siblings.begin(), siblings.end(), this));
} }
StaticResource& SubRequest::get_resource_key
StaticSubRequest::get_static_resource() StaticSubRequest::get_resource_key() const
{ {
return *_static_resource; return _resource_key;
} }
StaticRequest& StaticRequest&

View File

@ -23,6 +23,8 @@
#include "config.h" #include "config.h"
#include "sub_request.hh"
namespace sgpem namespace sgpem
{ {
class StaticSubRequest; class StaticSubRequest;
@ -32,16 +34,18 @@ namespace sgpem
class SG_DLLLOCAL StaticSubRequest class SG_DLLLOCAL StaticSubRequest
{ {
public: public:
typedef SubRequest::resource_key_t resource_key_t;
StaticSubRequest(StaticRequest* req, StaticSubRequest(StaticRequest* req,
StaticResource* resource, resource_key_t resource_key,
unsigned int length, unsigned int length,
unsigned int places = 1); unsigned int places = 1);
virtual ~StaticSubRequest(); virtual ~StaticSubRequest();
StaticResource& get_static_resource(); resource_key_t get_resource_key() const;
StaticRequest& get_static_request(); StaticRequest& get_request();
unsigned int get_places() const; unsigned int get_places() const;
@ -49,9 +53,10 @@ namespace sgpem
private: private:
StaticSubRequest(const StaticSubRequest&); StaticSubRequest(const StaticSubRequest&);
StaticSubRequest& operator=(const StaticSubRequest&);
StaticRequest* _static_request; StaticRequest* _static_request;
StaticResource* _static_resource; resource_key_t _resource_key;
unsigned int _length; unsigned int _length;
unsigned int _places; unsigned int _places;
}; };

View File

@ -27,6 +27,7 @@ namespace sgpem
{ {
class SubRequest; class SubRequest;
class SerializeVisitor; class SerializeVisitor;
class Request;
class Resource; class Resource;
class SG_DLLEXPORT SubRequest class SG_DLLEXPORT SubRequest
@ -38,7 +39,7 @@ namespace sgpem
virtual bool operator==(const SubRequest& op2) const = 0; virtual bool operator==(const SubRequest& op2) const = 0;
virtual resource_key_t get_resource() = 0; virtual resource_key_t get_resource_key() const = 0;
virtual unsigned int get_places() const = 0; virtual unsigned int get_places() const = 0;
@ -46,6 +47,8 @@ namespace sgpem
virtual int get_queue_position() const = 0; virtual int get_queue_position() const = 0;
virtual Request& get_request() = 0;
virtual void serialize(SerializeVisitor& translator) const = 0; virtual void serialize(SerializeVisitor& translator) const = 0;
}; };