23#ifndef _LIBQBOX_COMPONENTS_LIBQEMU_PLUGIN_H
24#define _LIBQBOX_COMPONENTS_LIBQEMU_PLUGIN_H
29#include <libqemu-cxx/libqemu-cxx.h>
31#include <scp/report.h>
32#include <cci_configuration>
37constexpr double NSEC_IN_ONE_SEC = 1e9;
50 std::atomic<bool> alive{
true };
51 std::atomic<int> refcount{ 0 };
57class LibQemuPlugin :
public sc_core::sc_module
62 cci::cci_param<uint64_t> m_id;
69 LibQemuPlugin(
const sc_core::sc_module_name& nm,
qemu::LibQemu& inst)
70 : sc_module(nm), m_inst(inst), m_id(
"id", 0,
"qemu plugin id"), m_handle(
new PluginHandle())
72 m_handle->self =
this;
80 void push_plugin_args(
const std::string& plugin_path)
82 SCP_DEBUG(())(
"push_plugin_args, key: {} ", m_id.name());
83 SCP_DEBUG(())(
"push_plugin_args, plugin_path: {} ", plugin_path);
84 std::stringstream opts;
86 opts <<
",key=" << m_id.name();
87 m_inst.push_qemu_arg(
"-plugin");
88 m_inst.push_qemu_arg(opts.str().c_str());
100 if (!m_handle->alive.exchange(
false, std::memory_order_seq_cst)) {
103 while (m_handle->refcount.load(std::memory_order_acquire) > 0) {
104 std::this_thread::yield();
109 void* handle_as_userdata()
const {
return m_handle; }
126 template <
typename Body>
130 if (!h || !h->alive.load(std::memory_order_acquire))
return;
131 h->refcount.fetch_add(1, std::memory_order_seq_cst);
132 if (h->alive.load(std::memory_order_seq_cst)) {
135 h->refcount.fetch_sub(1, std::memory_order_seq_cst);
Base class for QBox C++ plugins that bridge QEMU plugin callbacks.
Definition libqemu-plugin.h:58
static void dispatch_userdata(void *userdata, Body body)
Run body on the live plugin instance, holding a refcount for the duration. No-op when the handle is n...
Definition libqemu-plugin.h:127
void shutdown_bridge()
Stop bridge dispatch and drain in-flight callbacks.
Definition libqemu-plugin.h:98
Definition libqemu-cxx.h:95
Stable userdata passed to every plugin callback.
Definition libqemu-plugin.h:48