Skip to content

Commit

Permalink
GH-525 Use shared_mutex to allow multiple readers
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Dec 14, 2022
1 parent b408aa2 commit a143803
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/multi_index/composite_key.hpp>
#include <fc/io/fstream.hpp>
#include <fstream>
#include <shared_mutex>

namespace eosio { namespace chain {
using boost::multi_index_container;
Expand Down Expand Up @@ -64,7 +65,7 @@ namespace eosio { namespace chain {
:datadir(data_dir)
{}

std::mutex mtx;
std::shared_mutex mtx;
fork_multi_index_type index;
block_state_ptr root; // Only uses the block_header_state portion
block_state_ptr head;
Expand Down Expand Up @@ -99,7 +100,7 @@ namespace eosio { namespace chain {
const flat_set<digest_type>&,
const vector<digest_type>& )>& validator )
{
std::lock_guard<std::mutex> g( my->mtx );
std::lock_guard g( my->mtx );
if (!fc::is_directory(my->datadir))
fc::create_directories(my->datadir);

Expand Down Expand Up @@ -175,7 +176,7 @@ namespace eosio { namespace chain {
}

void fork_database::close() {
std::lock_guard<std::mutex> g( my->mtx );
std::lock_guard g( my->mtx );
auto fork_db_dat = my->datadir / config::forkdb_filename;

if( !my->root ) {
Expand Down Expand Up @@ -243,7 +244,7 @@ namespace eosio { namespace chain {
}

void fork_database::reset( const block_header_state& root_bhs ) {
std::lock_guard<std::mutex> g( my->mtx );
std::lock_guard g( my->mtx );
my->reset_impl(root_bhs);
}

Expand All @@ -256,7 +257,7 @@ namespace eosio { namespace chain {
}

void fork_database::rollback_head_to_root() {
std::lock_guard<std::mutex> g( my->mtx );
std::lock_guard g( my->mtx );
my->rollback_head_to_root_impl();
}

Expand All @@ -273,7 +274,7 @@ namespace eosio { namespace chain {
}

void fork_database::advance_root( const block_id_type& id ) {
std::lock_guard<std::mutex> g( my->mtx );
std::lock_guard g( my->mtx );
my->advance_root_impl( id );
}

Expand Down Expand Up @@ -311,7 +312,7 @@ namespace eosio { namespace chain {
}

block_header_state_ptr fork_database::get_block_header( const block_id_type& id )const {
std::lock_guard<std::mutex> g( my->mtx );
std::shared_lock g( my->mtx );
return my->get_block_header_impl( id );
}

Expand Down Expand Up @@ -365,7 +366,7 @@ namespace eosio { namespace chain {
}

void fork_database::add( const block_state_ptr& n, bool ignore_duplicate ) {
std::lock_guard<std::mutex> g( my->mtx );
std::lock_guard g( my->mtx );
my->add_impl( n, ignore_duplicate, false,
[]( block_timestamp_type timestamp,
const flat_set<digest_type>& cur_features,
Expand All @@ -375,17 +376,17 @@ namespace eosio { namespace chain {
}

block_state_ptr fork_database::root()const {
std::lock_guard<std::mutex> g( my->mtx );
std::shared_lock g( my->mtx );
return my->root;
}

block_state_ptr fork_database::head()const {
std::lock_guard<std::mutex> g( my->mtx );
std::shared_lock g( my->mtx );
return my->head;
}

block_state_ptr fork_database::pending_head()const {
std::lock_guard<std::mutex> g( my->mtx );
std::shared_lock g( my->mtx );
const auto& indx = my->index.get<by_lib_block_num>();

auto itr = indx.lower_bound( false );
Expand All @@ -398,7 +399,7 @@ namespace eosio { namespace chain {
}

branch_type fork_database::fetch_branch( const block_id_type& h, uint32_t trim_after_block_num )const {
std::lock_guard<std::mutex> g( my->mtx );
std::shared_lock g( my->mtx );
return my->fetch_branch_impl( h, trim_after_block_num );
}

Expand All @@ -413,7 +414,7 @@ namespace eosio { namespace chain {
}

block_state_ptr fork_database::search_on_branch( const block_id_type& h, uint32_t block_num )const {
std::lock_guard<std::mutex> g( my->mtx );
std::shared_lock g( my->mtx );
return my->search_on_branch_impl( h, block_num );
}

Expand All @@ -432,7 +433,7 @@ namespace eosio { namespace chain {
*/
pair< branch_type, branch_type > fork_database::fetch_branch_from( const block_id_type& first,
const block_id_type& second )const {
std::lock_guard<std::mutex> g( my->mtx );
std::shared_lock g( my->mtx );
return my->fetch_branch_from_impl( first, second );
}

Expand Down Expand Up @@ -497,7 +498,7 @@ namespace eosio { namespace chain {

/// remove all of the invalid forks built off of this id including this id
void fork_database::remove( const block_id_type& id ) {
std::lock_guard<std::mutex> g( my->mtx );
std::lock_guard g( my->mtx );
return my->remove_impl( id );
}

Expand Down Expand Up @@ -525,7 +526,7 @@ namespace eosio { namespace chain {
}

void fork_database::mark_valid( const block_state_ptr& h ) {
std::lock_guard<std::mutex> g( my->mtx );
std::lock_guard g( my->mtx );
my->mark_valid_impl( h );
}

Expand All @@ -550,7 +551,7 @@ namespace eosio { namespace chain {
}

block_state_ptr fork_database::get_block(const block_id_type& id)const {
std::lock_guard<std::mutex> g( my->mtx );
std::shared_lock g( my->mtx );
return my->get_block_impl(id);
}

Expand Down

0 comments on commit a143803

Please sign in to comment.