HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_CappedCache.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 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  \file
26  \brief
27 */
28 #ifndef _GUSD_UT_CAPPEDCACHE_H_
29 #define _GUSD_UT_CAPPEDCACHE_H_
30 
31 #include "pxr/pxr.h"
32 
33 #include <SYS/SYS_AtomicInt.h>
34 #include <UT/UT_Assert.h>
35 #include <UT/UT_CappedCache.h>
37 #include <UT/UT_IntrusivePtr.h>
38 
40 
41 /** Convenience wrapper around UT_CappedKey.
42  This allows keys to be constructed in TBB's style.
43  These can only be used in a UT_CappedCache if all keys
44  in the cache have the same type.*/
45 template <typename KeyT,
46  typename HashCompare=tbb::tbb_hash_compare<KeyT> >
48 {
49 public:
51  GusdUT_CappedKey(const KeyT& key) : UT_CappedKey(), _key(key) {}
52 
53  ~GusdUT_CappedKey() override {}
54 
55  UT_CappedKey* duplicate() const override
56  { return new GusdUT_CappedKey(_key); }
57 
58  unsigned getHash() const override
59  { return HashCompare().hash(_key); }
60 
61  bool isEqual(const UT_CappedKey& key) const override
62  {
63  return HashCompare().equal(_key,
64  UTverify_cast<const GusdUT_CappedKey*>(
65  &key)->_key);
66  }
67 
68  KeyT* operator->() { return &_key; }
69  const KeyT* operator->() const { return &_key; }
70  KeyT& operator*() { return _key; }
71  const KeyT& operator*() const { return _key; }
72 
73 private:
74  KeyT _key;
75 };
76 
77 
78 /** Variant of UT_CappedCache that improves on item construction.
79  This adds in a mechanism for locking items during construction,
80  to prevent multiple threads from performing the same work to
81  initialize cache items.*/
83 {
84 public:
85  GusdUT_CappedCache(const char* name, int64 size_in_mb=32)
86  : UT_CappedCache(name, size_in_mb) {}
87  ~GusdUT_CappedCache() override {}
88 
89  template <typename Item>
91 
92  template <typename Item,typename Creator,typename... Args>
94  const Creator& creator,
95  Args&... args);
96 
97  template <typename T>
98  bool FindVal(const UT_CappedKey& key);
99 
100  template <typename T,typename Creator,typename... Args>
101  bool FindOrCreateVal(const UT_CappedKey& key,
102  const Creator& creator,
103  Args&... args);
104 
105  template <typename MatchFn>
106  int64 ClearEntries(const MatchFn& matchFn);
107 
108 private:
109 
110  struct _HashCompare
111  {
112  static size_t hash(const UT_CappedKeyHandle& k)
113  { return (size_t)k->getHash(); }
114  static bool equal(const UT_CappedKeyHandle& a,
115  const UT_CappedKeyHandle& b)
116  { return a->isEqual(*b); }
117  };
118 
119 
122  _HashCompare> _ConstructMap;
123  _ConstructMap _constructMap;
124 };
125 
126 
127 template <typename Item>
130 {
131  if(UT_CappedItemHandle hnd = findItem(key))
133  UTverify_cast<const Item*>(hnd.get()));
135 }
136 
137 
138 template <typename Item, typename Creator, typename... Args>
141  const Creator& creator,
142  Args&... args)
143 {
144  if(auto item = Find<Item>(key))
145  return item;
146 
147  UT_CappedKeyHandle keyHnd(key.duplicate());
148 
149  _ConstructMap::accessor a;
150  if(_constructMap.insert(a, keyHnd)) {
151  // Make sure another thread didn't beat us to it.
152  a->second = findItem(key);
153  if(!a->second) {
154  if((a->second = creator(args...))) {
155  addItem(key, a->second);
156  } else {
157  _constructMap.erase(a);
159  }
160  }
161  }
163  UTverify_cast<const Item*>(a->second.get()));
164  _constructMap.erase(a);
165  return item;
166 }
167 
168 template <typename MatchFn>
169 int64
170 GusdUT_CappedCache::ClearEntries(const MatchFn& matchFn)
171 {
172  int64 freed = 0;
173 
174  auto traverseFn([&](const UT_CappedKeyHandle& key,
175  const UT_CappedItemHandle& item)
176  {
177  if(matchFn(key, item)) {
178  freed += item->getMemoryUsage();
179  // XXX: deleteItem() is safe in threadSafeTraversal!
180  this->deleteItem(*key);
181  }
182  return true;
183  });
184  threadSafeTraversal(traverseFn);
185  return freed;
186 }
187 
189 
190 #endif /*_GUSD_UT_CAPPEDCACHE_H_*/
UT_CappedItemHandle findItem(const UT_CappedKey &key)
Find an item in the cache.
~GusdUT_CappedCache() override
GusdUT_CappedCache(const char *name, int64 size_in_mb=32)
UT_IntrusivePtr< UT_CappedItem > UT_CappedItemHandle
Base class for search keys for UT_CappedCache.
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
GusdUT_CappedKey(const KeyT &key)
SYS_FORCE_INLINE TO_T UTverify_cast(FROM_T from)
Definition: UT_Assert.h:229
~GusdUT_CappedKey() override
bool isEqual(const UT_CappedKey &key) const override
Test equality.
bool FindOrCreateVal(const UT_CappedKey &key, const Creator &creator, Args &...args)
int64 ClearEntries(const MatchFn &matchFn)
bool FindVal(const UT_CappedKey &key)
const KeyT & operator*() const
void deleteItem(const UT_CappedKey &key)
Remove an item from the cache.
void threadSafeTraversal(T &task)
unsigned getHash() const override
Return a hash for the key.
UT_CappedKey * duplicate() const override
The duplicate() method should return a copy of the key.
Wrapper around hboost::intrusive_ptr.
long long int64
Definition: SYS_Types.h:116
UT_CappedItemHandle addItem(const UT_CappedKey &key, const UT_CappedItemHandle &item)
GLuint const GLchar * name
Definition: glcorearb.h:786
const KeyT * operator->() const
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
#define UT_ConcurrentHashMap
UT_IntrusivePtr< const Item > Find(const UT_CappedKey &key)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
virtual UT_CappedKey * duplicate() const =0
The duplicate() method should return a copy of the key.
UT_IntrusivePtr< const Item > FindOrCreate(const UT_CappedKey &key, const Creator &creator, Args &...args)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
UT_IntrusivePtr< UT_CappedKey > UT_CappedKeyHandle
**If you just want to fire and args
Definition: thread.h:609