Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions xpti/src/xpti_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ XPTI_EXPORT_API void xptiFrameworkInitialize() {

XPTI_EXPORT_API void xptiFrameworkFinalize() {
if (xpti::ProxyLoader::instance().noErrors()) {
void *f = xpti::ProxyLoader::instance().functionByIndex(
XPTI_FRAMEWORK_INITIALIZE);
void *f =
xpti::ProxyLoader::instance().functionByIndex(XPTI_FRAMEWORK_FINALIZE);
if (f) {
(*reinterpret_cast<xpti_framework_initialize_t>(f))();
(*reinterpret_cast<xpti_framework_finalize_t>(f))();
}
}

Expand Down
35 changes: 32 additions & 3 deletions xptifw/src/xpti_trace_framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "xpti_int64_hash_table.hpp"
#include "xpti_string_table.hpp"

#include <atomic>
#include <cassert>
#include <cstdio>
#include <iostream>
Expand Down Expand Up @@ -976,11 +977,34 @@ class Framework {
}

static Framework &instance() {
static Framework *framework = new Framework();
return *framework;
Framework *TmpFramework = MInstance.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
if (TmpFramework == nullptr) {
std::lock_guard<utils::SpinLock> Lock{MSingletoneMutex};
TmpFramework = MInstance.load(std::memory_order_relaxed);
if (TmpFramework == nullptr) {
TmpFramework = new Framework();
std::atomic_thread_fence(std::memory_order_release);
MInstance.store(TmpFramework, std::memory_order_relaxed);
}
}

return *TmpFramework;
}

private:
friend void ::xptiFrameworkFinalize();

static Framework *release() {
Framework *TmpFramework = MInstance.load(std::memory_order_relaxed);
MInstance.store(nullptr, std::memory_order_relaxed);
return TmpFramework;
}

/// Stores singleton instance
static std::atomic<Framework *> MInstance;
/// Trivially destructible mutex for double-checked lock idiom
static utils::SpinLock MSingletoneMutex;
/// Thread-safe counter used for generating universal IDs
xpti::safe_uint64_t MUniversalIDs;
/// Manages loading the subscribers and calling their init() functions
Expand All @@ -1000,6 +1024,9 @@ class Framework {
};

static int GFrameworkReferenceCounter = 0;

std::atomic<Framework *> Framework::MInstance;
utils::SpinLock Framework::MSingletoneMutex;
} // namespace xpti

extern "C" {
Expand All @@ -1014,7 +1041,9 @@ XPTI_EXPORT_API void xptiFrameworkFinalize() {

xpti::GFrameworkReferenceCounter--;
if (xpti::GFrameworkReferenceCounter == 0) {
delete &xpti::Framework::instance();
xpti::Framework *FW = xpti::Framework::release();
if (FW)
delete FW;
}
}

Expand Down