0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-28 23:08:20 +02:00

ircd::ctx::mutex: Add deadlock assertion on lock.

This commit is contained in:
Jason Volk 2019-09-10 11:05:17 -07:00
parent fcb4f6c5f4
commit 87fd0f04ea

View file

@ -26,6 +26,8 @@ class ircd::ctx::mutex
dock q; dock q;
bool m; bool m;
void deadlock_assertion() const noexcept;
public: public:
bool locked() const noexcept; bool locked() const noexcept;
size_t waiting() const noexcept; size_t waiting() const noexcept;
@ -92,6 +94,8 @@ ircd::ctx::mutex::unlock()
inline void inline void
ircd::ctx::mutex::lock() ircd::ctx::mutex::lock()
{ {
deadlock_assertion();
q.wait([this] q.wait([this]
{ {
return !locked(); return !locked();
@ -111,6 +115,8 @@ template<class time_point>
bool bool
ircd::ctx::mutex::try_lock_until(const time_point &tp) ircd::ctx::mutex::try_lock_until(const time_point &tp)
{ {
deadlock_assertion();
const bool success const bool success
{ {
q.wait_until(tp, [this] q.wait_until(tp, [this]
@ -129,6 +135,8 @@ inline bool
ircd::ctx::mutex::try_lock() ircd::ctx::mutex::try_lock()
noexcept noexcept
{ {
deadlock_assertion();
if(locked()) if(locked())
return false; return false;
@ -156,3 +164,11 @@ const noexcept
{ {
return m; return m;
} }
inline void
__attribute__((always_inline, artificial))
ircd::ctx::mutex::deadlock_assertion()
const noexcept
{
assert(!locked() || !waiting(cur()));
}