2006-08-10 02:42:17 +02:00
|
|
|
import sys, mutex, thread
|
2006-02-21 23:57:14 +01:00
|
|
|
import sgpem
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-02-21 23:57:14 +01:00
|
|
|
## @brief This is an adapter class which acts as a proxy
|
|
|
|
# for user-implemented policies
|
2006-02-21 16:18:13 +01:00
|
|
|
#
|
2006-02-21 23:57:14 +01:00
|
|
|
# At runtime, this class will be initialized with the
|
|
|
|
# user-implemented policy, and then it will proxy the calls
|
|
|
|
# the policy member functions on a different thread, so
|
|
|
|
# to ensure asyncronous control.
|
2006-02-21 16:18:13 +01:00
|
|
|
#
|
2006-02-23 19:55:14 +01:00
|
|
|
# Instantiated in the C++ code, it is created like:
|
2006-02-21 16:18:13 +01:00
|
|
|
# @code
|
2006-02-21 23:57:14 +01:00
|
|
|
# adapter = ScriptAdapter(UserPolicyClass)
|
2006-02-21 16:18:13 +01:00
|
|
|
# @endcode
|
2006-02-23 19:55:14 +01:00
|
|
|
#
|
|
|
|
# The user shouldn't care about this class at all.
|
2006-02-21 16:18:13 +01:00
|
|
|
class ScriptAdapter :
|
2006-02-23 19:55:14 +01:00
|
|
|
## @var The policy this ScriptAdapter will use for calls
|
2006-02-21 23:57:14 +01:00
|
|
|
_policy = None
|
2006-02-23 19:55:14 +01:00
|
|
|
|
2006-03-10 15:54:24 +01:00
|
|
|
## @var Synchronized return value you can read from C++
|
|
|
|
# when a threaded function returns
|
|
|
|
_ret_val = None
|
2006-02-23 19:55:14 +01:00
|
|
|
|
2006-03-10 15:54:24 +01:00
|
|
|
## Var Testable syncronization object
|
|
|
|
_g_mutex = mutex.mutex()
|
2006-02-23 19:55:14 +01:00
|
|
|
|
2006-08-10 02:42:17 +02:00
|
|
|
_g_last_exception = None
|
|
|
|
|
2006-02-23 19:55:14 +01:00
|
|
|
## @brief Constructor of ScriptAdapter
|
|
|
|
#
|
|
|
|
# @param self The caller object
|
|
|
|
# @param policy A user-implemented class inheriting from Policy.Policy
|
2006-02-21 23:57:14 +01:00
|
|
|
def __init__(self, policy):
|
|
|
|
self._policy = policy()
|
|
|
|
print 'ScriptAdapter for policy ', policy, ' loaded'
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-02-23 19:55:14 +01:00
|
|
|
## @brief Asynchronously call Policy.configure()
|
|
|
|
#
|
|
|
|
# @param self The caller object
|
2006-02-21 16:18:13 +01:00
|
|
|
def async_configure(self):
|
2006-08-10 02:42:17 +02:00
|
|
|
self._g_last_exception = None
|
|
|
|
self._g_mutex.lock(ScriptAdapter._wrap_configure, self )
|
2006-02-21 16:18:13 +01:00
|
|
|
|
|
|
|
def _wrap_configure(self):
|
2006-02-21 23:57:14 +01:00
|
|
|
thread.start_new_thread(ScriptAdapter._wrap_configure_callback, (self,))
|
2006-02-22 16:16:08 +01:00
|
|
|
|
2006-02-21 16:18:13 +01:00
|
|
|
def _wrap_configure_callback(self):
|
2006-02-21 23:57:14 +01:00
|
|
|
# call configure method
|
2006-08-10 02:42:17 +02:00
|
|
|
try:
|
|
|
|
self._policy.configure()
|
|
|
|
except:
|
|
|
|
self._g_last_exception = sys.exc_value
|
|
|
|
self._g_mutex.unlock()
|
2006-02-23 19:55:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
## @brief Asynchronously call Policy.sort_queue()
|
|
|
|
#
|
|
|
|
# The queue is asked directly to the C++ sgpem::Scheduler
|
|
|
|
# singleton, via SWIG
|
|
|
|
#
|
|
|
|
# @param self The caller object
|
2006-06-03 17:51:38 +02:00
|
|
|
def async_sort_queue(self):
|
2006-08-10 02:42:17 +02:00
|
|
|
self._g_last_exception = None
|
2006-03-10 15:54:24 +01:00
|
|
|
self._g_mutex.lock(ScriptAdapter._wrap_sort_queue, self)
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-02-23 11:12:27 +01:00
|
|
|
def _wrap_sort_queue(self):
|
2006-06-03 17:51:38 +02:00
|
|
|
thread.start_new_thread(ScriptAdapter._wrap_sort_queue_callback, (self,))
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-06-03 17:51:38 +02:00
|
|
|
def _wrap_sort_queue_callback(self):
|
2006-02-21 23:57:14 +01:00
|
|
|
# here we retrieve and pass the ready queue
|
|
|
|
queue = sgpem.Scheduler.get_instance().get_ready_queue()
|
2006-08-10 02:42:17 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
self._policy.sort_queue(queue)
|
|
|
|
except:
|
|
|
|
self._g_last_exception = sys.exc_value
|
2006-03-10 15:54:24 +01:00
|
|
|
self._g_mutex.unlock()
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-02-23 19:55:14 +01:00
|
|
|
|
|
|
|
## @brief Asynchronously call Policy.is_preemptive()
|
|
|
|
#
|
|
|
|
# @param self The caller object
|
2006-02-23 12:29:25 +01:00
|
|
|
def async_is_preemptive(self):
|
2006-08-10 02:42:17 +02:00
|
|
|
self._g_last_exception = None
|
2006-03-10 16:21:44 +01:00
|
|
|
self._g_mutex.lock(ScriptAdapter._wrap_is_preemptive, self)
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-02-23 12:29:25 +01:00
|
|
|
def _wrap_is_preemptive(self):
|
|
|
|
thread.start_new_thread(ScriptAdapter._wrap_is_preemptive_callback, (self,))
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-02-23 12:29:25 +01:00
|
|
|
def _wrap_is_preemptive_callback(self):
|
2006-08-10 02:42:17 +02:00
|
|
|
try:
|
|
|
|
self._ret_val = self._policy.is_preemptive()
|
|
|
|
except:
|
|
|
|
self._g_last_exception = sys.exc_value
|
2006-03-10 15:54:24 +01:00
|
|
|
self._g_mutex.unlock()
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-07-22 16:47:39 +02:00
|
|
|
## @brief Asynchronously call Policy.get_time_slice()
|
|
|
|
#
|
|
|
|
# @param self The caller object
|
|
|
|
def async_get_time_slice(self):
|
2006-08-10 02:42:17 +02:00
|
|
|
self._g_last_exception = None
|
2006-07-22 16:47:39 +02:00
|
|
|
self._g_mutex.lock(ScriptAdapter._wrap_get_time_slice, self)
|
|
|
|
|
|
|
|
def _wrap_get_time_slice(self):
|
|
|
|
thread.start_new_thread(ScriptAdapter._wrap_get_time_slice_callback, (self,))
|
|
|
|
|
|
|
|
def _wrap_get_time_slice_callback(self):
|
2006-08-10 02:42:17 +02:00
|
|
|
try:
|
|
|
|
self._ret_val = self._policy.get_time_slice()
|
|
|
|
except:
|
|
|
|
self._g_last_exception = sys.exc_value
|
2006-07-22 16:47:39 +02:00
|
|
|
self._g_mutex.unlock()
|
|
|
|
|
2006-03-10 15:54:24 +01:00
|
|
|
## @brief Return the global shared variable with the methods' last return value
|
|
|
|
def get_return_value(self):
|
|
|
|
return self._ret_val
|
|
|
|
|
2006-03-10 16:21:44 +01:00
|
|
|
def mutex_test_lock(self):
|
2006-03-10 15:54:24 +01:00
|
|
|
return self._g_mutex.test()
|
2006-08-10 02:42:17 +02:00
|
|
|
|
|
|
|
def get_last_exception(self):
|
|
|
|
return self._g_last_exception
|
|
|
|
|