This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
threadLocalScopedCache.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_AR_THREAD_LOCAL_SCOPED_CACHE_H
8#define PXR_USD_AR_THREAD_LOCAL_SCOPED_CACHE_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/ar/api.h"
12
13#include "pxr/base/vt/value.h"
15
16#include <tbb/enumerable_thread_specific.h>
17#include <memory>
18#include <vector>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
48template <class CachedType>
50{
51public:
52 using CachePtr = std::shared_ptr<CachedType>;
53
54 void BeginCacheScope(VtValue* cacheScopeData)
55 {
56 // Since this is intended to be used by ArResolver implementations,
57 // we expect cacheScopeData to never be NULL and to either be empty
58 // or holding a cache pointer that we've filled in previously.
59 if (!cacheScopeData ||
60 (!cacheScopeData->IsEmpty() &&
61 !cacheScopeData->IsHolding<CachePtr>())) {
62 TF_CODING_ERROR("Unexpected cache scope data");
63 return;
64 }
65
66 _CachePtrStack& cacheStack = _threadCacheStack.local();
67 if (cacheScopeData->IsHolding<CachePtr>()) {
68 cacheStack.push_back(cacheScopeData->UncheckedGet<CachePtr>());
69 }
70 else {
71 if (cacheStack.empty()) {
72 cacheStack.push_back(std::make_shared<CachedType>());
73 }
74 else {
75 cacheStack.push_back(cacheStack.back());
76 }
77 }
78 *cacheScopeData = cacheStack.back();
79 }
80
81 void EndCacheScope(VtValue* cacheScopeData)
82 {
83 _CachePtrStack& cacheStack = _threadCacheStack.local();
84 if (TF_VERIFY(!cacheStack.empty())) {
85 cacheStack.pop_back();
86 }
87 }
88
89 CachePtr GetCurrentCache()
90 {
91 _CachePtrStack& cacheStack = _threadCacheStack.local();
92 return (cacheStack.empty() ? CachePtr() : cacheStack.back());
93 }
94
95private:
96 using _CachePtrStack = std::vector<CachePtr>;
97 using _ThreadLocalCachePtrStack =
98 tbb::enumerable_thread_specific<_CachePtrStack>;
99 _ThreadLocalCachePtrStack _threadCacheStack;
100};
101
102PXR_NAMESPACE_CLOSE_SCOPE
103
104#endif // PXR_USD_AR_THREAD_LOCAL_SCOPED_CACHE_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Utility class for custom resolver implementations.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:147
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1285
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1064
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1104
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:68
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266