Skip to content

Commit

Permalink
Use lock_guard with scope instead of unique_lock
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Sep 25, 2024
1 parent 574b34c commit b8e8d47
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4497,10 +4497,11 @@ namespace eosio {
return "invalid peer address";
}

std::unique_lock g( connections_mtx );
if( find_connection_i( peer_address ) )
return "already connected";
g.unlock();
{
std::lock_guard g(connections_mtx);
if (find_connection_i(peer_address))
return "already connected";
}

auto [host, port, type] = split_host_port_type(peer_address);

Expand All @@ -4511,12 +4512,13 @@ namespace eosio {
connection_ptr c = std::make_shared<connection>( peer_address, listen_address );
c->strand.post([this, resolver, c, err, results, host, port, peer_address]() {
c->set_heartbeat_timeout( heartbeat_timeout );
std::unique_lock g( connections_mtx );
connections.emplace( connection_detail{
.host = peer_address,
.c = c,
});
g.unlock();
{
std::lock_guard g( connections_mtx );
connections.emplace( connection_detail{
.host = peer_address,
.c = c,
});
}
if( !err ) {
c->connect( results );
} else {
Expand Down Expand Up @@ -4544,10 +4546,11 @@ namespace eosio {
}

void connections_manager::reconnect(const connection_ptr& c) {
std::unique_lock g( connections_mtx );
auto& index = connections.get<by_connection>();
index.erase(c);
g.unlock();
{
std::lock_guard g( connections_mtx );
auto& index = connections.get<by_connection>();
index.erase(c);
}
resolve_and_connect(c->peer_address(), c->listen_address);
}

Expand Down

0 comments on commit b8e8d47

Please sign in to comment.