This is an automated email from the ASF dual-hosted git repository.

csullivan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new c932777d48 [Hexagon][runtime] Make 
HexagonThreadManager::CheckSemaphore thread safe (#13609)
c932777d48 is described below

commit c932777d4885c75a99e734c054957ab7e5dca52f
Author: Janet Schneider <[email protected]>
AuthorDate: Fri Dec 16 16:40:14 2022 -0800

    [Hexagon][runtime] Make HexagonThreadManager::CheckSemaphore thread safe 
(#13609)
    
    Protect CheckSemaphore with mutex. Ensure that only one thread can add a 
semaphore if it doesn't already exist.
---
 src/runtime/hexagon/hexagon_thread_manager.cc | 10 ++++++++--
 src/runtime/hexagon/hexagon_thread_manager.h  |  3 +++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/runtime/hexagon/hexagon_thread_manager.cc 
b/src/runtime/hexagon/hexagon_thread_manager.cc
index 3658611cf0..4f8ddd156b 100644
--- a/src/runtime/hexagon/hexagon_thread_manager.cc
+++ b/src/runtime/hexagon/hexagon_thread_manager.cc
@@ -265,9 +265,15 @@ void HexagonThreadManager::WaitOnThreads() {
 }
 
 void HexagonThreadManager::CheckSemaphore(unsigned syncID) {
+  // We want the success case to be fast, so do not lock the mutex
   if (semaphores_.find(syncID) == semaphores_.end()) {
-    semaphores_[syncID] = 
reinterpret_cast<qurt_sem_t*>(malloc(sizeof(qurt_sem_t)));
-    qurt_sem_init_val(semaphores_[syncID], 0);
+    // If we don't find it, lock the mutex, make sure it hasn't
+    // been added by another thread before creating it.
+    std::lock_guard<std::mutex> lock(semaphores_mutex_);
+    if (semaphores_.find(syncID) == semaphores_.end()) {
+      semaphores_[syncID] = 
reinterpret_cast<qurt_sem_t*>(malloc(sizeof(qurt_sem_t)));
+      qurt_sem_init_val(semaphores_[syncID], 0);
+    }
   }
 }
 
diff --git a/src/runtime/hexagon/hexagon_thread_manager.h 
b/src/runtime/hexagon/hexagon_thread_manager.h
index c911d1326a..9bf6bb6efe 100644
--- a/src/runtime/hexagon/hexagon_thread_manager.h
+++ b/src/runtime/hexagon/hexagon_thread_manager.h
@@ -213,6 +213,9 @@ class HexagonThreadManager {
   //! \brief Semaphores used by `Signal` and `Wait` mapped by ID.
   std::unordered_map<unsigned, qurt_sem_t*> semaphores_;
 
+  //! \brief Protects updates to semaphores_
+  std::mutex semaphores_mutex_;
+
   //! \brief Start semaphore created at time of construction; signled by 
`Start`.
   qurt_sem_t start_semaphore_;
 

Reply via email to