- 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:
parent
390af1f09d
commit
45ef305a1b
7 changed files with 80 additions and 39 deletions
|
@ -97,7 +97,7 @@ class CPUPolicy:
|
|||
#
|
||||
# @return A sgpem::PolicyParameters instance
|
||||
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
|
||||
|
|
|
@ -29,6 +29,11 @@ using namespace std;
|
|||
|
||||
#define WAIT_FOR (250000)
|
||||
|
||||
|
||||
// Static member data
|
||||
Glib::StaticRecMutex PythonCPUPolicy::_mtx = GLIBMM_STATIC_REC_MUTEX_INIT;
|
||||
|
||||
|
||||
// WARNING : this class needs extensive and above all
|
||||
// *strong* exception checking / handling!
|
||||
|
||||
|
@ -89,19 +94,29 @@ PythonCPUPolicy::~PythonCPUPolicy()
|
|||
void
|
||||
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
|
||||
|
||||
configure();
|
||||
|
||||
set_callback_policy(NULL);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
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
|
||||
// destructor, AFTER having written activate()
|
||||
|
||||
_parameters.clear();
|
||||
|
||||
set_callback_policy(NULL);
|
||||
}
|
||||
|
||||
|
||||
|
@ -109,16 +124,24 @@ PythonCPUPolicy::deactivate()
|
|||
void
|
||||
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);
|
||||
Py_DECREF(retval);
|
||||
|
||||
wait_unlock();
|
||||
|
||||
set_callback_policy(NULL);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
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);
|
||||
|
||||
// Do minimal debugging
|
||||
|
@ -126,6 +149,8 @@ PythonCPUPolicy::sort_queue() const throw(UserInterruptException, MalformedPolic
|
|||
else Py_DECREF(retval);
|
||||
|
||||
wait_unlock();
|
||||
|
||||
set_callback_policy(NULL);
|
||||
}
|
||||
|
||||
|
||||
|
@ -145,6 +170,9 @@ PythonCPUPolicy::get_name() const
|
|||
bool
|
||||
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);
|
||||
Py_DECREF(retval);
|
||||
|
||||
|
@ -155,6 +183,8 @@ PythonCPUPolicy::is_pre_emptive() const throw(UserInterruptException, MalformedP
|
|||
assert(retval);
|
||||
bool ret = PyObject_IsTrue(retval);
|
||||
Py_DECREF(retval);
|
||||
|
||||
set_callback_policy(NULL);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -162,6 +192,9 @@ PythonCPUPolicy::is_pre_emptive() const throw(UserInterruptException, MalformedP
|
|||
int
|
||||
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);
|
||||
|
||||
// Do minimal debugging
|
||||
|
@ -176,6 +209,7 @@ PythonCPUPolicy::get_time_slice() const throw(UserInterruptException, MalformedP
|
|||
long tmp = PyInt_AsLong(retval);
|
||||
Py_DECREF(retval);
|
||||
|
||||
set_callback_policy(NULL);
|
||||
return tmp < 0 ? numeric_limits<int>::max() : static_cast<int>(tmp);
|
||||
}
|
||||
|
||||
|
@ -208,7 +242,7 @@ PythonCPUPolicy::wait_unlock() const throw(UserInterruptException, MalformedPoli
|
|||
PyEval_RestoreThread(_save);
|
||||
|
||||
if(PyErr_Occurred() != NULL)
|
||||
abort();
|
||||
abort();
|
||||
|
||||
throw UserInterruptException(_("User-defined policy is "
|
||||
"taking too long to terminate."));
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "config.h"
|
||||
|
||||
#include <Python.h>
|
||||
#include <glibmm/thread.h>
|
||||
#include <glibmm/ustring.h>
|
||||
|
||||
#include <iostream>
|
||||
|
@ -63,7 +64,7 @@ namespace sgpem
|
|||
*/
|
||||
Glib::ustring get_description() const;
|
||||
|
||||
Glib::ustring get_name() const;
|
||||
Glib::ustring get_name() const;
|
||||
|
||||
/**
|
||||
\returns \c TRUE if the policy is preemptive.
|
||||
|
@ -87,6 +88,8 @@ namespace sgpem
|
|||
void wait_unlock() const throw(UserInterruptException, MalformedPolicyException);
|
||||
static std::string get_exception_information();
|
||||
|
||||
static Glib::StaticRecMutex _mtx;
|
||||
|
||||
PyObject* _upolicy_dict;
|
||||
PyObject* _adapter;
|
||||
|
||||
|
|
|
@ -67,26 +67,10 @@ namespace sgpem {
|
|||
virtual ~CPUPolicy() = 0;
|
||||
sgpem::PolicyParameters& get_parameters();
|
||||
|
||||
%extend {
|
||||
// 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);
|
||||
};
|
||||
}
|
||||
static CPUPolicy* callback_get_policy() throw(std::runtime_error);
|
||||
};
|
||||
|
||||
// --------------------------------------------
|
||||
|
||||
// class PolicyParametersException : public std::runtime_error {
|
||||
// public:
|
||||
// PolicyParametersException(char* msg);
|
||||
// %rename (__str__) what;
|
||||
// virtual const char* what();
|
||||
// }; //~ class PolicyParametersException
|
||||
|
||||
// --------------------------------------------
|
||||
// --------------------------------------------
|
||||
class PolicyParameters
|
||||
{
|
||||
public:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue