- Correct(?) DynamicProcess::get_state()

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@672 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-06-28 16:55:00 +00:00
parent a79b4a57d7
commit d7259b8963
1 changed files with 71 additions and 26 deletions

View File

@ -21,6 +21,8 @@
#include "dynamic_process.hh" #include "dynamic_process.hh"
#include "static_process.hh" #include "static_process.hh"
#include "dynamic_thread.hh" #include "dynamic_thread.hh"
#include <algorithm>
#include <cassert> #include <cassert>
using namespace sgpem; using namespace sgpem;
@ -52,42 +54,85 @@ Schedulable::state
DynamicProcess::get_state() const DynamicProcess::get_state() const
{ {
typedef vector<DynamicThread*>::const_iterator ThreadIt; typedef vector<DynamicThread*>::const_iterator ThreadIt;
static const int uninitialized = -1;
assert(_dynamic_threads.size() > 0); assert(_dynamic_threads.size() > 0);
state result = _dynamic_threads[0]->get_state(); state result = state_future;
bool no_hole = false; int next_thread_starts_at = uninitialized;
for(ThreadIt it = _dynamic_threads.begin(); it != _dynamic_threads.end(); ++it) for(ThreadIt it = _dynamic_threads.begin(); it != _dynamic_threads.end(); ++it)
{ {
state thread_state = (*it)->get_state();
// This is the logic behind the code: // This is the logic behind the code:
// If there is at least one running or ready thread, the result is // If there is at least one running thread, the result is
// the same. If not, it may be either blocked, future or terminated. // running. If not, it may be either blocked, ready, future or terminated.
// This is guessed from the state of the first thread.
// We have three cases: // We have these cases (a state takes precedence over some other one):
// - in case the first thread is blocked, this will be the result. // (a) result starts as future
// - in case the first thread is terminated, a blocked thread will // (b) if a thread is running, return immediately state_running
// override the result and make the process blocked too. // (c) if a thread is ready, puts unconditionally result as state_ready,
// - in case the first thread is future, a blocked or terminated thread // and continue iterating (to see if there's a running thread)
// will override the result. If this isn't the case, also the absence of at least // (d) if a thread is blocked, and result is not state_ready, result
// one future thread starting at instant 0 will make the result equal to terminated. // becomes state_blocked, and continue iterating (to see if there are
// ready or running threads)
// (e) if a thread is future, and result is not state_ready or
// state_blocked, put result as state_future, and remember
// when the next thread will start (e1) (see at the end of this
// method for the rationale (e2)). Then continue iterating.
// (f) else (if all threads are state_terminated) put result as
// state_terminated.
// TODO Is this OK? Must be tested... // TODO Is this OK? Must be tested...
if((*it)->get_state() == state_running || (*it)->get_state() == state_ready) // (b)
return (*it)->get_state(); if(thread_state == state_running)
else if((*it)->get_state() == state_blocked) return state_running;
result = state_blocked;
else if(result == state_future)
{
if((*it)->get_state() == state_terminated)
result = state_terminated;
// NOTE this is not necessary, an else will equally work, but this is more clear...
else if((*it)->get_state() == state_future)
no_hole = no_hole || (*it)->get_arrival_time() == 0;
}
}
if(result == state_future && no_hole == false) // (c)
if(thread_state == state_ready)
{
result = state_ready;
continue;
}
// (d)
if(thread_state == state_blocked)
{
result = state_blocked;
continue;
}
// (e)
if(thread_state == state_future)
{
result = state_future;
int thread_starts_at = (*it)->get_arrival_time();
// (e1)
if(next_thread_starts_at == uninitialized)
next_thread_starts_at = thread_starts_at;
else
next_thread_starts_at = std::min(thread_starts_at, next_thread_starts_at);
continue;
}
// (f)
if(thread_state == state_terminated)
result = state_terminated;
} //~ "for" iterating over threads
// Now check if a "hole" happens: if result == state_future, but the next
// thread to start, e.g. the one with the least arrival_time, has
// start time greater than the current process elapsed time, then
// pass from state_future to state_terminated:
// (e2)
int elapsed_time = get_total_cpu_time() - get_remaining_time();
if(result == state_future && next_thread_starts_at > elapsed_time )
result = state_terminated; result = state_terminated;
return result; return result;