Blob Blame History Raw
commit 01dadb0a8d1c86493d310f400a084fa899915edb
Author: Kamil Rakoczy <kamilrakoczy1@gmail.com>
Date:   Fri Mar 1 21:29:13 2024 +0100

    Fix try-lock spuriously fails (#4931) (#4938)

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