/** Python script filename. */
const char* fname;
+ /** Python main thread */
+ PyThreadState* mainthr;
/** Python module. */
PyObject* module;
/* Initialize module */
FILE* script_py = NULL;
PyObject* py_cfg, *res;
+ PyGILState_STATE gil;
struct pythonmod_env* pe = (struct pythonmod_env*)calloc(1, sizeof(struct pythonmod_env));
if (!pe)
{
Py_NoSiteFlag = 1;
Py_Initialize();
PyEval_InitThreads();
- PyEval_ReleaseLock();
SWIG_init();
+ pe->mainthr = PyEval_SaveThread();
}
- PyEval_AcquireLock();
+ gil = PyGILState_Ensure();
/* Initialize Python */
PyRun_SimpleString("import sys \n");
if (PyRun_SimpleString("from unboundmodule import *\n") < 0)
{
log_err("pythonmod: cannot initialize core module: unboundmodule.py");
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
return 0;
}
if ((script_py = fopen(pe->fname, "r")) == NULL)
{
log_err("pythonmod: can't open file %s for reading", pe->fname);
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
return 0;
}
if (PyRun_SimpleFile(script_py, pe->fname) < 0)
{
log_err("pythonmod: can't parse Python script %s", pe->fname);
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
return 0;
}
if ((pe->func_init = PyDict_GetItemString(pe->dict, "init")) == NULL)
{
log_err("pythonmod: function init is missing in %s", pe->fname);
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
return 0;
}
if ((pe->func_deinit = PyDict_GetItemString(pe->dict, "deinit")) == NULL)
{
log_err("pythonmod: function deinit is missing in %s", pe->fname);
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
return 0;
}
if ((pe->func_operate = PyDict_GetItemString(pe->dict, "operate")) == NULL)
{
log_err("pythonmod: function operate is missing in %s", pe->fname);
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
return 0;
}
if ((pe->func_inform = PyDict_GetItemString(pe->dict, "inform_super")) == NULL)
{
log_err("pythonmod: function inform_super is missing in %s", pe->fname);
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
return 0;
}
Py_XDECREF(res);
Py_XDECREF(py_cfg);
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
return 1;
}
if(pe->module != NULL)
{
PyObject* res;
+ PyGILState_STATE gil = PyGILState_Ensure();
/* Deinit module */
- PyEval_AcquireLock();
res = PyObject_CallFunction(pe->func_deinit, "i", id);
if (PyErr_Occurred()) {
log_err("pythonmod: Exception occurred in function deinit");
Py_XDECREF(res);
/* Free shared data if any */
Py_XDECREF(pe->data);
+ PyGILState_Release(gil);
+ PyEval_RestoreThread(pe->mainthr);
Py_Finalize();
+ pe->mainthr = NULL;
}
pe->fname = NULL;
free(pe);
struct pythonmod_env* pe = (struct pythonmod_env*)qstate->env->modinfo[id];
struct pythonmod_qstate* pq = (struct pythonmod_qstate*)qstate->minfo[id];
PyObject* py_qstate, *py_sqstate, *res;
+ PyGILState_STATE gil = PyGILState_Ensure();
log_query_info(VERB_ALGO, "pythonmod: inform_super, sub is", &qstate->qinfo);
log_query_info(VERB_ALGO, "super is", &super->qinfo);
py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0);
py_sqstate = SWIG_NewPointerObj((void*) super, SWIGTYPE_p_module_qstate, 0);
- PyEval_AcquireLock();
res = PyObject_CallFunction(pe->func_inform, "iOOO", id, py_qstate,
py_sqstate, pq->data);
Py_XDECREF(py_sqstate);
Py_XDECREF(py_qstate);
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
}
void pythonmod_operate(struct module_qstate* qstate, enum module_ev event,
struct pythonmod_env* pe = (struct pythonmod_env*)qstate->env->modinfo[id];
struct pythonmod_qstate* pq = (struct pythonmod_qstate*)qstate->minfo[id];
PyObject* py_qstate, *res;
+ PyGILState_STATE gil = PyGILState_Ensure();
if ( pq == NULL)
{
Py_INCREF(pq->data);
}
- /* Lock Python */
- PyEval_AcquireLock();
-
/* Call operate */
py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0);
res = PyObject_CallFunction(pe->func_operate, "iiOO", id, (int) event,
Py_XDECREF(res);
Py_XDECREF(py_qstate);
- /* Unlock Python */
- PyEval_ReleaseLock();
+ PyGILState_Release(gil);
}
void pythonmod_clear(struct module_qstate* qstate, int id)
(unsigned long int)pq);
if(pq != NULL)
{
+ PyGILState_STATE gil = PyGILState_Ensure();
Py_DECREF(pq->data);
+ PyGILState_Release(gil);
/* Free qstate */
free(pq);
}