From a2a492b5d53619caeec0d504629bfbbc73007a91 Mon Sep 17 00:00:00 2001 From: tchernobog Date: Thu, 23 Feb 2006 10:12:27 +0000 Subject: [PATCH] - 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 --- src/backend/pyloader/Policy.py | 20 ++++++++++---------- src/backend/pyloader/ScriptAdapter.py | 11 ++++++----- src/backend/pyloader/python_policy.cc | 5 +++-- src/backend/schedulable_list.hh | 14 +++++++------- src/backend/sgpem.i | 4 ++-- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/backend/pyloader/Policy.py b/src/backend/pyloader/Policy.py index c1431cf..f9f7a7e 100644 --- a/src/backend/pyloader/Policy.py +++ b/src/backend/pyloader/Policy.py @@ -1,4 +1,4 @@ -from Abstract import * +from Abstract import * ## @brief This is the abstract class a user-defined policy # should inherit from @@ -28,8 +28,8 @@ class Policy: # @param queue The SchedulableQueue to be sorted in place # @param cmpf The binary function to use to compare elements # @returns None - def sort(queue, cmpf): - __recursive_qsort_(queue, 0, queue.size()-1, cmpf) + def sort(self, queue, cmpf): + self.__recursive_qsort(queue, 0, queue.size()-1, cmpf) ## @brief Recursive (private) call to perform quicksort on a # queue @@ -39,12 +39,12 @@ class Policy: # @param b The final element position of the slice # @param cmpf The user-defined compare function to employ # @returns None - def __recursive_qsort_(queue, a, b, cmpf): + def __recursive_qsort(self, queue, a, b, cmpf): if(b>a): - pivot = __partition_(queue, a, b, cmpf) - __recursive_qsort_(queue, a, pivot-1, cmpf) - __recursive_qsort_(queue, pivot+1, b, cmpf) - + pivot = self.__partition(queue, a, b, cmpf) + self.__recursive_qsort(queue, a, pivot-1, cmpf) + self.__recursive_qsort(queue, pivot+1, b, cmpf) + ## @brief Recursive (private) call to partition a slice of the queue # # This private function (the name mangling should work) @@ -58,11 +58,11 @@ class Policy: # @param b The partition ending element position in the queue # @param cmpf The binary function to use for comparing two elements # @return The new pivot index - def __partition_(queue, a, b, cmpf): + def __partition_(self, queue, a, b, cmpf): # takes pivot element: right = queue.get_item_at(b) i = a - for j in range(a,b-1): + for j in range(a,b): # goes from a to b-1 if cmpf(queue.get_item_at(j), right): # the C++ code should do nothing if i == j: queue.swap(i,j) diff --git a/src/backend/pyloader/ScriptAdapter.py b/src/backend/pyloader/ScriptAdapter.py index 0dbb632..623f2ec 100644 --- a/src/backend/pyloader/ScriptAdapter.py +++ b/src/backend/pyloader/ScriptAdapter.py @@ -21,15 +21,14 @@ _ret_val = None # @endcode class ScriptAdapter : _policy = None + _event = None def __init__(self, policy): self._policy = policy() print 'ScriptAdapter for policy ', policy, ' loaded' def async_configure(self): - print 'Calling ', self._policy, ' configure() method.' _g_mutex.lock(ScriptAdapter._wrap_configure, self ) - print 'Returning from configure() method' def _wrap_configure(self): thread.start_new_thread(ScriptAdapter._wrap_configure_callback, (self,)) @@ -40,10 +39,12 @@ class ScriptAdapter : _g_mutex.unlock() def async_sort_queue(self, event): - _g_mutex.lock(ScriptAdapter._wrap_sort_queue, (self, event)) + self._event = event + _g_mutex.lock(ScriptAdapter._wrap_sort_queue, self) - def _wrap_sort_queue(self, event): - thread.start_new_thread(ScriptAdapter._wrap_sort_queue_callback, (self, event)) + 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 diff --git a/src/backend/pyloader/python_policy.cc b/src/backend/pyloader/python_policy.cc index aae5edd..3847b56 100644 --- a/src/backend/pyloader/python_policy.cc +++ b/src/backend/pyloader/python_policy.cc @@ -19,6 +19,7 @@ // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "python_policy.hh" +#include #include using namespace sgpem; using namespace std; @@ -103,8 +104,8 @@ PythonPolicy::sort_queue(Scheduler::event event) const PyObject* pEvent = PyInt_FromLong(event); PyObject* pMethodName = PyString_FromString("async_sort_queue"); PyObject* retval = PyObject_CallMethodObjArgs(_adapter, pMethodName, pEvent, NULL); - // Why we haven't to decref this? - // Py_DECREF(retval); + if(!retval) PyErr_Print(); + else Py_DECREF(retval); Py_DECREF(pMethodName); Py_DECREF(pEvent); diff --git a/src/backend/schedulable_list.hh b/src/backend/schedulable_list.hh index fa5e6a8..bf06488 100644 --- a/src/backend/schedulable_list.hh +++ b/src/backend/schedulable_list.hh @@ -40,13 +40,13 @@ namespace sgpem bool operator==(const SchedulableList&) const; bool has_same_objects(const SchedulableList& dx) const; - sgpem::SchedulableStatus* top(); - sgpem::SchedulableStatus* bottom(); - void add_at_bottom(const sgpem::SchedulableStatus&); - void add_at_top(const sgpem::SchedulableStatus&); - memory::smart_ptr remove(const uint& position); - bool insert_at(const uint&, const uint&); - uint size() const; + SchedulableStatus* top(); + SchedulableStatus* bottom(); + void add_at_bottom(const SchedulableStatus&); + void add_at_top(const SchedulableStatus&); + memory::smart_ptr remove(const unsigned int& position); + bool insert_at(const unsigned int&, const unsigned int&); + unsigned int size() const; SchedulableStatus* get_item_at(const uint&); const SchedulableStatus* get_item_at(const uint&) const; void clear(); diff --git a/src/backend/sgpem.i b/src/backend/sgpem.i index 71227d5..9779c03 100644 --- a/src/backend/sgpem.i +++ b/src/backend/sgpem.i @@ -126,8 +126,8 @@ namespace sgpem { class SchedulableList { public: - uint size() const; - const SchedulableStatus* get_item_at(const uint&) const; + unsigned int size() const; + const SchedulableStatus* get_item_at(const unsigned int&) const; void swap(unsigned int positionA, unsigned int positionB) throw(); private: