- 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:
parent
a79b4a57d7
commit
d7259b8963
|
@ -21,6 +21,8 @@
|
|||
#include "dynamic_process.hh"
|
||||
#include "static_process.hh"
|
||||
#include "dynamic_thread.hh"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
using namespace sgpem;
|
||||
|
@ -52,42 +54,85 @@ Schedulable::state
|
|||
DynamicProcess::get_state() const
|
||||
{
|
||||
typedef vector<DynamicThread*>::const_iterator ThreadIt;
|
||||
static const int uninitialized = -1;
|
||||
|
||||
assert(_dynamic_threads.size() > 0);
|
||||
|
||||
state result = _dynamic_threads[0]->get_state();
|
||||
bool no_hole = false;
|
||||
state result = state_future;
|
||||
int next_thread_starts_at = uninitialized;
|
||||
|
||||
for(ThreadIt it = _dynamic_threads.begin(); it != _dynamic_threads.end(); ++it)
|
||||
{
|
||||
state thread_state = (*it)->get_state();
|
||||
|
||||
// This is the logic behind the code:
|
||||
// If there is at least one running or ready thread, the result is
|
||||
// the same. If not, it may be either blocked, future or terminated.
|
||||
// This is guessed from the state of the first thread.
|
||||
// We have three cases:
|
||||
// - in case the first thread is blocked, this will be the result.
|
||||
// - in case the first thread is terminated, a blocked thread will
|
||||
// override the result and make the process blocked too.
|
||||
// - in case the first thread is future, a blocked or terminated thread
|
||||
// will override the result. If this isn't the case, also the absence of at least
|
||||
// one future thread starting at instant 0 will make the result equal to terminated.
|
||||
// If there is at least one running thread, the result is
|
||||
// running. If not, it may be either blocked, ready, future or terminated.
|
||||
|
||||
// We have these cases (a state takes precedence over some other one):
|
||||
// (a) result starts as future
|
||||
// (b) if a thread is running, return immediately state_running
|
||||
// (c) if a thread is ready, puts unconditionally result as state_ready,
|
||||
// and continue iterating (to see if there's a running thread)
|
||||
// (d) if a thread is blocked, and result is not state_ready, result
|
||||
// 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...
|
||||
|
||||
if((*it)->get_state() == state_running || (*it)->get_state() == state_ready)
|
||||
return (*it)->get_state();
|
||||
else if((*it)->get_state() == state_blocked)
|
||||
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;
|
||||
}
|
||||
}
|
||||
// (b)
|
||||
if(thread_state == state_running)
|
||||
return state_running;
|
||||
|
||||
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;
|
||||
|
||||
return result;
|
||||
|
|
Loading…
Reference in New Issue