mirror of
https://github.com/matrix-construct/construct
synced 2024-11-17 15:30:52 +01:00
4fdc6355fc
Fixes #81.
51 lines
1.7 KiB
C++
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_
|