All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
instantiateSingleton.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 /*
25  * This header is not meant to be included in a .h file.
26  * Complain if we see this header twice through.
27  */
28 
29 #ifdef TF_INSTANTIATESINGLETON_H
30 #error This file should only be included once in any given source (.cpp) file.
31 #endif
32 
33 #define TF_INSTANTIATESINGLETON_H
34 
38 
39 #include "pxr/pxr.h"
40 #include "pxr/base/tf/singleton.h"
41 #include "pxr/base/tf/mallocTag.h"
42 #include "pxr/base/arch/demangle.h"
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
46 template <class T> std::mutex* TfSingleton<T>::_mutex = 0;
47 template <class T> T* TfSingleton<T>::_instance = 0;
48 
49 template <typename T>
50 T&
52 {
53  // Why is TfSingleton<T>::_mutex a pointer requiring allocation and
54  // construction and not simply an object? Because the default
55  // std::mutex c'tor on MSVC 2015 isn't constexpr . That means the
56  // mutex is dynamically initialized. That can be too late for
57  // singletons, which are often accessed via ARCH_CONSTRUCTOR()
58  // functions.
59  static std::once_flag once;
60  std::call_once(once, [](){
61  TfSingleton<T>::_mutex = new std::mutex;
62  });
63 
64  TfAutoMallocTag2 tag2("Tf", "TfSingleton::_CreateInstance");
65  TfAutoMallocTag tag("Create Singleton " + ArchGetDemangled<T>());
66 
67  std::lock_guard<std::mutex> lock(*TfSingleton<T>::_mutex);
69  ARCH_PRAGMA_PUSH
70  ARCH_PRAGMA_MAY_NOT_BE_ALIGNED
71  T *inst = new T;
72  ARCH_PRAGMA_POP
73 
74  // T's constructor could cause this to be created and set
75  // already, so guard against that.
78  }
79  }
80 
82 }
83 
84 template <typename T>
85 void
87 {
88  std::lock_guard<std::mutex> lock(*TfSingleton<T>::_mutex);
91 }
92 
100 #define TF_INSTANTIATE_SINGLETON(T) \
101  template class PXR_NS_GLOBAL::TfSingleton<T>
102 
103 
104 PXR_NAMESPACE_CLOSE_SCOPE
Manage a single instance of an object (see.
Definition: singleton.h:122
Scoped (i.e.
Definition: mallocTag.h:349
Scoped (i.e.
Definition: mallocTag.h:262