add spin_yield to undo DB

This commit is contained in:
Pavel Baykov 2022-04-20 07:45:55 -03:00
parent 6eccec2ba4
commit 73db2e5037
2 changed files with 26 additions and 1 deletions

View file

@ -25,6 +25,9 @@
#include <graphene/db/object.hpp> #include <graphene/db/object.hpp>
#include <deque> #include <deque>
#include <fc/exception/exception.hpp> #include <fc/exception/exception.hpp>
#include <fc/thread/spin_yield_lock.hpp>
#include <fc/fwd.hpp>
#include <fc/thread/unique_lock.hpp>
namespace graphene { namespace db { namespace graphene { namespace db {
@ -132,6 +135,8 @@ namespace graphene { namespace db {
std::deque<undo_state> _stack; std::deque<undo_state> _stack;
object_database& _db; object_database& _db;
size_t _max_size = 256; size_t _max_size = 256;
mutable fc::spin_yield_lock _spin_yield;
}; };
} } // graphene::db } } // graphene::db

View file

@ -25,6 +25,8 @@
#include <graphene/db/undo_database.hpp> #include <graphene/db/undo_database.hpp>
#include <fc/reflect/variant.hpp> #include <fc/reflect/variant.hpp>
#include <fc/thread/spin_yield_lock.hpp>
namespace graphene { namespace db { namespace graphene { namespace db {
void undo_database::enable() { _disabled = false; } void undo_database::enable() { _disabled = false; }
@ -44,6 +46,8 @@ undo_database::session::~session() {
undo_database::session undo_database::start_undo_session( bool force_enable ) undo_database::session undo_database::start_undo_session( bool force_enable )
{ {
synchronized(_spin_yield)
if( _disabled && !force_enable ) return session(*this); if( _disabled && !force_enable ) return session(*this);
bool disable_on_exit = _disabled && force_enable; bool disable_on_exit = _disabled && force_enable;
if( force_enable ) if( force_enable )
@ -58,6 +62,8 @@ undo_database::session undo_database::start_undo_session( bool force_enable )
} }
void undo_database::on_create( const object& obj ) void undo_database::on_create( const object& obj )
{ {
synchronized(_spin_yield)
if( _disabled ) return; if( _disabled ) return;
if( _stack.empty() ) if( _stack.empty() )
@ -73,6 +79,8 @@ void undo_database::on_modify( const object& obj )
{ {
if( _disabled ) return; if( _disabled ) return;
synchronized(_spin_yield)
if( _stack.empty() ) if( _stack.empty() )
_stack.emplace_back(); _stack.emplace_back();
auto& state = _stack.back(); auto& state = _stack.back();
@ -86,6 +94,8 @@ void undo_database::on_remove( const object& obj )
{ {
if( _disabled ) return; if( _disabled ) return;
synchronized(_spin_yield)
if( _stack.empty() ) if( _stack.empty() )
_stack.emplace_back(); _stack.emplace_back();
undo_state& state = _stack.back(); undo_state& state = _stack.back();
@ -105,7 +115,9 @@ void undo_database::on_remove( const object& obj )
} }
void undo_database::undo() void undo_database::undo()
{ try { {
synchronized(_spin_yield)
try {
FC_ASSERT( !_disabled ); FC_ASSERT( !_disabled );
FC_ASSERT( _active_sessions > 0 ); FC_ASSERT( _active_sessions > 0 );
disable(); disable();
@ -136,6 +148,8 @@ void undo_database::undo()
void undo_database::merge() void undo_database::merge()
{ {
synchronized(_spin_yield)
FC_ASSERT( _active_sessions > 0 ); FC_ASSERT( _active_sessions > 0 );
if( _active_sessions == 1 && _stack.size() == 1 ) if( _active_sessions == 1 && _stack.size() == 1 )
{ {
@ -256,12 +270,16 @@ void undo_database::merge()
} }
void undo_database::commit() void undo_database::commit()
{ {
synchronized(_spin_yield)
FC_ASSERT( _active_sessions > 0 ); FC_ASSERT( _active_sessions > 0 );
--_active_sessions; --_active_sessions;
} }
void undo_database::pop_commit() void undo_database::pop_commit()
{ {
synchronized(_spin_yield)
FC_ASSERT( _active_sessions == 0 ); FC_ASSERT( _active_sessions == 0 );
FC_ASSERT( !_stack.empty() ); FC_ASSERT( !_stack.empty() );
@ -299,6 +317,8 @@ void undo_database::pop_commit()
} }
const undo_state& undo_database::head()const const undo_state& undo_database::head()const
{ {
synchronized(_spin_yield)
FC_ASSERT( !_stack.empty() ); FC_ASSERT( !_stack.empty() );
return _stack.back(); return _stack.back();
} }