make singleton thread safe
This commit is contained in:
@@ -7,9 +7,11 @@
|
||||
|
||||
namespace pywrap {
|
||||
PyWrap* PyWrap::wrapper = nullptr;
|
||||
std::mutex PyWrap::mutex;
|
||||
|
||||
PyWrap* PyWrap::GetInstance()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
if (wrapper == nullptr) {
|
||||
std::cout << "Creando instancia" << std::endl;
|
||||
wrapper = new PyWrap();
|
||||
@@ -35,15 +37,15 @@ namespace pywrap {
|
||||
return;
|
||||
}
|
||||
std::cout << "No estaba en el mapa" << std::endl;
|
||||
module = PyImport_ImportModule(moduleName.c_str());
|
||||
PyObject* module = PyImport_ImportModule(moduleName.c_str());
|
||||
if (PyErr_Occurred()) {
|
||||
errorAbort("Could't import module " + moduleName);
|
||||
}
|
||||
classObject = PyObject_GetAttrString(module, className.c_str());
|
||||
PyObject* classObject = PyObject_GetAttrString(module, className.c_str());
|
||||
if (PyErr_Occurred()) {
|
||||
errorAbort("Couldn't find class " + className);
|
||||
}
|
||||
instance = PyObject_CallObject(classObject, NULL);
|
||||
PyObject* instance = PyObject_CallObject(classObject, NULL);
|
||||
if (PyErr_Occurred()) {
|
||||
errorAbort("Couldn't create instance of class " + className);
|
||||
}
|
||||
@@ -89,7 +91,7 @@ namespace pywrap {
|
||||
errorAbort("Module " + moduleName + " and class " + className + " not found");
|
||||
}
|
||||
std::cout << "Clase encontrada" << std::endl;
|
||||
instance = std::get<2>(item->second);
|
||||
PyObject* instance = std::get<2>(item->second);
|
||||
PyObject* result;
|
||||
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
|
||||
errorAbort("Couldn't call method " + method);
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
#include <mutex>
|
||||
|
||||
namespace pywrap {
|
||||
/*
|
||||
@@ -24,10 +25,8 @@ namespace pywrap {
|
||||
void errorAbort(const std::string& message);
|
||||
PyStatus initPython();
|
||||
static PyWrap* wrapper;
|
||||
static std::mutex mutex;
|
||||
std::map<std::pair<std::string, std::string>, std::tuple<PyObject*, PyObject*, PyObject*>> moduleClassMap;
|
||||
PyObject* module;
|
||||
PyObject* classObject;
|
||||
PyObject* instance;
|
||||
};
|
||||
} /* namespace python */
|
||||
#endif /* PYWRAP_H */
|
Reference in New Issue
Block a user