CB_END,
} cb_type;
-#define Py_LOCK() PyGILState_Ensure ()
-#define Py_UNLOCK(gstate) PyGILState_Release (gstate)
-
/**
* @brief Python embedding core structure
*/
~PYCore ();
int init (const GstTensorFilterProperties *prop);
- int loadScript ();
const char *getScriptPath ();
int getInputTensorDim (GstTensorsInfo *info);
int getOutputTensorDim (GstTensorsInfo *info);
return callback_type;
}
- int checkTensorType (int nns_type, int np_type);
- int checkTensorSize (GstTensorMemory *output, PyArrayObject *array);
-
private:
+ int loadScript ();
const std::string script_path; /**< from model_path property */
const std::string module_args; /**< from custom property */
bool configured; /**< True if the script is successfully loaded */
void *handle; /**< returned handle by dlopen() */
+ GMutex py_mutex;
+
+ int checkTensorSize (GstTensorMemory *output, PyArrayObject *array);
+ int checkTensorType (int nns_type, int np_type);
+
+ /** @brief Lock python-related actions */
+ void Py_LOCK ()
+ {
+ g_mutex_lock (&py_mutex);
+ }
+ /** @brief Unlock python-related actions */
+ void Py_UNLOCK ()
+ {
+ g_mutex_unlock (&py_mutex);
+ }
};
#ifdef __cplusplus
core_obj = NULL;
configured = false;
shape_cls = NULL;
+ g_mutex_init (&py_mutex);
}
/**
gst_tensors_info_free (&inputTensorMeta);
gst_tensors_info_free (&outputTensorMeta);
- PyGILState_STATE gstate = Py_LOCK ();
+ Py_LOCK ();
Py_SAFEDECREF (core_obj);
Py_SAFEDECREF (shape_cls);
PyErr_Clear ();
- Py_UNLOCK (gstate);
+ Py_UNLOCK ();
dlclose (handle);
}
int
PYCore::init (const GstTensorFilterProperties *prop)
{
- PyGILState_STATE gstate = Py_LOCK ();
int ret = -EINVAL;
/** Find nnstreamer_api module */
+ Py_LOCK ();
PyObject *api_module = PyImport_ImportModule ("nnstreamer_python");
if (api_module == NULL) {
Py_ERRMSG ("Cannt find `nnstreamer_python` module");
ret = loadScript ();
exit:
- Py_UNLOCK (gstate);
+ Py_UNLOCK ();
return ret;
}
int
PYCore::loadScript ()
{
+ /** This is a private method that needs the lock kept locked */
#if (DBG)
gint64 start_time = g_get_real_time ();
#endif
int ret = -EINVAL;
- PyGILState_STATE gstate = Py_LOCK ();
PyObject *module = PyImport_ImportModule (module_name.c_str ());
if (module) {
ret = 0;
exit:
- Py_UNLOCK (gstate);
return ret;
}
int
PYCore::checkTensorSize (GstTensorMemory *output, PyArrayObject *array)
{
+ /** This is a private method that needs the lock kept locked */
if (nullptr == output || nullptr == array)
throw std::invalid_argument ("Null pointers are given to PYCore::checkTensorSize().\n");
- PyGILState_STATE gstate = Py_LOCK ();
size_t total_size = PyArray_ITEMSIZE (array);
for (int i = 0; i < PyArray_NDIM (array); i++)
total_size *= PyArray_DIM (array, i);
- Py_UNLOCK (gstate);
-
return (output->size == total_size);
}
if (nullptr == info)
throw std::invalid_argument ("A null pointer is given to PYCore::getInputTensorDim().\n");
- PyGILState_STATE gstate = Py_LOCK ();
+ Py_LOCK ();
PyObject *result = PyObject_CallMethod (core_obj, (char *) "getInputDim", NULL);
if (result) {
res = -1;
}
- Py_UNLOCK (gstate);
+ Py_UNLOCK ();
return res;
}
if (nullptr == info)
throw std::invalid_argument ("A null pointer is given to PYCore::getOutputTensorDim().\n");
- PyGILState_STATE gstate = Py_LOCK ();
+ Py_LOCK ();
PyObject *result = PyObject_CallMethod (core_obj, (char *) "getOutputDim", NULL);
if (result) {
res = -1;
}
- Py_UNLOCK (gstate);
+ Py_UNLOCK ();
return res;
}
if (nullptr == in_info || nullptr == out_info)
throw std::invalid_argument ("Null pointers are given to PYCore::setInputTensorDim().\n");
- PyGILState_STATE gstate = Py_LOCK ();
+ Py_LOCK ();
/** to Python list object */
PyObject *param = PyList_New (in_info->num_tensors);
- if (nullptr == param)
+ if (nullptr == param) {
+ Py_UNLOCK ();
throw std::runtime_error ("PyList_New(); has failed.");
+ }
for (unsigned int i = 0; i < in_info->num_tensors; i++) {
PyObject *shape;
_info = gst_tensors_info_get_nth_info ((GstTensorsInfo *) in_info, i);
shape = PyTensorShape_New (shape_cls, _info);
- if (nullptr == shape)
+ if (nullptr == shape) {
+ Py_UNLOCK ();
throw std::runtime_error ("PyTensorShape_New(); has failed.");
+ }
PyList_SetItem (param, i, shape);
}
res = -1;
}
- Py_UNLOCK (gstate);
+ Py_UNLOCK ();
return res;
}
PYCore::freeOutputTensors (void *data)
{
std::map<void *, PyArrayObject *>::iterator it;
- PyGILState_STATE gstate = Py_LOCK ();
it = outputArrayMap.find (data);
if (it != outputArrayMap.end ()) {
} else {
ml_loge ("Cannot find output data: 0x%lx", (unsigned long) data);
}
-
- Py_UNLOCK (gstate);
}
/**
if (nullptr == output || nullptr == input)
throw std::invalid_argument ("Null pointers are given to PYCore::run().\n");
- PyGILState_STATE gstate = Py_LOCK ();
+ Py_LOCK ();
PyObject *param = PyList_New (inputTensorMeta.num_tensors);
for (unsigned int i = 0; i < inputTensorMeta.num_tensors; i++) {
exit_decref:
Py_SAFEDECREF (param);
- Py_UNLOCK (gstate);
+ Py_UNLOCK ();
#if (DBG)
gint64 stop_time = g_get_real_time ();
g_return_val_if_fail (input, -EINVAL);
g_return_val_if_fail (output, -EINVAL);
- return core->run (input, output);
+ PyGILState_STATE gstate = PyGILState_Ensure ();
+ int ret = core->run (input, output);
+ PyGILState_Release (gstate);
+ return ret;
}
/**
PYCore *core = static_cast<PYCore *> (*private_data);
if (core) {
+ PyGILState_STATE gstate = PyGILState_Ensure ();
core->freeOutputTensors (data);
+ PyGILState_Release (gstate);
}
}
return -ENOENT;
}
- return core->setInputTensorDim (in_info, out_info);
+ PyGILState_STATE gstate = PyGILState_Ensure ();
+ int ret = core->setInputTensorDim (in_info, out_info);
+ PyGILState_Release (gstate);
+ return ret;
}
/**
return -ENOENT;
}
- return core->getInputTensorDim (info);
+ PyGILState_STATE gstate = PyGILState_Ensure ();
+ int ret = core->getInputTensorDim (info);
+ PyGILState_Release (gstate);
+ return ret;
}
/**
return -ENOENT;
}
- return core->getOutputTensorDim (info);
+ PyGILState_STATE gstate = PyGILState_Ensure ();
+ int ret = core->getOutputTensorDim (info);
+ PyGILState_Release (gstate);
+ return ret;
}
/**
UNUSED (prop);
g_return_if_fail (core != NULL);
+ PyGILState_STATE gstate = PyGILState_Ensure ();
delete core;
+ PyGILState_Release (gstate);
*private_data = NULL;
}
{
if (!Py_IsInitialized ())
throw std::runtime_error ("Python is not initialize.");
+ /* Do not acquire GIL. py_loadScriptFile will do it */
int status = py_loadScriptFile (prop, private_data);
return status;