- Finish implementing async setup for Python policies' access

- Correct method names for the Python policy (design problem?)
- Compiles, but can't run it on this machine. I'll test it later
(and I expect quite a lot of SIGSEGVs).


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@382 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-02-22 15:16:08 +00:00
parent 882a6acf5e
commit 8f655f2f69
8 changed files with 211 additions and 96 deletions

View file

@ -19,10 +19,159 @@
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "python_policy.hh"
#include <unistd.h>
using namespace sgpem;
using namespace std;
PythonPolicy::PythonPolicy()
// WARNING : this class needs extensive and above all
// *strong* exception checking / handling!
PythonPolicy::PythonPolicy(const char* name)
: _adapter(NULL), _adapter_dict(NULL),
_lock(NULL), _name(name)
{
PyObject* pLoadmeStr = PyString_FromString(name);
PyObject* pUserPolicyModule = PyImport_Import(pLoadmeStr);
Py_DECREF(pLoadmeStr);
if( !pUserPolicyModule )
{
PyErr_Print(); // Error in import
// FIXME : don't exit abruptly, but fall back gracefully
exit(-1);
}
// Dictionary with defined ``symbols'' for .pyc file
PyObject* pUserPolicyDict = PyModule_GetDict(pUserPolicyModule);
assert(pUserPolicyDict);
// Loads ScriptAdapter module and get its dictionary
pLoadmeStr = PyString_FromString("ScriptAdapter");
PyObject* pScriptAdapterModule = PyImport_Import(pLoadmeStr);
Py_DECREF(pLoadmeStr);
assert(pScriptAdapterModule);
_adapter_dict = PyModule_GetDict(pScriptAdapterModule);
assert(_adapter_dict);
// We want to keep a reference to it even if we decref
// its containing module
Py_INCREF(_adapter_dict);
// Now takes the user-defined policy class from pUserPolicyDict
PyObject* pPolicyClass = PyDict_GetItemString(pUserPolicyDict, name);
assert(pPolicyClass); // FIXME needs stricter checking and exception throwing
// Save a reference to the global lock
_lock = PyDict_GetItemString(_adapter_dict, "_g_mutex");
assert(_lock);
// Creates a new object of type ScriptAdapter :
// takes init function from ScriptAdapter class
PyObject* pAdapterClass = PyDict_GetItemString(_adapter_dict, "ScriptAdapter");
PyObject* pAdapterCtorParam = PyTuple_New(1);
Py_INCREF(pPolicyClass); // PyTuple_SetItem steals a reference
PyTuple_SetItem(pAdapterCtorParam, 0, pPolicyClass);
_adapter = PyInstance_New(pAdapterClass, pAdapterCtorParam, NULL);
Py_DECREF(pAdapterCtorParam);
assert(_adapter);
Py_DECREF(pUserPolicyModule);
Py_DECREF(pScriptAdapterModule);
// And now, who's your daddy, huh?
}
PythonPolicy::~PythonPolicy()
{
if(_adapter) Py_DECREF(_adapter);
if(_adapter_dict) Py_DECREF(_adapter_dict);
}
void
PythonPolicy::configure()
{
PyObject* retval = PyObject_CallMethod(_adapter, "async_configure", NULL);
Py_DECREF(retval);
wait_unlock();
}
void
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);
Py_DECREF(retval);
Py_DECREF(pMethodName);
Py_DECREF(pEvent);
wait_unlock();
}
Glib::ustring
PythonPolicy::get_description() const
{
return _name;
}
bool
PythonPolicy::is_pre_emptive() const {
PyObject* retval = PyObject_CallMethod(_adapter, "async_is_preemptive", NULL);
Py_DECREF(retval);
wait_unlock();
// Parse return value stored in global Python object
retval = PyDict_GetItemString(_adapter_dict, "_ret_val");
return static_cast<bool>(PyInt_AsLong(retval));
}
int
PythonPolicy::get_time_slice() const {
PyObject* retval = PyObject_CallMethod(_adapter, "async_get_time_slice", NULL);
Py_DECREF(retval);
wait_unlock();
// Parse return value stored in global Python object
retval = PyDict_GetItemString(_adapter_dict, "_ret_val");
return static_cast<int>(PyInt_AsLong(retval));
}
void
PythonPolicy::set_time_slice(const int&)
{
throw "The design is wrong, and I shouldn't be called.";
}
void
PythonPolicy::wait_unlock() const
{
PyThreadState *_save;
Py_UNBLOCK_THREADS
sleep(5); // hack'a'ton! magggggiccc nummmbeeerrrrrs!!
// What we should really do here:
/* do {
enable python threads
wait for some time...
disable python threads
...check if user asked for interruption, reading a
syncronized variable...
...if he has, break
...else:
if the global lock is set:
stay in this loop
else:
all's went okay, can exit loop
} */
Py_BLOCK_THREADS
}