7
7
#include " xpti_int64_hash_table.hpp"
8
8
#include " xpti_string_table.hpp"
9
9
10
+ #include < atomic>
10
11
#include < cassert>
11
12
#include < cstdio>
12
13
#include < iostream>
@@ -976,11 +977,24 @@ class Framework {
976
977
}
977
978
978
979
static Framework &instance () {
979
- static Framework *framework = new Framework ();
980
- return *framework;
980
+ Framework *TmpFramework = MInstance.load (std::memory_order_relaxed);
981
+ std::atomic_thread_fence (std::memory_order_acquire);
982
+ if (TmpFramework == nullptr ) {
983
+ std::lock_guard<utils::SpinLock> Lock{MSingletoneMutex};
984
+ TmpFramework = MInstance.load (std::memory_order_relaxed);
985
+ if (TmpFramework == nullptr ) {
986
+ TmpFramework = new Framework ();
987
+ std::atomic_thread_fence (std::memory_order_release);
988
+ MInstance.store (TmpFramework, std::memory_order_relaxed);
989
+ }
990
+ }
991
+
992
+ return *TmpFramework;
981
993
}
982
994
983
995
private:
996
+ static std::atomic<Framework *> MInstance;
997
+ static utils::SpinLock MSingletoneMutex;
984
998
// / Thread-safe counter used for generating universal IDs
985
999
xpti::safe_uint64_t MUniversalIDs;
986
1000
// / Manages loading the subscribers and calling their init() functions
@@ -1000,6 +1014,9 @@ class Framework {
1000
1014
};
1001
1015
1002
1016
static int GFrameworkReferenceCounter = 0 ;
1017
+
1018
+ std::atomic<Framework *> Framework::MInstance;
1019
+ utils::SpinLock Framework::MSingletoneMutex;
1003
1020
} // namespace xpti
1004
1021
1005
1022
extern " C" {
0 commit comments