HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
threadLocalScopedCache.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 #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 
38 
39 /// \class ArThreadLocalScopedCache
40 ///
41 /// Utility class for custom resolver implementations. This class wraps up
42 /// a common pattern for implementing thread-local scoped caches for
43 /// ArResolver::BeginCacheScope and ArResolver::EndCacheScope.
44 ///
45 /// \code{.cpp}
46 /// class MyResolver : public ArResolver {
47 /// using ResolveCache = ArThreadLocalScopedCache<...>;
48 /// ResolveCache _cache;
49 ///
50 /// void BeginCacheScope(VtValue* data) { _cache.BeginCacheScope(data); }
51 /// void EndCacheScope(VtValue* data) { _cache.EndCacheScope(data); }
52 /// void Resolve(...) {
53 /// // If caching is active in this thread, retrieve the current
54 /// // cache and use it to lookup/store values.
55 /// if (ResolveCache::CachePtr cache = _cache.GetCurrentCache()) {
56 /// // ...
57 /// }
58 /// // Otherwise, caching is not active
59 /// // ...
60 /// }
61 /// };
62 /// \endcode
63 ///
64 /// \see \ref ArResolver_scopedCache "Scoped Resolution Cache"
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 
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 
120 
121 #endif // PXR_USD_AR_THREAD_LOCAL_SCOPED_CACHE_H
T const & UncheckedGet() const
Definition: value.h:1101
#define TF_CODING_ERROR
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1255
void BeginCacheScope(VtValue *cacheScopeData)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
bool IsHolding() const
Definition: value.h:1073
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
void EndCacheScope(VtValue *cacheScopeData)
Definition: value.h:174