- Me again: still more simplified code for DynamicProcess::get_state().

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@673 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-06-28 17:09:46 +00:00
parent d7259b8963
commit 7aecc910ba
1 changed files with 26 additions and 43 deletions

View File

@ -70,67 +70,50 @@ DynamicProcess::get_state() const
// running. If not, it may be either blocked, ready, future or terminated. // running. If not, it may be either blocked, ready, future or terminated.
// We have these cases (a state takes precedence over some other one): // We have these cases (a state takes precedence over some other one):
// (a) result starts as future // (a) if a thread is running, return immediately state_running
// (b) if a thread is running, return immediately state_running // (b) if a thread is ready, puts unconditionally result as state_ready,
// (c) if a thread is ready, puts unconditionally result as state_ready,
// and continue iterating (to see if there's a running thread) // and continue iterating (to see if there's a running thread)
// (d) if a thread is blocked, and result is not state_ready, result // (c) if a thread is blocked, and result is not state_ready, result
// becomes state_blocked, and continue iterating (to see if there are // becomes state_blocked, and continue iterating (to see if there are
// ready or running threads) // ready or running threads)
// (e) if a thread is future, and result is not state_ready or // (d) if a thread is future, and result is not state_ready or
// state_blocked, put result as state_future, and remember // state_blocked, put result as state_future, and remember
// when the next thread will start (e1) (see at the end of this // when the next thread will start (d1) (see at the end of this
// method for the rationale (e2)). Then continue iterating. // method for the rationale (d2)). Then continue iterating.
// (f) else (if all threads are state_terminated) put result as // (e) else (if all threads are state_terminated) put result as
// state_terminated. // state_terminated.
// TODO Is this OK? Must be tested... // TODO Is this OK? Must be tested...
// (b) switch(thread_state) {
if(thread_state == state_running) case state_running: // (a)
return state_running; return state_running;
case state_ready: // (b)
// (c)
if(thread_state == state_ready)
{
result = state_ready; result = state_ready;
continue; continue;
} case state_blocked: // (c)
// (d)
if(thread_state == state_blocked)
{
result = state_blocked; result = state_blocked;
continue; continue;
} case state_future: // (d)
// (e)
if(thread_state == state_future)
{
result = state_future; result = state_future;
int thread_starts_at = (*it)->get_arrival_time(); int thread_starts_at = (*it)->get_arrival_time();
if(next_thread_starts_at == uninitialized) // (d1)
// (e1)
if(next_thread_starts_at == uninitialized)
next_thread_starts_at = thread_starts_at; next_thread_starts_at = thread_starts_at;
else else
next_thread_starts_at = std::min(thread_starts_at, next_thread_starts_at); next_thread_starts_at = std::min(thread_starts_at, next_thread_starts_at);
continue; continue;
default: // (e)
result = state_terminated;
} }
// (f)
if(thread_state == state_terminated)
result = state_terminated;
} //~ "for" iterating over threads } //~ "for" iterating over threads
// Now check if a "hole" happens: if result == state_future, but the next // 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 // thread to start, e.g. the one with the least arrival_time, has
// start time greater than the current process elapsed time, then // start time greater than the current process elapsed time, then
// pass from state_future to state_terminated: // pass from state_future to state_terminated:
// (e2) // (d2)
int elapsed_time = get_total_cpu_time() - get_remaining_time(); int elapsed_time = get_total_cpu_time() - get_remaining_time();
if(result == state_future && next_thread_starts_at > elapsed_time ) if(result == state_future && next_thread_starts_at > elapsed_time )
result = state_terminated; result = state_terminated;