- Added Policy::wants() and updated related code. Scheduler::get_ready_queue() still always returns a process queue...

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@615 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-06-09 16:51:53 +00:00
parent e5b90a39ad
commit 1e75fe91f1
9 changed files with 81 additions and 4 deletions

View File

@ -92,6 +92,17 @@ class Policy:
# @return 0+ To specify a time slice duration for this policy # @return 0+ To specify a time slice duration for this policy
get_time_slice = AbstractMethod('get_time_slice') get_time_slice = AbstractMethod('get_time_slice')
## @brief Returns wether this policy sorts processes or threads
#
# Should be implemented with signature:
# @code
# def wants(self):
# # function body
# @endcode
#
# @return sgpem.policy_sorts_processes If the policy sorts processes
# @return sgpem.policy_sorts_threads If the policy sorts threads
wants = AbstractMethod('wants')
## @brief Returns the PolicyParameters instance you can use in ## @brief Returns the PolicyParameters instance you can use in
# Policy::Policy::configure() # Policy::Policy::configure()

View File

@ -95,6 +95,19 @@ class ScriptAdapter :
self._ret_val = self._policy.get_time_slice() self._ret_val = self._policy.get_time_slice()
self._g_mutex.unlock() self._g_mutex.unlock()
## @brief Asynchronously call Policy.wants()
#
# @param self The caller object
def async_wants(self):
self._g_mutex.lock(ScriptAdapter._wrap_wants, self)
def _wrap_wants(self):
thread.start_new_thread(ScriptAdapter._wrap_wants_callback, (self,))
def _wrap_wants_callback(self):
self._ret_val = self._policy.wants()
self._g_mutex.unlock()
## @brief Return the global shared variable with the methods' last return value ## @brief Return the global shared variable with the methods' last return value
def get_return_value(self): def get_return_value(self):
return self._ret_val return self._ret_val

View File

@ -21,6 +21,7 @@
from Policy import Policy from Policy import Policy
import sys import sys
from sgpem import policy_sorts_processes
class fcfs(Policy) : class fcfs(Policy) :
def __init__(self): def __init__(self):
@ -35,6 +36,9 @@ class fcfs(Policy) :
def get_time_slice(self): def get_time_slice(self):
return -2 return -2
def wants(self):
return policy_sorts_processes
def sort_queue(self, queue): def sort_queue(self, queue):
cmpf = lambda a, b: \ cmpf = lambda a, b: \
a.get_schedulable().get_arrival_time() < \ a.get_schedulable().get_arrival_time() < \

View File

@ -21,6 +21,7 @@
from Policy import Policy from Policy import Policy
import sys import sys
from sgpem import policy_sorts_processes
class sjf(Policy) : class sjf(Policy) :
def __init__(self): def __init__(self):
@ -35,6 +36,9 @@ class sjf(Policy) :
def get_time_slice(self): def get_time_slice(self):
return -1 return -1
def wants(self):
return policy_sorts_processes
def sort_queue(self, queue): def sort_queue(self, queue):
cmpf = lambda a, b: \ cmpf = lambda a, b: \
a.get_cpu_time_left() < \ a.get_cpu_time_left() < \

View File

@ -138,7 +138,8 @@ PythonPolicy::is_pre_emptive() const throw(UserInterruptException)
int int
PythonPolicy::get_time_slice() const throw(UserInterruptException) { PythonPolicy::get_time_slice() const throw(UserInterruptException)
{
PyObject* retval = PyObject_CallMethod(_adapter, "async_get_time_slice", NULL); PyObject* retval = PyObject_CallMethod(_adapter, "async_get_time_slice", NULL);
Py_DECREF(retval); Py_DECREF(retval);
@ -153,6 +154,27 @@ PythonPolicy::get_time_slice() const throw(UserInterruptException) {
return tmp < 0 ? numeric_limits<int>::max() : static_cast<int>(tmp); return tmp < 0 ? numeric_limits<int>::max() : static_cast<int>(tmp);
} }
policy_sorts_type
PythonPolicy::wants() const throw(UserInterruptException)
{
PyObject* retval = PyObject_CallMethod(_adapter, "async_wants", NULL);
Py_DECREF(retval);
wait_unlock();
// Parse return value stored in global Python object
retval = PyObject_CallMethod(_adapter, "get_return_value", NULL);
assert(retval);
long tmp = PyInt_AsLong(retval);
Py_DECREF(retval);
//FIXME add the MalformedPolicyException class and throw it the else
// branch instead
if(tmp == policy_sorts_threads || tmp == policy_sorts_processes)
return static_cast<policy_sorts_type>(tmp);
else
return policy_sorts_processes;
}
void void
PythonPolicy::wait_unlock() const throw(UserInterruptException) PythonPolicy::wait_unlock() const throw(UserInterruptException)

View File

@ -74,6 +74,8 @@ namespace sgpem
*/ */
int get_time_slice() const throw(UserInterruptException); int get_time_slice() const throw(UserInterruptException);
policy_sorts_type wants() const throw(UserInterruptException);
private: private:
PythonPolicy(const PythonPolicy&); PythonPolicy(const PythonPolicy&);
PythonPolicy& operator=(const PythonPolicy&); PythonPolicy& operator=(const PythonPolicy&);

View File

@ -38,6 +38,9 @@ namespace std {
namespace sgpem { namespace sgpem {
/** Don't get worried, order is not important! */
enum policy_sorts_type { policy_sorts_threads, policy_sorts_processes };
class Policy { class Policy {
public: public:
virtual ~Policy() = 0; virtual ~Policy() = 0;

View File

@ -33,6 +33,12 @@
namespace sgpem namespace sgpem
{ {
enum policy_sorts_type
{
policy_sorts_threads,
policy_sorts_processes
};
class Policy; class Policy;
/** \brief /** \brief
@ -44,6 +50,7 @@ namespace sgpem
class SG_DLLEXPORT Policy class SG_DLLEXPORT Policy
{ {
public: public:
virtual ~Policy(); virtual ~Policy();
/** /**
@ -96,6 +103,16 @@ namespace sgpem
*/ */
virtual int get_time_slice() const throw(UserInterruptException) = 0; virtual int get_time_slice() const throw(UserInterruptException) = 0;
/**
Tell what kind of entities are scheduled by this policy.
Because it's a pure virtual method, must be re-implemented
in concrete derived classes.
\return A SortsType value identifying the desired type for the objects
composing the queue passed to the sort_queue method.
*/
virtual policy_sorts_type wants() const throw(UserInterruptException) = 0;
/** /**
Gets the parameters related with this policy. Gets the parameters related with this policy.

View File

@ -49,6 +49,7 @@ Scheduler::get_instance()
SchedulableQueue* SchedulableQueue*
Scheduler::get_ready_queue() Scheduler::get_ready_queue()
{ {
// FIXME return the correct queue accordingly to the value returned by Policy::wants()
return &_ready_queue; return &_ready_queue;
} }