diff --git a/src/backend/dynamic_process.cc b/src/backend/dynamic_process.cc index 83dd729..9ff85fb 100644 --- a/src/backend/dynamic_process.cc +++ b/src/backend/dynamic_process.cc @@ -51,9 +51,46 @@ DynamicProcess::get_threads() Schedulable::state DynamicProcess::get_state() const { -// //TODO complicated code here! leave me some time to figure -// // out what I need to do inside this method - return DynamicSchedulable::get_state(); + typedef vector::const_iterator ThreadIt; + + assert(_dynamic_threads.size() > 0); + + state result = _dynamic_threads[0]->get_state(); + bool no_hole = false; + + for(ThreadIt it = _dynamic_threads.begin(); it != _dynamic_threads.end(); ++it) + { + // 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. + // 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; + } + } + + if(result == state_future && no_hole == false) + result = state_terminated; + + return result; } void