Commit d02d0c99 authored by Yixing Lao's avatar Yixing Lao Committed by Scott Cyphers

register plugin with Manager class (#318)

parent 588d69a4
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <dlfcn.h> #include <dlfcn.h>
#include <iostream> #include <iostream>
#include <mutex>
#include <sstream> #include <sstream>
#include <string> #include <string>
...@@ -23,57 +24,73 @@ ...@@ -23,57 +24,73 @@
using namespace ngraph::runtime; using namespace ngraph::runtime;
static std::mutex load_plugins_mutex;
static std::mutex close_plugins_mutex;
bool Manager::m_is_factory_map_initialized = false; bool Manager::m_is_factory_map_initialized = false;
std::shared_ptr<std::vector<void*>> Manager::m_plugin_lib_handles = std::vector<void*> Manager::m_plugin_handles = {};
std::make_shared<std::vector<void*>>(std::vector<void*>());
void Manager::load_plugins(const std::string& runtime_plugin_libs) void Manager::load_plugins(const std::string& runtime_plugin_libs)
{ {
std::vector<std::string> plugin_lib_paths = ngraph::split(runtime_plugin_libs, ':', false); std::lock_guard<std::mutex> lock(load_plugins_mutex);
for (auto plugin_lib_path : plugin_lib_paths) if (Manager::m_is_factory_map_initialized)
{ {
if (plugin_lib_path.size() > 0) return;
}
std::vector<std::string> plugin_paths = ngraph::split(runtime_plugin_libs, ':', false);
for (auto plugin_path : plugin_paths)
{
if (plugin_path.size() > 0)
{ {
void* lib_handle = dlopen(plugin_lib_path.c_str(), RTLD_NOW); void* plugin_handle = dlopen(plugin_path.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (lib_handle) if (plugin_handle)
{ {
Manager::m_plugin_lib_handles->push_back(lib_handle); void (*register_plugin)() =
reinterpret_cast<void (*)()>(dlsym(plugin_handle, "register_plugin"));
if (register_plugin != NULL)
{
register_plugin();
Manager::m_plugin_handles.push_back(plugin_handle);
}
else
{
throw ngraph_error("register_plugin() not found in " + plugin_path);
}
} }
else else
{ {
throw ngraph_error("Cannot open library " + plugin_lib_path); throw ngraph_error("Cannot open library " + plugin_path);
} }
} }
} }
Manager::m_is_factory_map_initialized = true;
} }
// TODO: Should call this function after plugin is not needed anymore. // TODO: Should call this function after plugin is not needed anymore.
void Manager::close_plugins() void Manager::close_plugins()
{ {
for (auto lib_handle : *Manager::m_plugin_lib_handles) std::lock_guard<std::mutex> lock(close_plugins_mutex);
for (auto plugin_handle : Manager::m_plugin_handles)
{ {
dlclose(lib_handle); dlclose(plugin_handle);
} }
Manager::m_plugin_lib_handles->clear(); Manager::m_plugin_handles.clear();
} }
Manager::FactoryMap& Manager::get_factory_map() Manager::FactoryMap& Manager::get_factory_map()
{ {
// Stores Manager Factories // Stores Manager Factories
static FactoryMap factory_map; static FactoryMap factory_map;
// Try to load runtime plugins
if (!Manager::m_is_factory_map_initialized)
{
Manager::load_plugins(RUNTIME_PLUGIN_LIBS);
Manager::m_is_factory_map_initialized = true;
}
return factory_map; return factory_map;
} }
std::shared_ptr<Manager> Manager::get(const std::string& name) std::shared_ptr<Manager> Manager::get(const std::string& name)
{ {
Manager::load_plugins(RUNTIME_PLUGIN_LIBS);
return get_factory_map().at(name)(name); return get_factory_map().at(name)(name);
} }
......
...@@ -57,7 +57,7 @@ namespace ngraph ...@@ -57,7 +57,7 @@ namespace ngraph
static void close_plugins(); static void close_plugins();
static std::shared_ptr<std::vector<void*>> m_plugin_lib_handles; static std::vector<void*> m_plugin_handles;
static bool m_is_factory_map_initialized; static bool m_is_factory_map_initialized;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment