- "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

@ -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<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()
: 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<Process>(processes, process);
if(found)
reset(true);
}
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
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
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 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<DynamicProcess&>(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<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,
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
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();