Skip to content

Commit

Permalink
[Hexagon][runtime] Make HexagonThreadManager::CheckSemaphore thread s…
Browse files Browse the repository at this point in the history
…afe (#13609)

Protect CheckSemaphore with mutex. Ensure that only one thread can add a semaphore if it doesn't already exist.
  • Loading branch information
janetsc committed Dec 17, 2022
1 parent cded048 commit c932777
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/runtime/hexagon/hexagon_thread_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/runtime/hexagon/hexagon_thread_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;

Expand Down

0 comments on commit c932777

Please sign in to comment.