|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef TVM_RUNTIME_THREAD_MAP_H_ |
| 21 | +#define TVM_RUNTIME_THREAD_MAP_H_ |
| 22 | + |
| 23 | +#include <functional> |
| 24 | +#include <memory> |
| 25 | +#include <mutex> |
| 26 | +#include <shared_mutex> |
| 27 | +#include <thread> |
| 28 | +#include <unordered_map> |
| 29 | +#include <utility> |
| 30 | + |
| 31 | +namespace tvm { |
| 32 | +namespace runtime { |
| 33 | + |
| 34 | +/*! \brief Container to hold one value per thread |
| 35 | + * |
| 36 | + * Similar to thread_local, but intended for use as a non-static or |
| 37 | + * non-block variable, such as class member variables. All member |
| 38 | + * functions are thread-safe to call. If only the current thread's |
| 39 | + * value is accessed, no additional synchronization is required. If |
| 40 | + * another thread's stored values are accessed, external |
| 41 | + * synchronization may be required. |
| 42 | + * |
| 43 | + * Calls that only require access to already-existing values will not |
| 44 | + * block each other. Calls that require constructing a new value will |
| 45 | + * block any other calls. |
| 46 | + * |
| 47 | + * \tparam T The object type to be held. For instantiation of |
| 48 | + * ThreadMap<T> and for calls to ThreadMap<T>::Get, only a forward |
| 49 | + * declaration is required. For calls to ThreadMap<T>::GetOrMake, a |
| 50 | + * full class definition is required. |
| 51 | + */ |
| 52 | +template <typename T> |
| 53 | +class ThreadMap { |
| 54 | + public: |
| 55 | + ThreadMap() {} |
| 56 | + |
| 57 | + /*! \brief Return the current thread's stored object, if it exists. |
| 58 | + * |
| 59 | + * \return If it exists, a pointer to the stored object. Otherwise, |
| 60 | + * returns nullptr. |
| 61 | + */ |
| 62 | + const T* Get() const { return this->Get(std::this_thread::get_id()); } |
| 63 | + |
| 64 | + /*! \brief Return the stored object for a given thread, if it exists. |
| 65 | + * |
| 66 | + * \param id The thread whose object should be returned. |
| 67 | + * |
| 68 | + * \return If it exists, a pointer to the stored object. Otherwise, |
| 69 | + * returns nullptr. |
| 70 | + */ |
| 71 | + const T* Get(std::thread::id id) const { |
| 72 | + std::shared_lock<std::shared_timed_mutex> lock(mutex_); |
| 73 | + auto res = values_.find(id); |
| 74 | + if (res == values_.end()) { |
| 75 | + return nullptr; |
| 76 | + } else { |
| 77 | + return res->second.get(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /*! \brief Return the current thread's stored object, if it exists. |
| 82 | + * |
| 83 | + * \return If it exists, a pointer to the stored object. Otherwise, |
| 84 | + * returns nullptr. |
| 85 | + */ |
| 86 | + T* Get() { return const_cast<T*>(const_cast<const ThreadMap<T>*>(this)->Get()); } |
| 87 | + |
| 88 | + /*! \brief Return the stored object for a given thread, if it exists. |
| 89 | + * |
| 90 | + * \param id The thread whose object should be returned. |
| 91 | + * |
| 92 | + * \return If it exists, a pointer to the stored object. Otherwise, |
| 93 | + * returns nullptr. |
| 94 | + */ |
| 95 | + T* Get(std::thread::id id) { |
| 96 | + return const_cast<T*>(const_cast<const ThreadMap<T>*>(this)->Get(id)); |
| 97 | + } |
| 98 | + |
| 99 | + /*! \brief Return the current thread's stored object, making it if |
| 100 | + * necessary. |
| 101 | + * |
| 102 | + * Since this method can modify the stored map, there is no |
| 103 | + * non-const version available. |
| 104 | + * |
| 105 | + * \tparam Params Types of the stored object's constructor arguments |
| 106 | + * |
| 107 | + * \return A reference to the stored object |
| 108 | + */ |
| 109 | + template <typename... Params> |
| 110 | + T& GetOrMake(Params&&... params) { |
| 111 | + return GetOrMake(std::this_thread::get_id(), std::forward<Params>(params)...); |
| 112 | + } |
| 113 | + |
| 114 | + /*! \brief Return the stored object for a given thread, making it if |
| 115 | + * necessary |
| 116 | + * |
| 117 | + * Since this method can modify the stored map, there is no |
| 118 | + * non-const version available. |
| 119 | + * |
| 120 | + * \tparam Params Types of the stored object's constructor arguments |
| 121 | + * |
| 122 | + * \param id The thread whose object should be returned. |
| 123 | + * |
| 124 | + * \param params Arguments to the stored object's constructor. Only |
| 125 | + * used if the specified thread does not currently exist in the map. |
| 126 | + * |
| 127 | + * \return A reference to the stored object |
| 128 | + */ |
| 129 | + template <typename... Params> |
| 130 | + T& GetOrMake(std::thread::id id, Params&&... params) { |
| 131 | + // Try to get stored value first, which would only require shared |
| 132 | + // access. |
| 133 | + if (T* output = Get(id)) { |
| 134 | + return *output; |
| 135 | + } |
| 136 | + |
| 137 | + // Not in map, need exclusive lock to write |
| 138 | + std::unique_lock<std::shared_timed_mutex> lock(mutex_); |
| 139 | + |
| 140 | + // Check again, in case another thread got the unique lock first |
| 141 | + // and already constructed the object. |
| 142 | + auto res = values_.find(id); |
| 143 | + if (res != values_.end()) { |
| 144 | + return *res->second; |
| 145 | + } |
| 146 | + |
| 147 | + // No value exists, make one and return it. |
| 148 | + std::unique_ptr<T>& new_val = values_[id] = |
| 149 | + std::make_unique<T>(std::forward<Params>(params)...); |
| 150 | + return *new_val; |
| 151 | + } |
| 152 | + |
| 153 | + /*! \brief Clears all values held by the ThreadMap |
| 154 | + * |
| 155 | + * Calling Clear() invalidates any pointers/references previously |
| 156 | + * returned by Get/GetOrMake. |
| 157 | + * |
| 158 | + */ |
| 159 | + void Clear() { |
| 160 | + std::unique_lock<std::shared_timed_mutex> lock(mutex_); |
| 161 | + values_.clear(); |
| 162 | + } |
| 163 | + |
| 164 | + private: |
| 165 | + //! \brief Mutex to protect values_ |
| 166 | + mutable std::shared_timed_mutex mutex_; |
| 167 | + |
| 168 | + //! \brief Map containing stored values |
| 169 | + std::unordered_map<std::thread::id, std::unique_ptr<T>> values_; |
| 170 | +}; |
| 171 | + |
| 172 | +} // namespace runtime |
| 173 | +} // namespace tvm |
| 174 | + |
| 175 | +#endif // TVM_RUNTIME_THREAD_MAP_H_ |
0 commit comments