0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-27 11:18:51 +02:00

ircd::db: Convert rocksdb::port from pthread to ircd::ctx.

This commit is contained in:
Jason Volk 2018-06-01 11:31:46 -07:00
parent b8aba99ed4
commit 7e25d99cc6
2 changed files with 154 additions and 10 deletions

View file

@ -34,6 +34,8 @@
namespace rocksdb::port namespace rocksdb::port
{ {
using namespace ircd;
struct Mutex; struct Mutex;
struct CondVar; struct CondVar;
struct RWMutex; struct RWMutex;
@ -43,7 +45,7 @@ class rocksdb::port::Mutex
{ {
friend class CondVar; friend class CondVar;
std::mutex mu; ctx::mutex mu;
public: public:
void Lock(); void Lock();
@ -60,7 +62,7 @@ class rocksdb::port::Mutex
class rocksdb::port::CondVar class rocksdb::port::CondVar
{ {
Mutex *mu; Mutex *mu;
std::condition_variable cv; ctx::condition_variable cv;
public: public:
void Wait(); void Wait();
@ -74,7 +76,7 @@ class rocksdb::port::CondVar
class rocksdb::port::RWMutex class rocksdb::port::RWMutex
{ {
std::shared_mutex mu; ctx::shared_mutex mu;
public: public:
void ReadLock(); void ReadLock();

View file

@ -2950,28 +2950,75 @@ noexcept
rocksdb::port::Mutex::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 CTOR", ctx::id(), this
};
#endif
} }
rocksdb::port::Mutex::Mutex(bool adaptive) 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 void
rocksdb::port::Mutex::Lock() 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(); mu.lock();
} }
void void
rocksdb::port::Mutex::Unlock() 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(); mu.unlock();
} }
void void
rocksdb::port::Mutex::AssertHeld() 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() 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() rocksdb::port::RWMutex::~RWMutex()
{ {
#ifdef RB_DEBUG_DB_PORT_
log::debug
{
db::log, "shared_mutex %lu %p DTOR", ctx::id(), this
};
#endif
} }
void void
rocksdb::port::RWMutex::ReadLock() 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(); mu.lock_shared();
} }
void void
rocksdb::port::RWMutex::WriteLock() 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(); mu.lock();
} }
void void
rocksdb::port::RWMutex::ReadUnlock() 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(); mu.unlock_shared();
} }
void void
rocksdb::port::RWMutex::WriteUnlock() 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(); mu.unlock();
} }
@ -3017,41 +3108,92 @@ rocksdb::port::RWMutex::WriteUnlock()
rocksdb::port::CondVar::CondVar(Mutex *mu) rocksdb::port::CondVar::CondVar(Mutex *mu)
:mu{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() rocksdb::port::CondVar::~CondVar()
{ {
#ifdef RB_DEBUG_DB_PORT_
log::debug
{
db::log, "cond %lu %p %p DTOR", ctx::id(), this, mu
};
#endif
} }
void void
rocksdb::port::CondVar::Wait() 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); assert(mu);
std::unique_lock<decltype(mu->mu)> ul(mu->mu); assert_main_thread();
cv.wait(ul); std::unique_lock<decltype(mu->mu)> l
{
mu->mu, std::adopt_lock
};
cv.wait(l);
} }
// Returns true if timeout occurred // Returns true if timeout occurred
bool bool
rocksdb::port::CondVar::TimedWait(uint64_t abs_time_us) 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(mu);
assert_main_thread();
const std::chrono::microseconds us(abs_time_us); const std::chrono::microseconds us(abs_time_us);
const std::chrono::system_clock::time_point tp(us); const std::chrono::steady_clock::time_point tp(us);
std::unique_lock<decltype(mu->mu)> ul(mu->mu); std::unique_lock<decltype(mu->mu)> l
const auto cvs(cv.wait_until(ul, tp)); {
return cvs == std::cv_status::timeout; mu->mu, std::adopt_lock
};
return cv.wait_until(l, tp) == std::cv_status::timeout;
} }
void void
rocksdb::port::CondVar::Signal() 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(); cv.notify_one();
} }
void void
rocksdb::port::CondVar::SignalAll() 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(); cv.notify_all();
} }