diff --git a/src/backend/concrete_environment.cc b/src/backend/concrete_environment.cc index edb36ad..fa0ee6c 100644 --- a/src/backend/concrete_environment.cc +++ b/src/backend/concrete_environment.cc @@ -66,10 +66,10 @@ ConcreteEnvironment::ConcreteEnvironment(const ConcreteEnvironment& ce) : -const Environment::ConstProcesses +const Environment::Processes& 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 { - return map(_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 { - ConstRequests request_queue; + Requests request_queue; 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(); 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); break; diff --git a/src/backend/concrete_environment.hh b/src/backend/concrete_environment.hh index a6ee378..d70903d 100644 --- a/src/backend/concrete_environment.hh +++ b/src/backend/concrete_environment.hh @@ -47,9 +47,6 @@ namespace sgpem { public: - typedef std::vector Processes; - typedef std::map Resources; - /// \brief Standard constructor. /// Builds an empty environment. ConcreteEnvironment(); @@ -69,7 +66,7 @@ namespace sgpem /// always safe. /// /// \return a constant set of snapshots of processes - virtual const ConstProcesses + virtual const Processes& get_processes() const; @@ -101,7 +98,7 @@ namespace sgpem /// /// \return a indexed constant set of snapshot of resources. /// \see DynamicSybrequest::get_resource() - virtual const ConstResources + virtual const Resources& get_resources() const; @@ -126,7 +123,7 @@ namespace sgpem /// /// \param resource The resource the requests are for /// \return The current ready requests queue. - virtual const ConstRequests + virtual const Requests get_request_queue(resource_key_t resource_key) const; diff --git a/src/backend/concrete_history.cc b/src/backend/concrete_history.cc index bdb1c3f..8e20851 100644 --- a/src/backend/concrete_history.cc +++ b/src/backend/concrete_history.cc @@ -22,6 +22,12 @@ #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_thread.hh" #include "static_resource.hh" @@ -38,6 +44,40 @@ using namespace sgpem; using namespace std; using memory::smart_ptr; + +// --------------- +// For all your evil-doers on Earth, this is your punishment! + +template +static bool deep_remove(std::vector& v, const T& obj) +{ + typedef typename std::vector 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 +static T* deep_find(const std::vector& v, const T& obj) +{ + typedef typename std::vector Vector; + for(typename Vector::const_iterator it = v.begin(); it != v.end(); it++) + if(**it == obj) + { + return *it; + } + return NULL; +} + + +// ----------------- + ConcreteHistory::ConcreteHistory() : History(), _snapshots() { @@ -85,23 +125,18 @@ ConcreteHistory::get_environment_at(position index) const void ConcreteHistory::remove(resource_key_t resource_key) { + // Pay attention that initial isn't deleted by reset() ConcreteEnvironment& initial = *_snapshots.front(); ConcreteEnvironment::Resources& resources = initial.get_resources(); ConcreteEnvironment::Resources::iterator found = resources.find(resource_key); - if(found == resources.end()) - { - notify_change(); - return; // not found, just return. Can we do this better (without a reset)? - } + if(found == resources.end()) + return; reset(false); - // FIXME: I'm really tired, check that the next two lines do - // what they're meant to do! delete found->second; resources.erase(found); - #warning "write me!" // FIXME write me : check for subrequests to remove @@ -110,47 +145,92 @@ ConcreteHistory::remove(resource_key_t resource_key) void -ConcreteHistory::remove(const Process& process) +ConcreteHistory::remove(Process& process) { - - reset(false); - + // Pay attention that initial isn't deleted by reset() ConcreteEnvironment& initial = *_snapshots.front(); ConcreteEnvironment::Processes& processes = initial.get_processes(); - ConcreteEnvironment::Processes::iterator found = find(processes.begin(), processes.end(), &process); - if(found == processes.end()) - { - 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(); + bool found = deep_remove(processes, process); + if(found) + reset(true); } void -ConcreteHistory::remove(const Thread& thread) +ConcreteHistory::remove(Thread& thread) { -#warning "write me!" + DynamicThread& dyn_thr = dynamic_cast(thread); + + // Pay attention that initial isn't deleted by reset() + ConcreteEnvironment& initial = *_snapshots.front(); + ConcreteEnvironment::Processes& processes = initial.get_processes(); + + Process* found = deep_find(processes, dyn_thr.get_process()); + + if(found == NULL) + return; // not found, just return. + + DynamicProcess& dynamic_found = dynamic_cast(*found); + bool removed = deep_remove(dynamic_found.get_dynamic_threads(), dyn_thr); + + if(removed) + reset(true); } void -ConcreteHistory::remove(const Request& request) +ConcreteHistory::remove(Request& request) { -#warning "write me!" + DynamicRequest& dyn_req = dynamic_cast(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(processes, dyn_proc); + DynamicProcess* dyn_proc_ref = dynamic_cast(proc_ref); + if(dyn_proc_ref == NULL) + return; // not found, just return. + DynamicThread* thr_ref = deep_find(dyn_proc_ref->get_dynamic_threads(), dyn_thr); + if(thr_ref == NULL) + return; // not found, just return. + + bool removed = deep_remove(thr_ref->get_dynamic_requests(), dyn_req); + + if(removed) + reset(true); } void -ConcreteHistory::remove(const SubRequest& subrequest) +ConcreteHistory::remove(SubRequest& subrequest) { -#warning "write me!" + DynamicSubRequest& dyn_sub = dynamic_cast(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(processes, dyn_proc); + DynamicProcess* dyn_proc_ref = dynamic_cast(proc_ref); + if(dyn_proc_ref == NULL) + return; // not found, just return. + DynamicThread* thr_ref = deep_find(dyn_proc_ref->get_dynamic_threads(), dyn_thr); + if(thr_ref == NULL) + return; // not found, just return. + DynamicRequest* req_ref = deep_find(thr_ref->get_dynamic_requests(), dyn_req); + if(req_ref == NULL) + return; // not found, just return. + + bool removed = deep_remove(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 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); notify_change(); + return *proc; } @@ -192,11 +283,12 @@ ConcreteHistory::add_thread(const Glib::ustring& name, // Holy cow! *THIS* is ugly!!!! DynamicProcess& parent_process = dynamic_cast(parent); - StaticProcess& parent_core = parent_process.get_core(); - StaticThread* core = new StaticThread(name, parent_core, cpu_time, arrival_time, base_priority); - DynamicThread* thread = new DynamicThread(core, &parent_process); + StaticProcess& parent_core = parent_process.get_core(); + StaticThread* core = new StaticThread(name, parent_core, cpu_time, arrival_time, base_priority); + DynamicThread* thread = new DynamicThread(core, &parent_process); notify_change(); + return *thread; } @@ -204,7 +296,18 @@ DynamicRequest& ConcreteHistory::add_request(Thread& owner, time_t instant) { -#warning "write me!" + reset(false); + + DynamicThread& dyn_owner = dynamic_cast(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, size_t places) { -#warning "write me!" + reset(false); + + DynamicRequest& dyn_request = dynamic_cast(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 ConcreteHistory::reset(bool notify) { + assert(_snapshots.size() > 0); Snapshots::iterator it = _snapshots.begin(); - ConcreteEnvironment* model = *it; - it++; // Skip first environment that we saved + for_each(it, _snapshots.end(), ptr_fun(operator delete)); - _snapshots.clear(); - _snapshots.push_back(new ConcreteEnvironment()); - - // FIXME write code to copy processes and threads and subrequests...... -#warning "write me!" - - delete model; + _snapshots.resize(1); // Truncate to keep only our "model" if(notify) notify_change(); diff --git a/src/backend/concrete_history.hh b/src/backend/concrete_history.hh index 9f0b9f6..0c83a63 100644 --- a/src/backend/concrete_history.hh +++ b/src/backend/concrete_history.hh @@ -57,10 +57,10 @@ namespace sgpem 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(const Process& process); - virtual void remove(const Thread& thread); - virtual void remove(const Request& request); - virtual void remove(const SubRequest& subrequest); + virtual void remove(Process& process); + virtual void remove(Thread& thread); + virtual void remove(Request& request); + virtual void remove(SubRequest& subrequest); virtual ResourcePair& add_resource(const Glib::ustring& name, diff --git a/src/backend/dynamic_request.cc b/src/backend/dynamic_request.cc index 0bbcf1a..5c7dd62 100644 --- a/src/backend/dynamic_request.cc +++ b/src/backend/dynamic_request.cc @@ -45,10 +45,6 @@ DynamicRequest::DynamicRequest(StaticRequest *core, DynamicRequest::~DynamicRequest() { - typedef std::vector 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)); } diff --git a/src/backend/dynamic_sub_request.cc b/src/backend/dynamic_sub_request.cc index aedd9ae..274ab43 100644 --- a/src/backend/dynamic_sub_request.cc +++ b/src/backend/dynamic_sub_request.cc @@ -29,9 +29,8 @@ using namespace sgpem; DynamicSubRequest::DynamicSubRequest(StaticSubRequest* core, - DynamicRequest* owner, - resource_key_t key) : - _static_subrequest(core), _owner(owner), _resource_key(key), + DynamicRequest* owner) : + _static_subrequest(core), _owner(owner), _queue_position(-1) { assert(core != NULL); @@ -41,9 +40,6 @@ DynamicSubRequest::DynamicSubRequest(StaticSubRequest* core, DynamicSubRequest::~DynamicSubRequest() { - typedef std::vector 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 DynamicSubRequest::get_resource_key() const { - return _resource_key; + return _static_subrequest->get_resource_key(); } unsigned int @@ -85,6 +81,14 @@ DynamicSubRequest::set_queue_position(int position) _queue_position = position; } + +DynamicRequest& +DynamicSubRequest::get_request() +{ + return *_owner; +} + + void DynamicSubRequest::serialize(SerializeVisitor& translator) const { diff --git a/src/backend/dynamic_sub_request.hh b/src/backend/dynamic_sub_request.hh index fc6f6a1..0e5ba34 100644 --- a/src/backend/dynamic_sub_request.hh +++ b/src/backend/dynamic_sub_request.hh @@ -23,6 +23,7 @@ #include "config.h" #include "sub_request.hh" +#include "dynamic_request.hh" #include "dynamic_resource.hh" #include "static_sub_request.hh" @@ -31,7 +32,6 @@ namespace sgpem { class DynamicSubRequest; - class DynamicRequest; class SerializeVisitor; class Resource; class StaticSubRequest; @@ -40,8 +40,7 @@ namespace sgpem { public: DynamicSubRequest(StaticSubRequest* core, - DynamicRequest* owner, - resource_key_t resource); + DynamicRequest* owner); virtual ~DynamicSubRequest(); @@ -56,6 +55,8 @@ namespace sgpem int get_queue_position() const; void set_queue_position(int position); + virtual DynamicRequest& get_request(); + void serialize(SerializeVisitor& translator) const; StaticSubRequest& get_core(); @@ -64,11 +65,9 @@ namespace sgpem private: memory::smart_ptr _static_subrequest; DynamicRequest* _owner; - resource_key_t _resource_key; int _queue_position; }; } #endif - diff --git a/src/backend/dynamic_thread.cc b/src/backend/dynamic_thread.cc index 5f11780..8de5736 100644 --- a/src/backend/dynamic_thread.cc +++ b/src/backend/dynamic_thread.cc @@ -54,10 +54,6 @@ DynamicThread::DynamicThread(const DynamicThread &other) : DynamicThread::~DynamicThread() { - typedef std::vector 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)); } diff --git a/src/backend/environment.hh b/src/backend/environment.hh index 49ed66b..b4dc8d8 100644 --- a/src/backend/environment.hh +++ b/src/backend/environment.hh @@ -60,9 +60,9 @@ namespace sgpem public: typedef SubRequest::resource_key_t resource_key_t; - typedef std::vector ConstProcesses; - typedef std::map ConstResources; - typedef std::vector ConstRequests; + typedef std::vector Processes; + typedef std::map Resources; + typedef std::vector Requests; /// \brief Returns an indexed set of snapshots of the processes /// Returns a standard vector of Process objects describing @@ -71,7 +71,7 @@ namespace sgpem /// /// \return a constant set of snapshots of processes - virtual const ConstProcesses + virtual const Processes& get_processes() const = 0; @@ -89,7 +89,7 @@ namespace sgpem /// /// \return an indexed constant set of snapshot of resources. - virtual const ConstResources + virtual const Resources& get_resources() const = 0; @@ -100,7 +100,7 @@ namespace sgpem /// \param resource the resource the requests are for /// \return the current ready requests queue (constant). - virtual const ConstRequests + virtual const Requests get_request_queue(resource_key_t resource_key) const = 0; diff --git a/src/backend/history.hh b/src/backend/history.hh index 52bd765..f157b3b 100644 --- a/src/backend/history.hh +++ b/src/backend/history.hh @@ -71,10 +71,10 @@ namespace sgpem 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(const Process& process) = 0; - virtual void remove(const Thread& thread) = 0; - virtual void remove(const Request& request) = 0; - virtual void remove(const SubRequest& subrequest) = 0; + virtual void remove(Process& process) = 0; + virtual void remove(Thread& thread) = 0; + virtual void remove(Request& request) = 0; + virtual void remove(SubRequest& subrequest) = 0; virtual ResourcePair& add_resource(const Glib::ustring& name, diff --git a/src/backend/static_sub_request.cc b/src/backend/static_sub_request.cc index f9fcabf..e09d145 100644 --- a/src/backend/static_sub_request.cc +++ b/src/backend/static_sub_request.cc @@ -27,10 +27,10 @@ using namespace sgpem; StaticSubRequest::StaticSubRequest(StaticRequest* req, - StaticResource* resource, + resource_key_t resource_key, unsigned int length, unsigned int places) : - _static_request(req), _static_resource(resource), + _static_request(req), _resource_key(resource_key), _length(length), _places(places) { assert(req != NULL && resource != NULL); @@ -42,13 +42,13 @@ StaticSubRequest::~StaticSubRequest() { typedef std::vector SubRequests; SubRequests siblings& = _static_request.get_subrequests(); - siblings.erase(find(siblings.begin(), siblings.end(), this); + siblings.erase(find(siblings.begin(), siblings.end(), this)); } -StaticResource& -StaticSubRequest::get_static_resource() +SubRequest::get_resource_key +StaticSubRequest::get_resource_key() const { - return *_static_resource; + return _resource_key; } StaticRequest& diff --git a/src/backend/static_sub_request.hh b/src/backend/static_sub_request.hh index 3466193..83efefa 100644 --- a/src/backend/static_sub_request.hh +++ b/src/backend/static_sub_request.hh @@ -23,6 +23,8 @@ #include "config.h" +#include "sub_request.hh" + namespace sgpem { class StaticSubRequest; @@ -32,16 +34,18 @@ namespace sgpem class SG_DLLLOCAL StaticSubRequest { public: + typedef SubRequest::resource_key_t resource_key_t; + StaticSubRequest(StaticRequest* req, - StaticResource* resource, + resource_key_t resource_key, unsigned int length, unsigned int places = 1); 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; @@ -49,9 +53,10 @@ namespace sgpem private: StaticSubRequest(const StaticSubRequest&); + StaticSubRequest& operator=(const StaticSubRequest&); StaticRequest* _static_request; - StaticResource* _static_resource; + resource_key_t _resource_key; unsigned int _length; unsigned int _places; }; diff --git a/src/backend/sub_request.hh b/src/backend/sub_request.hh index 358fc0c..38a3aa1 100644 --- a/src/backend/sub_request.hh +++ b/src/backend/sub_request.hh @@ -27,6 +27,7 @@ namespace sgpem { class SubRequest; class SerializeVisitor; + class Request; class Resource; class SG_DLLEXPORT SubRequest @@ -38,7 +39,7 @@ namespace sgpem 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; @@ -46,6 +47,8 @@ namespace sgpem virtual int get_queue_position() const = 0; + virtual Request& get_request() = 0; + virtual void serialize(SerializeVisitor& translator) const = 0; };