diff --git a/include/ircd/db/database/env/port.h b/include/ircd/db/database/env/port.h index f6e0bc137..547176c29 100644 --- a/include/ircd/db/database/env/port.h +++ b/include/ircd/db/database/env/port.h @@ -34,6 +34,8 @@ namespace rocksdb::port { + using namespace ircd; + struct Mutex; struct CondVar; struct RWMutex; @@ -43,7 +45,7 @@ class rocksdb::port::Mutex { friend class CondVar; - std::mutex mu; + ctx::mutex mu; public: void Lock(); @@ -60,7 +62,7 @@ class rocksdb::port::Mutex class rocksdb::port::CondVar { Mutex *mu; - std::condition_variable cv; + ctx::condition_variable cv; public: void Wait(); @@ -74,7 +76,7 @@ class rocksdb::port::CondVar class rocksdb::port::RWMutex { - std::shared_mutex mu; + ctx::shared_mutex mu; public: void ReadLock(); diff --git a/ircd/db.cc b/ircd/db.cc index 352540a8d..676246948 100644 --- a/ircd/db.cc +++ b/ircd/db.cc @@ -2950,28 +2950,75 @@ noexcept rocksdb::port::Mutex::Mutex() { + #ifdef RB_DEBUG_DB_PORT_ + if(unlikely(!ctx::current || !is_main_thread())) + return; + + log::debug + { + db::log, "mutex %lu %p CTOR", ctx::id(), this + }; + #endif } rocksdb::port::Mutex::Mutex(bool adaptive) +:Mutex{} { } +rocksdb::port::Mutex::~Mutex() +{ + #ifdef RB_DEBUG_DB_PORT_ + if(unlikely(!ctx::current || !is_main_thread())) + return; + + log::debug + { + db::log, "mutex %lu %p DTOR", ctx::id(), this + }; + #endif +} + void rocksdb::port::Mutex::Lock() { + if(unlikely(!is_main_thread() || !ctx::current)) + return; + + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "mutex %lu %p LOCK", ctx::id(), this + }; + #endif + mu.lock(); } void rocksdb::port::Mutex::Unlock() { + if(unlikely(!is_main_thread() || !ctx::current)) + return; + + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "mutex %lu %p UNLOCK", ctx::id(), this + }; + #endif + + assert(mu.locked()); mu.unlock(); } void rocksdb::port::Mutex::AssertHeld() { - assert(1); + if(unlikely(!is_main_thread() || !ctx::current)) + return; + + assert(mu.locked()); } // @@ -2980,33 +3027,77 @@ rocksdb::port::Mutex::AssertHeld() rocksdb::port::RWMutex::RWMutex() { + #ifdef RB_DEBUG_DB_PORT_ + log::debug + { + db::log, "shared_mutex %lu %p CTOR", ctx::id(), this + }; + #endif } rocksdb::port::RWMutex::~RWMutex() { + #ifdef RB_DEBUG_DB_PORT_ + log::debug + { + db::log, "shared_mutex %lu %p DTOR", ctx::id(), this + }; + #endif } void rocksdb::port::RWMutex::ReadLock() { + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "shared_mutex %lu %p LOCK SHARED", ctx::id(), this + }; + #endif + + assert_main_thread(); mu.lock_shared(); } void rocksdb::port::RWMutex::WriteLock() { + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "shared_mutex %lu %p LOCK", ctx::id(), this + }; + #endif + + assert_main_thread(); mu.lock(); } void rocksdb::port::RWMutex::ReadUnlock() { + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "shared_mutex %lu %p UNLOCK SHARED", ctx::id(), this + }; + #endif + + assert_main_thread(); mu.unlock_shared(); } void rocksdb::port::RWMutex::WriteUnlock() { + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "shared_mutex %lu %p UNLOCK", ctx::id(), this + }; + #endif + + assert_main_thread(); mu.unlock(); } @@ -3017,41 +3108,92 @@ rocksdb::port::RWMutex::WriteUnlock() rocksdb::port::CondVar::CondVar(Mutex *mu) :mu{mu} { + #ifdef RB_DEBUG_DB_PORT_ + log::debug + { + db::log, "cond %lu %p %p CTOR", ctx::id(), this, mu + }; + #endif } rocksdb::port::CondVar::~CondVar() { + #ifdef RB_DEBUG_DB_PORT_ + log::debug + { + db::log, "cond %lu %p %p DTOR", ctx::id(), this, mu + }; + #endif } void rocksdb::port::CondVar::Wait() { + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "cond %lu %p %p WAIT", ctx::id(), this, mu + }; + #endif + assert(mu); - std::unique_lockmu)> ul(mu->mu); - cv.wait(ul); + assert_main_thread(); + std::unique_lockmu)> l + { + mu->mu, std::adopt_lock + }; + + cv.wait(l); } // Returns true if timeout occurred bool rocksdb::port::CondVar::TimedWait(uint64_t abs_time_us) { + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "cond %lu %p %p WAIT_UNTIL %lu", ctx::id(), this, mu, abs_time_us + }; + #endif + assert(mu); + assert_main_thread(); const std::chrono::microseconds us(abs_time_us); - const std::chrono::system_clock::time_point tp(us); - std::unique_lockmu)> ul(mu->mu); - const auto cvs(cv.wait_until(ul, tp)); - return cvs == std::cv_status::timeout; + const std::chrono::steady_clock::time_point tp(us); + std::unique_lockmu)> l + { + mu->mu, std::adopt_lock + }; + + return cv.wait_until(l, tp) == std::cv_status::timeout; } void rocksdb::port::CondVar::Signal() { + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "cond %lu %p %p NOTIFY", ctx::id(), this, mu + }; + #endif + + assert_main_thread(); cv.notify_one(); } void rocksdb::port::CondVar::SignalAll() { + #ifdef RB_DEBUG_DB_PORT + log::debug + { + db::log, "cond %lu %p %p BROADCAST", ctx::id(), this, mu + }; + #endif + + assert_main_thread(); cv.notify_all(); }