- Incapsulate code so that the hack we previously used in CPUPoliciesGatekeeper isn't needed anymore

- Now CPUPolicy has a callback method for scripting languages, but it is up to derived classes to take
mutexes and set the value when needed (maybe we can improve this?)


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@862 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-08-14 14:28:41 +00:00
parent 390af1f09d
commit 45ef305a1b
7 changed files with 80 additions and 39 deletions

View File

@ -97,7 +97,7 @@ class CPUPolicy:
# #
# @return A sgpem::PolicyParameters instance # @return A sgpem::PolicyParameters instance
def get_parameters(self): def get_parameters(self):
return sgpem.CPUPolicy.get_active_policy().get_parameters() return sgpem.CPUPolicy.callback_get_policy().get_parameters()
## @brief This function implements an in-place stable sort ## @brief This function implements an in-place stable sort

View File

@ -29,6 +29,11 @@ using namespace std;
#define WAIT_FOR (250000) #define WAIT_FOR (250000)
// Static member data
Glib::StaticRecMutex PythonCPUPolicy::_mtx = GLIBMM_STATIC_REC_MUTEX_INIT;
// WARNING : this class needs extensive and above all // WARNING : this class needs extensive and above all
// *strong* exception checking / handling! // *strong* exception checking / handling!
@ -89,19 +94,29 @@ PythonCPUPolicy::~PythonCPUPolicy()
void void
PythonCPUPolicy::activate() throw(UserInterruptException, MalformedPolicyException) PythonCPUPolicy::activate() throw(UserInterruptException, MalformedPolicyException)
{ {
Glib::RecMutex::Lock lock(_mtx);;
set_callback_policy(const_cast<PythonCPUPolicy*>(this));
// FIXME write the rest, taking away code from constructor // FIXME write the rest, taking away code from constructor
configure(); configure();
set_callback_policy(NULL);
} }
void void
PythonCPUPolicy::deactivate() PythonCPUPolicy::deactivate()
{ {
Glib::RecMutex::Lock lock(_mtx);;
set_callback_policy(const_cast<PythonCPUPolicy*>(this));
// FIXME See if some code has to be moved here from the // FIXME See if some code has to be moved here from the
// destructor, AFTER having written activate() // destructor, AFTER having written activate()
_parameters.clear(); _parameters.clear();
set_callback_policy(NULL);
} }
@ -109,16 +124,24 @@ PythonCPUPolicy::deactivate()
void void
PythonCPUPolicy::configure() throw(UserInterruptException, MalformedPolicyException) PythonCPUPolicy::configure() throw(UserInterruptException, MalformedPolicyException)
{ {
Glib::RecMutex::Lock lock(_mtx);;
set_callback_policy(const_cast<PythonCPUPolicy*>(this));
PyObject* retval = PyObject_CallMethod(_adapter, "async_configure", NULL); PyObject* retval = PyObject_CallMethod(_adapter, "async_configure", NULL);
Py_DECREF(retval); Py_DECREF(retval);
wait_unlock(); wait_unlock();
set_callback_policy(NULL);
} }
void void
PythonCPUPolicy::sort_queue() const throw(UserInterruptException, MalformedPolicyException) PythonCPUPolicy::sort_queue() const throw(UserInterruptException, MalformedPolicyException)
{ {
Glib::RecMutex::Lock lock(_mtx);;
set_callback_policy(const_cast<PythonCPUPolicy*>(this));
PyObject* retval = PyObject_CallMethod(_adapter, "async_sort_queue", NULL); PyObject* retval = PyObject_CallMethod(_adapter, "async_sort_queue", NULL);
// Do minimal debugging // Do minimal debugging
@ -126,6 +149,8 @@ PythonCPUPolicy::sort_queue() const throw(UserInterruptException, MalformedPolic
else Py_DECREF(retval); else Py_DECREF(retval);
wait_unlock(); wait_unlock();
set_callback_policy(NULL);
} }
@ -145,6 +170,9 @@ PythonCPUPolicy::get_name() const
bool bool
PythonCPUPolicy::is_pre_emptive() const throw(UserInterruptException, MalformedPolicyException) PythonCPUPolicy::is_pre_emptive() const throw(UserInterruptException, MalformedPolicyException)
{ {
Glib::RecMutex::Lock lock(_mtx);;
set_callback_policy(const_cast<PythonCPUPolicy*>(this));
PyObject* retval = PyObject_CallMethod(_adapter, "async_is_preemptive", NULL); PyObject* retval = PyObject_CallMethod(_adapter, "async_is_preemptive", NULL);
Py_DECREF(retval); Py_DECREF(retval);
@ -155,6 +183,8 @@ PythonCPUPolicy::is_pre_emptive() const throw(UserInterruptException, MalformedP
assert(retval); assert(retval);
bool ret = PyObject_IsTrue(retval); bool ret = PyObject_IsTrue(retval);
Py_DECREF(retval); Py_DECREF(retval);
set_callback_policy(NULL);
return ret; return ret;
} }
@ -162,6 +192,9 @@ PythonCPUPolicy::is_pre_emptive() const throw(UserInterruptException, MalformedP
int int
PythonCPUPolicy::get_time_slice() const throw(UserInterruptException, MalformedPolicyException) PythonCPUPolicy::get_time_slice() const throw(UserInterruptException, MalformedPolicyException)
{ {
Glib::RecMutex::Lock lock(_mtx);;
set_callback_policy(const_cast<PythonCPUPolicy*>(this));
PyObject* retval = PyObject_CallMethod(_adapter, "async_get_time_slice", NULL); PyObject* retval = PyObject_CallMethod(_adapter, "async_get_time_slice", NULL);
// Do minimal debugging // Do minimal debugging
@ -176,6 +209,7 @@ PythonCPUPolicy::get_time_slice() const throw(UserInterruptException, MalformedP
long tmp = PyInt_AsLong(retval); long tmp = PyInt_AsLong(retval);
Py_DECREF(retval); Py_DECREF(retval);
set_callback_policy(NULL);
return tmp < 0 ? numeric_limits<int>::max() : static_cast<int>(tmp); return tmp < 0 ? numeric_limits<int>::max() : static_cast<int>(tmp);
} }

View File

@ -24,6 +24,7 @@
#include "config.h" #include "config.h"
#include <Python.h> #include <Python.h>
#include <glibmm/thread.h>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <iostream> #include <iostream>
@ -87,6 +88,8 @@ namespace sgpem
void wait_unlock() const throw(UserInterruptException, MalformedPolicyException); void wait_unlock() const throw(UserInterruptException, MalformedPolicyException);
static std::string get_exception_information(); static std::string get_exception_information();
static Glib::StaticRecMutex _mtx;
PyObject* _upolicy_dict; PyObject* _upolicy_dict;
PyObject* _adapter; PyObject* _adapter;

View File

@ -67,24 +67,8 @@ namespace sgpem {
virtual ~CPUPolicy() = 0; virtual ~CPUPolicy() = 0;
sgpem::PolicyParameters& get_parameters(); sgpem::PolicyParameters& get_parameters();
%extend { static CPUPolicy* callback_get_policy() throw(std::runtime_error);
// Convenience function to get the current policy
static CPUPolicy* get_active_policy()
{
History& h = Simulation::get_instance().get_history();
return CPUPoliciesGatekeeper::get_instance().get_current_policy(&h);
}; };
}
};
// --------------------------------------------
// class PolicyParametersException : public std::runtime_error {
// public:
// PolicyParametersException(char* msg);
// %rename (__str__) what;
// virtual const char* what();
// }; //~ class PolicyParametersException
// -------------------------------------------- // --------------------------------------------
class PolicyParameters class PolicyParameters

View File

@ -118,25 +118,11 @@ CPUPoliciesGatekeeper::activate_policy(History *history, CPUPolicy* policy) thro
try try
{ {
// policy->activate() needs already an active policy, because:
// * it calls the configure() method on the
// insert-your-favourite-scripting-language-here-policy
// * which in turn calls the configure() method in the
// code written by the user
// * which probably uses Simulation to get the _active_ C++ policy,
// so it can get its policy_parameters()
// ... so **DON'T** play Mr. Clever Dick and swap the following two
// lines in an optimization frenzy! Or the user policy WILL fail.
_active_policies[history] = policy;
policy->activate(); policy->activate();
_active_policies[history] = policy;
} }
catch(const CPUPolicyException& e) catch(const CPUPolicyException& e)
{ {
//std::cerr << e.what() << std::endl;
// See the comment above to understand why we do this
// in this way
_active_policies.erase(_active_policies.find(history));
// the caller need to know if it failed // the caller need to know if it failed
throw; throw;
} }

View File

@ -22,6 +22,11 @@
using namespace std; using namespace std;
using namespace sgpem; using namespace sgpem;
// Static member data
CPUPolicy* CPUPolicy::_callback_policy = NULL;
CPUPolicy::~CPUPolicy() CPUPolicy::~CPUPolicy()
{} {}
@ -31,3 +36,20 @@ CPUPolicy::get_parameters()
{ {
return _parameters; return _parameters;
} }
CPUPolicy*
CPUPolicy::callback_get_policy() throw(std::runtime_error)
{
if(_callback_policy == NULL)
throw std::runtime_error("CPUPolicy::callback_get_policy() not used as a callback method. NULL ptr returned.");
return _callback_policy;
}
void
CPUPolicy::set_callback_policy(CPUPolicy* ptr)
{
_callback_policy = ptr;
}

View File

@ -30,6 +30,8 @@
#include "user_interrupt_exception.hh" #include "user_interrupt_exception.hh"
#include "malformed_policy_exception.hh" #include "malformed_policy_exception.hh"
#include <stdexcept>
namespace sgpem namespace sgpem
{ {
class CPUPolicy; class CPUPolicy;
@ -102,8 +104,18 @@ namespace sgpem
*/ */
PolicyParameters& get_parameters(); PolicyParameters& get_parameters();
/** This method is used only as a callback by scripting languages */
static CPUPolicy* callback_get_policy() throw(std::runtime_error);
protected: protected:
PolicyParameters _parameters; PolicyParameters _parameters;
static void set_callback_policy(CPUPolicy* ptr = NULL);
private:
// Used by callback_get_policy:
static CPUPolicy* _callback_policy;
}; };
}//~ namespace sgpem }//~ namespace sgpem