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