0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-29 20:28:52 +02:00
construct/ircd/db_write_thread.cc
2019-06-11 13:57:15 -07:00

51 lines
1.7 KiB
C++

// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#if __has_include("db/write_thread.h")
#define ROCKSDB_PLATFORM_POSIX
#include "db/write_thread.h"
#endif
#ifndef STORAGE_ROCKSDB_INCLUDE_DB_H_
#warning "The RocksDB source code is not available. Cannot interpose bugfixes for db/write_thread.h."
#endif
#ifdef STORAGE_ROCKSDB_INCLUDE_DB_H_
uint8_t
__attribute__((externally_visible))
rocksdb::WriteThread::BlockingAwaitState(Writer *const w,
uint8_t goal_mask)
{
// Create the class member mutex and cv where it's expected by
// rocksdb callers
w->CreateMutex();
auto state(w->state.load(std::memory_order_acquire));
assert(state != STATE_LOCKED_WAITING);
if((state & goal_mask) == 0 && w->state.compare_exchange_strong(state, STATE_LOCKED_WAITING))
{
size_t yields(0);
while((state = w->state.load(std::memory_order_relaxed)) == STATE_LOCKED_WAITING)
{
ircd::ctx::yield();
++yields;
}
// Since we're using a coarse ctx::yield() it's theoretically possible
// that our loop can spin out of control. That is highly unlikely,
// and there is usually not even more than one iteration. Nevertheless
// we assert to be sure this is working within reason.
assert(yields < 32);
}
assert((state & goal_mask) != 0);
return state;
}
#endif STORAGE_ROCKSDB_INCLUDE_DB_H_