From 17c561fb3a19aee2886364f15458ab6662cc47f3 Mon Sep 17 00:00:00 2001 From: Janet Schneider Date: Thu, 15 Sep 2022 17:11:46 -0700 Subject: [PATCH 1/2] Protect access to global buffer manager map --- src/runtime/hexagon/hexagon_buffer_manager.h | 25 +++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/runtime/hexagon/hexagon_buffer_manager.h b/src/runtime/hexagon/hexagon_buffer_manager.h index 658a39fac8a8..4ecc11ce9b1d 100644 --- a/src/runtime/hexagon/hexagon_buffer_manager.h +++ b/src/runtime/hexagon/hexagon_buffer_manager.h @@ -43,7 +43,10 @@ class HexagonBufferManager { CHECK(it != hexagon_buffer_map_.end()) << "Attempt made to free unknown or already freed dataspace allocation"; CHECK(it->second != nullptr); - hexagon_buffer_map_.erase(it); + { + std::lock_guard lock(map_mutex_); + hexagon_buffer_map_.erase(it); + } } /*! * \brief Allocate a HexagonBuffer. @@ -53,15 +56,22 @@ class HexagonBufferManager { void* AllocateHexagonBuffer(Args&&... args) { auto buf = std::make_unique(std::forward(args)...); void* ptr = buf->GetPointer(); - hexagon_buffer_map_.insert({ptr, std::move(buf)}); + { + std::lock_guard lock(map_mutex_); + hexagon_buffer_map_.insert({ptr, std::move(buf)}); + } return ptr; } //! \brief Returns whether the HexagonBuffer is in the map. - size_t count(void* ptr) { return hexagon_buffer_map_.count(ptr); } + size_t count(void* ptr) { + std::lock_guard lock(map_mutex_); + return hexagon_buffer_map_.count(ptr); + } //! \brief Returns an iterator to the HexagonBuffer within the map. HexagonBuffer* find(void* ptr) { + std::lock_guard lock(map_mutex_); auto it = hexagon_buffer_map_.find(ptr); if (it != hexagon_buffer_map_.end()) { return it->second.get(); @@ -69,9 +79,18 @@ class HexagonBufferManager { return nullptr; } + //! \brief Returns whether the HexagonBufferManager has any allocations. + bool empty() { + std::lock_guard lock(map_mutex_); + return hexagon_buffer_map_.empty(); + } + private: //! \brief Contains the HexagonBuffer objects managed by this class. std::unordered_map> hexagon_buffer_map_; + + //! \brief Protects updates to the map. + std::mutex map_mutex_; }; } // namespace hexagon From 152546900357df8d5f0f6f392bcdfb3b974fa878 Mon Sep 17 00:00:00 2001 From: Janet Schneider Date: Thu, 15 Sep 2022 17:22:02 -0700 Subject: [PATCH 2/2] Fix lint --- src/runtime/hexagon/hexagon_buffer_manager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/hexagon/hexagon_buffer_manager.h b/src/runtime/hexagon/hexagon_buffer_manager.h index 4ecc11ce9b1d..a698b0ecb163 100644 --- a/src/runtime/hexagon/hexagon_buffer_manager.h +++ b/src/runtime/hexagon/hexagon_buffer_manager.h @@ -82,7 +82,7 @@ class HexagonBufferManager { //! \brief Returns whether the HexagonBufferManager has any allocations. bool empty() { std::lock_guard lock(map_mutex_); - return hexagon_buffer_map_.empty(); + return hexagon_buffer_map_.empty(); } private: