sgpemv2/src/backend/pyloader/ScriptAdapter.py
tchernobog a2a492b5d5 - Previous segmentation fault was due to a faulty call (wrong parameters
to ScriptAdapter.__wrap_sort_queue()). Now prints an error and check
for retval before Py_DECREF'ing it.
- Fix(?) qsort implementation in Policy
- SWIG doesn't understand "uint". Change into more verbose (but surely
standard) unsigned int


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@394 3ecf2c5c-341e-0410-92b4-d18e462d057c
2006-02-23 10:12:27 +00:00

73 lines
2.3 KiB
Python

import mutex, thread
import sgpem
_g_mutex = mutex.mutex()
## @val Synchronized return value you can read from C++
# when a threaded function returns
_ret_val = None
## @brief This is an adapter class which acts as a proxy
# for user-implemented policies
#
# 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.
#
# Instantiated in the C++ code, it should be instantiated like:
# @code
# adapter = ScriptAdapter(UserPolicyClass)
# @endcode
class ScriptAdapter :
_policy = None
_event = None
def __init__(self, policy):
self._policy = policy()
print 'ScriptAdapter for policy ', policy, ' loaded'
def async_configure(self):
_g_mutex.lock(ScriptAdapter._wrap_configure, self )
def _wrap_configure(self):
thread.start_new_thread(ScriptAdapter._wrap_configure_callback, (self,))
def _wrap_configure_callback(self):
# call configure method
self._policy.configure()
_g_mutex.unlock()
def async_sort_queue(self, event):
self._event = event
_g_mutex.lock(ScriptAdapter._wrap_sort_queue, self)
def _wrap_sort_queue(self):
thread.start_new_thread(ScriptAdapter._wrap_sort_queue_callback,
(self,self._event))
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)
_g_mutex.unlock()
def async_is_preemptible(self):
_g_mutex.lock(ScriptAdapter._wrap_is_preemptible, self)
def _wrap_is_preemptible(self):
thread.start_new_thread(ScriptAdapter._wrap_is_preemptible_callback, (self,))
def _wrap_is_preemptible_callback(self):
_ret_val = self._policy.is_preemptible()
_g_mutex.unlock()
def async_get_time_slice(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):
_ret_val = self._policy.get_time_slice()
_g_mutex.unlock()