12#ifdef PXR_BASE_TF_INSTANTIATE_SINGLETON_H
13#error This file should only be included once in any given source (.cpp) file.
16#define PXR_BASE_TF_INSTANTIATE_SINGLETON_H
31PXR_NAMESPACE_OPEN_SCOPE
33struct Tf_SingletonInitState
35 const std::thread::id initThreadId;
36 void *
const initInstance;
41struct Tf_SingletonPyGILDropper
44 Tf_SingletonPyGILDropper();
46 ~Tf_SingletonPyGILDropper();
48 std::unique_ptr<class TfPyLock> _pyLock;
63 std::unique_ptr<Tf_SingletonInitState> initState(
64 new Tf_SingletonInitState {
65 std::this_thread::get_id(),
static_cast<void *
>(&instance)
68 if (_instance.load() || _initState.exchange(initState.get()) !=
nullptr) {
70 "GetInstance() or another SetInstanceConstructed() "
80 static std::atomic<bool> isInitializing;
88 if (Tf_SingletonInitState *initState = _initState.load()) {
89 if (initState->initThreadId == std::this_thread::get_id()) {
91 return static_cast<T *
>(initState->initInstance);
98 Tf_SingletonPyGILDropper dropGIL;
103 if (isInitializing.exchange(
true) ==
false) {
114 delete _initState.exchange(
nullptr);
117 T *curInst =
nullptr;
118 if (Tf_SingletonInitState *initState = _initState.load()) {
120 curInst =
static_cast<T *
>(initState->initInstance);
122 if (curInst && (curInst != newInst)) {
125 TF_AXIOM(instance.exchange(newInst) ==
nullptr);
127 isInitializing =
false;
131 std::this_thread::yield();
135 return instance.load();
144 T *instance = _instance.load();
145 if (instance && _instance.compare_exchange_strong(instance,
nullptr)) {
147 delete _initState.exchange(
nullptr);
158#define TF_INSTANTIATE_SINGLETON(T) \
159 template class PXR_NS_GLOBAL::TfSingleton<T>
162PXR_NAMESPACE_CLOSE_SCOPE
Manage a single instance of an object (see.
static void DeleteInstance()
Destroy the sole instance object of type T, if it exists.
static void SetInstanceConstructed(T &instance)
Indicate that the sole instance object has already been created.
Demangle C++ typenames generated by the typeid() facility.
Stripped down version of diagnostic.h that doesn't define std::string.
std::string ArchGetDemangled()
Return demangled RTTI generated-type name.
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
#define TF_FATAL_ERROR(fmt, args)
Issue a fatal error and end the program.
Manage a single instance of an object.