• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_COMPILER_XLA_REFCOUNTING_HASH_MAP_H_
17 #define TENSORFLOW_COMPILER_XLA_REFCOUNTING_HASH_MAP_H_
18 
19 #include <functional>
20 #include <memory>
21 
22 #include "absl/base/thread_annotations.h"
23 #include "absl/container/node_hash_map.h"
24 #include "absl/memory/memory.h"
25 #include "absl/synchronization/mutex.h"
26 #include "tensorflow/compiler/xla/statusor.h"
27 
28 namespace xla {
29 
30 // RefcountingHashMap is an "eager, thread-safe cache".
31 //
32 // Given a key k you can retrieve a shared_ptr to a value v.  If k is not
33 // already in the map, we construct a new V; if it is already in the map, we'll
34 // return the existing v.  Once all shared_ptrs are destroyed, the entry is
35 // removed from the map.
36 //
37 // This class is thread-safe.
38 //
39 // Word to the wise: You might want an erase() function here that removes a
40 // value from the map but leaves existing shared_ptrs intact.  My experience is,
41 // this is extremely complicated to implement correctly.
42 template <typename K, typename V>
43 class RefcountingHashMap {
44  public:
45   // Default-constructs new values.
46   RefcountingHashMap() = default;
47 
48   // Not copyable or movable because this contains internal pointers (namely,
49   // instances of Deleter contain pointers to `this` and into `map_`).
50   RefcountingHashMap(const RefcountingHashMap&) = delete;
51   RefcountingHashMap(RefcountingHashMap&&) = delete;
52   RefcountingHashMap& operator=(const RefcountingHashMap&) = delete;
53   RefcountingHashMap& operator=(RefcountingHashMap&&) = delete;
54 
55   // Gets the value for the given key.
56   //
57   // If the map doesn't contain a live value for the key, constructs one
58   // using `value_factory`.
GetOrCreateIfAbsent(const K & key,const std::function<std::unique_ptr<V> (const K &)> & value_factory)59   std::shared_ptr<V> GetOrCreateIfAbsent(
60       const K& key,
61       const std::function<std::unique_ptr<V>(const K&)>& value_factory) {
62     absl::MutexLock lock(&mu_);
63     auto it = map_.find(key);
64     if (it != map_.end()) {
65       // We ensure that the entry has not expired in case deleter was running
66       // when we have entered this block.
67       if (std::shared_ptr<V> value = it->second.lock()) {
68         return value;
69       }
70     }
71 
72     // Create entry in the map and then set its value, so the value can
73     // contain a pointer back into the map.
74     it = map_.emplace(key, std::weak_ptr<V>()).first;
75     std::shared_ptr<V> value(value_factory(key).release(),
76                              Deleter{it->first, *this});
77     it->second = value;  // Set the weak ptr to the shared ptr.
78     return value;
79   }
80 
81  private:
82   struct Deleter {
83     const K& key;  // Points into parent->map_.
84     RefcountingHashMap& parent;
85 
operatorDeleter86     void operator()(V* v) {
87       delete v;
88       absl::MutexLock lock(&parent.mu_);
89       // We must check if that the entry is still expired in case the value was
90       // replaced while the deleter was running.
91       auto it = parent.map_.find(key);
92       if (it != parent.map_.end() && it->second.expired()) {
93         parent.map_.erase(it);
94       }
95     }
96   };
97 
98   absl::Mutex mu_;
99   absl::node_hash_map<K, std::weak_ptr<V>> map_ ABSL_GUARDED_BY(mu_);
100 };
101 
102 }  // namespace xla
103 
104 #endif  // TENSORFLOW_COMPILER_XLA_REFCOUNTING_HASH_MAP_H_
105