2006-02-21 16:18:13 +01:00
|
|
|
import mutex, thread
|
2006-02-21 23:57:14 +01:00
|
|
|
import sgpem
|
2006-02-21 16:18:13 +01:00
|
|
|
|
|
|
|
_g_mutex = mutex.mutex()
|
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
## @val Synchronized return value you can read from C++
|
|
|
|
# when a threaded function returns
|
|
|
|
_ret_val = None
|
|
|
|
|
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-21 23:57:14 +01:00
|
|
|
# Instantiated in the C++ code, it should be instantiated 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
|
|
|
|
class ScriptAdapter :
|
2006-02-21 23:57:14 +01:00
|
|
|
_policy = None
|
2006-02-23 11:12:27 +01:00
|
|
|
_event = None
|
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
|
|
|
|
|
|
|
def async_configure(self):
|
2006-02-21 23:57:14 +01:00
|
|
|
_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
|
|
|
|
self._policy.configure()
|
2006-02-21 16:18:13 +01:00
|
|
|
_g_mutex.unlock()
|
|
|
|
|
2006-02-21 23:57:14 +01:00
|
|
|
def async_sort_queue(self, event):
|
2006-02-23 11:12:27 +01:00
|
|
|
self._event = event
|
|
|
|
_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):
|
|
|
|
thread.start_new_thread(ScriptAdapter._wrap_sort_queue_callback,
|
|
|
|
(self,self._event))
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-02-21 23:57:14 +01:00
|
|
|
def _wrap_sort_queue_callback(self, event):
|
|
|
|
# here we retrieve and pass the ready queue
|
|
|
|
queue = sgpem.Scheduler.get_instance().get_ready_queue()
|
|
|
|
self._policy.sort_queue(event, queue)
|
2006-02-21 16:18:13 +01:00
|
|
|
_g_mutex.unlock()
|
|
|
|
|
2006-02-23 12:29:25 +01:00
|
|
|
def async_is_preemptive(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):
|
|
|
|
_ret_val = self._policy.is_preemptive()
|
2006-02-21 16:18:13 +01:00
|
|
|
_g_mutex.unlock()
|
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
def async_get_time_slice(self):
|
|
|
|
_g_mutex.lock(ScriptAdapter._wrap_get_time_slice, self)
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
def _wrap_get_time_slice(self):
|
|
|
|
thread.start_new_thread(ScriptAdapter._wrap_get_time_slice_callback, (self,))
|
2006-02-21 16:18:13 +01:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
def _wrap_get_time_slice_callback(self):
|
|
|
|
_ret_val = self._policy.get_time_slice()
|
2006-02-21 16:18:13 +01:00
|
|
|
_g_mutex.unlock()
|