From 01dadb0a8d1c86493d310f400a084fa899915edb Mon Sep 17 00:00:00 2001 From: Kamil Rakoczy Date: Fri, 1 Mar 2024 21:29:13 +0100 Subject: [PATCH] Fix try-lock spuriously fails (#4931) (#4938) --- src/V3ThreadPool.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/V3ThreadPool.h b/src/V3ThreadPool.h index 2d552c10a..09b2ba17c 100644 --- a/src/V3ThreadPool.h +++ b/src/V3ThreadPool.h @@ -139,7 +139,15 @@ class V3ThreadPool final { std::abort(); } - if (VL_UNCOVERABLE(!m_mutex.try_lock())) { + bool m_mutex_locked = m_mutex.try_lock(); + // try_lock can sometimes spontaneously fail even when mutex is not locked, + // make sure this isn't the case + for (int i = 0; i < VL_LOCK_SPINS; ++i) { + if (VL_LIKELY(m_mutex_locked)) break; + VL_CPU_RELAX(); + m_mutex_locked = m_mutex.try_lock(); + } + if (VL_UNCOVERABLE(!m_mutex_locked)) { if (VL_UNCOVERABLE(m_jobsInProgress != 0)) { // ThreadPool shouldn't be destroyed when jobs are running and mutex is locked, // something is wrong. Most likely Verilator is exiting as a result of failed