#include <nnkit/CmdlineArguments.h>
#include <nnkit/VectorArguments.h>
+#include <nnkit/BackendPlugin.h>
namespace
{
}
-// TODO Extract Backend-related helpers
-#include <nnkit/Backend.h>
-
-#include <memory>
-
-#include <dlfcn.h>
-#include <assert.h>
-
-namespace
-{
-
-class BackendBinder
-{
-private:
- typedef std::unique_ptr<nnkit::Backend> (*Entry)(const nnkit::CmdlineArguments &);
-
-public:
- BackendBinder(const std::string &path)
- {
- // NOTE Some backend (such as tflite) needs multithreading support (std::thread).
- //
- // std::thread in libstdc++.so includes weak symbols for pthread_XXX functions,
- // and these weak symbols should be overridden by strong symbols in libpthread.so.
- // If not, std::thread will not work correctly.
- //
- // RTLD_GLOBAL flag is necessary to allow weak symbols to be overridden.
- _handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
- assert(_handle != nullptr);
-
- _entry = reinterpret_cast<Entry>(dlsym(_handle, "make_backend"));
- assert(_entry != nullptr);
- }
-
-public:
- // Copy is not allowed to avoid double close
- BackendBinder(const BackendBinder &) = delete;
- BackendBinder(BackendBinder &&binder)
- {
- // Handle is transferd from 'binder' instance into this instance.
- _handle = binder._handle;
- _entry = binder._entry;
-
- binder._handle = nullptr;
- binder._entry = nullptr;
- }
-
-public:
- ~BackendBinder()
- {
- if (_handle != nullptr)
- {
- dlclose(_handle);
- }
- }
-
-public:
- std::unique_ptr<nnkit::Backend> make(const nnkit::CmdlineArguments &args) const
- {
- return _entry(args);
- }
-
-private:
- void *_handle;
- Entry _entry;
-};
-
-}
-
namespace
{
class BackendSection : public Section
{
public:
- BackendSection(const std::string &path) : _binder{path}
+ BackendSection(const std::string &path) : _plugin{path}
{
// DO NOTHING
}
public:
std::unique_ptr<nnkit::Backend> initialize(void) const
{
- return _binder.make(args());
+ return _plugin.create(args());
}
private:
- BackendBinder _binder;
+ nnkit::BackendPlugin _plugin;
};
}