File tree Expand file tree Collapse file tree 2 files changed +11
-2
lines changed Expand file tree Collapse file tree 2 files changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -265,9 +265,15 @@ void HexagonThreadManager::WaitOnThreads() {
265265}
266266
267267void 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments