Skip to content

Commit

Permalink
Try to extract owner of lock on mutex timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
dragorn committed Jul 25, 2018
1 parent 53696e1 commit 3a6c4b9
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions kis_mutex.h
Expand Up @@ -33,6 +33,10 @@
#include <chrono>
#include <atomic>

#include <pthread.h>

#include "fmt.h"

// Seconds a lock is allowed to be held before throwing a timeout error
#define KIS_THREAD_DEADLOCK_TIMEOUT 15

Expand Down Expand Up @@ -112,7 +116,10 @@ class local_locker {
cpplock.lock();
#else
if (!cpplock.try_lock_for(std::chrono::seconds(KIS_THREAD_DEADLOCK_TIMEOUT))) {
throw(std::runtime_error("deadlocked thread: mutex not available w/in timeout"));
auto native_m = cpplock.native_handle();
throw(std::runtime_error(fmt::format("deadlocked thread: mutex not available within "
"timeout, appears to be locked by {}",
native_m->__data.__owner)));
}
#endif
}
Expand All @@ -124,7 +131,10 @@ class local_locker {
cpplock.lock();
#else
if (!cpplock.try_lock_for(std::chrono::seconds(KIS_THREAD_DEADLOCK_TIMEOUT))) {
throw(std::runtime_error("deadlocked thread: mutex not available w/in timeout"));
auto native_m = cpplock.native_handle();
throw(std::runtime_error(fmt::format("deadlocked thread: mutex not available within "
"timeout, appears to be locked by {}",
native_m->__data.__owner)));
}
#endif
}
Expand Down Expand Up @@ -173,7 +183,10 @@ class local_demand_locker {
cpplock->lock();
#else
if (!cpplock->try_lock_for(std::chrono::seconds(KIS_THREAD_DEADLOCK_TIMEOUT))) {
throw(std::runtime_error("deadlocked thread: mutex not available w/in timeout"));
auto native_m = cpplock->native_handle();
throw(std::runtime_error(fmt::format("deadlocked thread: mutex not available within "
"timeout, appears to be locked by {}",
native_m->__data.__owner)));
}
#endif
}
Expand All @@ -196,7 +209,10 @@ class local_eol_locker {
in->lock();
#else
if (!in->try_lock_for(std::chrono::seconds(KIS_THREAD_DEADLOCK_TIMEOUT))) {
throw(std::runtime_error("deadlocked thread: mutex not available w/in timeout"));
auto native_m = in->native_handle();
throw(std::runtime_error(fmt::format("deadlocked thread: mutex not available within "
"timeout, appears to be locked by {}",
native_m->__data.__owner)));
}
#endif
}
Expand Down

0 comments on commit 3a6c4b9

Please sign in to comment.