Fix attempted to destroy locked thread pool error (#5040)

This commit is contained in:
Bartłomiej Chmiel 2024-04-29 13:59:24 +02:00 committed by GitHub
parent 5601056ed0
commit d1b45b8d32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -115,6 +115,7 @@ class V3ThreadPool final {
// CONSTRUCTORS
V3ThreadPool() = default;
~V3ThreadPool() {
m_shutdown = true;
if (m_multithreadingSuspended) {
// Ideally we shouldn't deal with this and just call the std::abort. However,
// std::exit(0) (which invokes this destructor) is called in multiple places with
@ -140,11 +141,15 @@ class V3ThreadPool final {
}
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) {
using namespace std::chrono_literals;
// try to obtain lock with timeout
// we need to wait for workers to start waiting
// on condition variable to perform destruction
constexpr int spins = 1s / 1ms;
for (int i = 0; i < spins; ++i) {
if (VL_LIKELY(m_mutex_locked)) break;
VL_CPU_RELAX();
std::this_thread::sleep_for(1ms);
m_mutex_locked = m_mutex.try_lock();
}
if (VL_UNCOVERABLE(!m_mutex_locked)) {