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

This commit is contained in:
Kamil Rakoczy 2024-03-01 21:29:13 +01:00 committed by GitHub
parent 9dcb7ea6a9
commit 01dadb0a8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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