Skip to content

Commit c932777

Browse files
authored
[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.
1 parent cded048 commit c932777

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/runtime/hexagon/hexagon_thread_manager.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,15 @@ void HexagonThreadManager::WaitOnThreads() {
265265
}
266266

267267
void HexagonThreadManager::CheckSemaphore(unsigned syncID) {
268+
// We want the success case to be fast, so do not lock the mutex
268269
if (semaphores_.find(syncID) == semaphores_.end()) {
269-
semaphores_[syncID] = reinterpret_cast<qurt_sem_t*>(malloc(sizeof(qurt_sem_t)));
270-
qurt_sem_init_val(semaphores_[syncID], 0);
270+
// If we don't find it, lock the mutex, make sure it hasn't
271+
// been added by another thread before creating it.
272+
std::lock_guard<std::mutex> lock(semaphores_mutex_);
273+
if (semaphores_.find(syncID) == semaphores_.end()) {
274+
semaphores_[syncID] = reinterpret_cast<qurt_sem_t*>(malloc(sizeof(qurt_sem_t)));
275+
qurt_sem_init_val(semaphores_[syncID], 0);
276+
}
271277
}
272278
}
273279

src/runtime/hexagon/hexagon_thread_manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ class HexagonThreadManager {
213213
//! \brief Semaphores used by `Signal` and `Wait` mapped by ID.
214214
std::unordered_map<unsigned, qurt_sem_t*> semaphores_;
215215

216+
//! \brief Protects updates to semaphores_
217+
std::mutex semaphores_mutex_;
218+
216219
//! \brief Start semaphore created at time of construction; signled by `Start`.
217220
qurt_sem_t start_semaphore_;
218221

0 commit comments

Comments
 (0)