Skip to content

Commit

Permalink
Use unique_ptr and use more namespaces
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Howe <[email protected]>
  • Loading branch information
bmhowe23 committed Jan 18, 2025
1 parent bb7b874 commit 5bbd9fc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
23 changes: 17 additions & 6 deletions libs/qec/include/cudaq/qec/plugin_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,37 @@
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/

#ifndef PLUGIN_LOADER_H
#define PLUGIN_LOADER_H
#pragma once

#include <dlfcn.h>
#include <map>
#include <memory>
#include <string>

namespace cudaq::qec {

/// @brief Enum to define different types of plugins
enum class PluginType {
DECODER, // Decoder plugins
CODE // QEC codes plugins
// Add other plugin types here as needed
};

struct PluginDeleter // deleter
{
void operator()(void *h) const {
if (h)
dlclose(h);
};
};

/// @brief A struct to store plugin handle with its type
struct PluginHandle {
std::shared_ptr<void> handle; // Pointer to the shared library handle. This is
// the result of dlopen() function.
PluginType type; // Type of the plugin (e.g., decoder, code, etc)
// Pointer to the shared library handle. This is the result of dlopen()
// function.
std::unique_ptr<void, PluginDeleter> handle;
// Type of the plugin (e.g., decoder, code, etc)
PluginType type;
};

/// @brief Function to load plugins from a directory based on type
Expand All @@ -39,4 +50,4 @@ void load_plugins(const std::string &plugin_dir, PluginType type);
/// be cleaned up.
void cleanup_plugins(PluginType type);

#endif // PLUGIN_LOADER_H
} // namespace cudaq::qec
4 changes: 2 additions & 2 deletions libs/qec/lib/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ std::unique_ptr<decoder> get_decoder(const std::string &name,
const cudaqx::heterogeneous_map options) {
return decoder::get(name, H, options);
}
} // namespace cudaq::qec

// Constructor function for auto-loading plugins
__attribute__((constructor)) void load_decoder_plugins() {
Expand All @@ -89,4 +88,5 @@ __attribute__((constructor)) void load_decoder_plugins() {
__attribute__((destructor)) void cleanup_decoder_plugins() {
// Clean up decoder-specific plugins
cleanup_plugins(PluginType::DECODER);
}
}
} // namespace cudaq::qec
14 changes: 7 additions & 7 deletions libs/qec/lib/plugin_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace fs = std::filesystem;

namespace cudaq::qec {

static std::map<std::string, PluginHandle> &get_plugin_handles() {
static std::map<std::string, PluginHandle> plugin_handles;
return plugin_handles;
Expand All @@ -28,15 +30,11 @@ void load_plugins(const std::string &plugin_dir, PluginType type) {
if (entry.path().extension() == ".so") {
void *raw_handle = dlopen(entry.path().c_str(), RTLD_NOW);
if (raw_handle) {
// Custom deleter ensures dlclose is called
auto deleter = [](void *h) {
if (h)
dlclose(h);
};

get_plugin_handles().emplace(
entry.path().filename().string(),
PluginHandle{std::shared_ptr<void>(raw_handle, deleter), type});
PluginHandle{std::unique_ptr<void, PluginDeleter>(raw_handle,
PluginDeleter()),
type});
} else {
std::cerr << "ERROR: Failed to load plugin: " << entry.path()
<< " Error: " << dlerror() << std::endl;
Expand All @@ -57,3 +55,5 @@ void cleanup_plugins(PluginType type) {
}
}
}

} // namespace cudaq::qec

0 comments on commit 5bbd9fc

Please sign in to comment.