All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
threadLocalScopedCache.h
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 #ifndef AR_THREAD_LOCAL_SCOPED_CACHE_H
25 #define AR_THREAD_LOCAL_SCOPED_CACHE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/ar/api.h"
29 
30 #include "pxr/base/vt/value.h"
31 
32 #include <tbb/enumerable_thread_specific.h>
33 #include <memory>
34 #include <vector>
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
64 template <class CachedType>
66 {
67 public:
68  using CachePtr = std::shared_ptr<CachedType>;
69 
70  void BeginCacheScope(VtValue* cacheScopeData)
71  {
72  // Since this is intended to be used by ArResolver implementations,
73  // we expect cacheScopeData to never be NULL and to either be empty
74  // or holding a cache pointer that we've filled in previously.
75  if (!cacheScopeData ||
76  (!cacheScopeData->IsEmpty() &&
77  !cacheScopeData->IsHolding<CachePtr>())) {
78  TF_CODING_ERROR("Unexpected cache scope data");
79  return;
80  }
81 
82  _CachePtrStack& cacheStack = _threadCacheStack.local();
83  if (cacheScopeData->IsHolding<CachePtr>()) {
84  cacheStack.push_back(cacheScopeData->UncheckedGet<CachePtr>());
85  }
86  else {
87  if (cacheStack.empty()) {
88  cacheStack.push_back(std::make_shared<CachedType>());
89  }
90  else {
91  cacheStack.push_back(cacheStack.back());
92  }
93  }
94  *cacheScopeData = cacheStack.back();
95  }
96 
97  void EndCacheScope(VtValue* cacheScopeData)
98  {
99  _CachePtrStack& cacheStack = _threadCacheStack.local();
100  if (TF_VERIFY(!cacheStack.empty())) {
101  cacheStack.pop_back();
102  }
103  }
104 
105  CachePtr GetCurrentCache()
106  {
107  _CachePtrStack& cacheStack = _threadCacheStack.local();
108  return (cacheStack.empty() ? CachePtr() : cacheStack.back());
109  }
110 
111 private:
112  using _CachePtrStack = std::vector<CachePtr>;
113  using _ThreadLocalCachePtrStack =
114  tbb::enumerable_thread_specific<_CachePtrStack>;
115  _ThreadLocalCachePtrStack _threadCacheStack;
116 };
117 
118 PXR_NAMESPACE_CLOSE_SCOPE
119 
120 #endif // AR_THREAD_LOCAL_SCOPED_CACHE_H
T const & UncheckedGet() const
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:836
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:87
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:808
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:289
Utility class for custom resolver implementations.
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:990
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182