diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f847563e1..274a41a45e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,33 @@ set(NANO_TIMED_LOCKS set(NANO_TIMED_LOCKS_IGNORE_BLOCKED OFF CACHE BOOL "") +set(NANO_TIMED_LOCKS_FILTER + "" + CACHE + STRING + "Selects which mutex should be the only one to have stacktraces generated, empty string means report on all" +) +set_property( + CACHE NANO_TIMED_LOCKS_FILTER + PROPERTY STRINGS + active + block_arrival + block_processor + block_uniquer + confirmation_height_processor + dropped_elections, + election_winner_details + gap_cache + network_filter + observer_set + request_aggregator + state_block_signature_verification + telemetry + vote_generator + vote_processor + vote_uniquer + votes_cache + work_pool) set(NANO_FUZZER_TEST OFF CACHE BOOL "") @@ -103,6 +130,7 @@ endif() if(${NANO_TIMED_LOCKS} GREATER 0) add_definitions(-DNANO_TIMED_LOCKS=${NANO_TIMED_LOCKS}) + add_definitions(-DNANO_TIMED_LOCKS_FILTER=${NANO_TIMED_LOCKS_FILTER}) if(NANO_TIMED_LOCKS_IGNORE_BLOCKED) add_definitions(-DNANO_TIMED_LOCKS_IGNORE_BLOCKED) endif() diff --git a/ci/build-travis.sh b/ci/build-travis.sh index 7e0d5256d4..39fa935509 100755 --- a/ci/build-travis.sh +++ b/ci/build-travis.sh @@ -13,9 +13,9 @@ if [[ $(grep -rl --exclude="*asio.hpp" "asio::async_write" ./nano) ]]; then exit 1 fi -# prevent unsolicited use of std::lock_guard, std::unique_lock & std::condition_variable outside of allowed areas -if [[ $(grep -rl --exclude={"*random_pool.cpp","*random_pool.hpp","*random_pool_shuffle.hpp","*locks.hpp","*locks.cpp"} "std::unique_lock\|std::lock_guard\|std::condition_variable" ./nano) ]]; then - echo "Using std::unique_lock, std::lock_guard or std::condition_variable is not permitted (except in nano/lib/locks.hpp and non-nano dependent libraries). Use the nano::* versions instead" +# prevent unsolicited use of std::lock_guard, std::unique_lock, std::condition_variable & std::mutex outside of allowed areas +if [[ $(grep -rl --exclude={"*random_pool.cpp","*random_pool.hpp","*random_pool_shuffle.hpp","*locks.hpp","*locks.cpp"} "std::unique_lock\|std::lock_guard\|std::condition_variable\|std::mutex" ./nano) ]]; then + echo "Using std::unique_lock, std::lock_guard, std::condition_variable or std::mutex is not permitted (except in nano/lib/locks.hpp and non-nano dependent libraries). Use the nano::* versions instead" exit 1 fi diff --git a/nano/core_test/active_transactions.cpp b/nano/core_test/active_transactions.cpp index d995d50f76..fa505b6380 100644 --- a/nano/core_test/active_transactions.cpp +++ b/nano/core_test/active_transactions.cpp @@ -48,7 +48,7 @@ TEST (active_transactions, confirm_active) auto peers (node2.network.random_set (1)); ASSERT_FALSE (peers.empty ()); { - nano::lock_guard guard (node2.rep_crawler.probable_reps_mutex); + nano::lock_guard guard (node2.rep_crawler.probable_reps_mutex); node2.rep_crawler.probable_reps.emplace (nano::dev_genesis_key.pub, nano::genesis_amount, *peers.begin ()); } ASSERT_TIMELY (10s, node2.ledger.cache.cemented_count == 2 && node2.active.empty ()); @@ -91,7 +91,7 @@ TEST (active_transactions, confirm_frontier) auto peers (node2.network.random_set (1)); ASSERT_FALSE (peers.empty ()); { - nano::lock_guard guard (node2.rep_crawler.probable_reps_mutex); + nano::lock_guard guard (node2.rep_crawler.probable_reps_mutex); node2.rep_crawler.probable_reps.emplace (nano::dev_genesis_key.pub, nano::genesis_amount, *peers.begin ()); } ASSERT_TIMELY (5s, node2.ledger.cache.cemented_count == 2 && node2.active.empty ()); @@ -271,7 +271,7 @@ TEST (active_transactions, inactive_votes_cache_existing_vote) ASSERT_EQ (send->hash (), last_vote1.hash); ASSERT_EQ (1, last_vote1.timestamp); // Attempt to change vote with inactive_votes_cache - nano::unique_lock active_lock (node.active.mutex); + nano::unique_lock active_lock (node.active.mutex); node.active.add_inactive_votes_cache (active_lock, send->hash (), key.pub); active_lock.unlock (); auto cache (node.active.find_inactive_votes_cache (send->hash ())); @@ -554,13 +554,13 @@ TEST (active_transactions, update_difficulty) { { // node1 - nano::lock_guard guard1 (node1.active.mutex); + nano::lock_guard guard1 (node1.active.mutex); auto const existing1 (node1.active.roots.find (send1->qualified_root ())); ASSERT_NE (existing1, node1.active.roots.end ()); auto const existing2 (node1.active.roots.find (send2->qualified_root ())); ASSERT_NE (existing2, node1.active.roots.end ()); // node2 - nano::lock_guard guard2 (node2.active.mutex); + nano::lock_guard guard2 (node2.active.mutex); auto const existing3 (node2.active.roots.find (send1->qualified_root ())); ASSERT_NE (existing3, node2.active.roots.end ()); auto const existing4 (node2.active.roots.find (send2->qualified_root ())); @@ -657,7 +657,7 @@ TEST (active_transactions, vote_replays) // Removing blocks as recently confirmed makes every vote indeterminate { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); node.active.recently_confirmed.clear (); } ASSERT_EQ (nano::vote_code::indeterminate, node.active.vote (vote_send1)); @@ -1015,7 +1015,7 @@ TEST (active_transactions, confirmation_consistency) ASSERT_NO_ERROR (system.poll (5ms)); } ASSERT_NO_ERROR (system.poll_until_true (1s, [&node, &block, i] { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); EXPECT_EQ (i + 1, node.active.recently_confirmed.size ()); EXPECT_EQ (block->qualified_root (), node.active.recently_confirmed.back ().first); return i + 1 == node.active.recently_cemented.size (); // done after a callback @@ -1106,7 +1106,7 @@ TEST (active_transactions, insertion_prioritization) std::sort (blocks.begin (), blocks.end (), [](auto const & blockl, auto const & blockr) { return blockl->difficulty () > blockr->difficulty (); }); auto update_active_multiplier = [&node] { - nano::unique_lock lock (node.active.mutex); + nano::unique_lock lock (node.active.mutex); node.active.update_active_multiplier (lock); }; @@ -1132,7 +1132,7 @@ TEST (active_multiplier, less_than_one) { nano::system system (1); auto & node (*system.nodes[0]); - nano::unique_lock lock (node.active.mutex); + nano::unique_lock lock (node.active.mutex); auto base_active_difficulty = node.network_params.network.publish_thresholds.epoch_1; auto base_active_multiplier = 1.0; auto min_active_difficulty = node.network_params.network.publish_thresholds.entry; @@ -1241,7 +1241,7 @@ TEST (active_transactions, election_difficulty_update_old) ASSERT_EQ (1, node.active.size ()); auto multiplier = node.active.roots.begin ()->multiplier; { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); ASSERT_EQ (node.active.normalized_multiplier (*send1), multiplier); } // Should not update with a lower difficulty @@ -1675,7 +1675,7 @@ TEST (active_transactions, difficulty_update_observer) auto & node (*system.nodes[0]); std::atomic update_received (false); node.observers.difficulty.add ([& mutex = node.active.mutex, &update_received](uint64_t difficulty_a) { - nano::unique_lock lock (mutex, std::defer_lock); + nano::unique_lock lock (mutex, std::defer_lock); EXPECT_TRUE (lock.try_lock ()); update_received = true; }); diff --git a/nano/core_test/bootstrap.cpp b/nano/core_test/bootstrap.cpp index 3e7035af17..17c5dad803 100644 --- a/nano/core_test/bootstrap.cpp +++ b/nano/core_test/bootstrap.cpp @@ -333,7 +333,7 @@ TEST (bootstrap_processor, DISABLED_pull_requeue_network_error) ASSERT_TIMELY (2s, attempt->frontiers_received); // Add non-existing pull & stop remote peer { - nano::unique_lock lock (node1->bootstrap_initiator.connections->mutex); + nano::unique_lock lock (node1->bootstrap_initiator.connections->mutex); ASSERT_FALSE (attempt->stopped); ++attempt->pulling; node1->bootstrap_initiator.connections->pulls.push_back (nano::pull_info (nano::dev_genesis_key.pub, send1->hash (), genesis.hash (), attempt->incremental_id)); diff --git a/nano/core_test/confirmation_height.cpp b/nano/core_test/confirmation_height.cpp index 1c7f4af3e5..be26896a9d 100644 --- a/nano/core_test/confirmation_height.cpp +++ b/nano/core_test/confirmation_height.cpp @@ -10,13 +10,13 @@ using namespace std::chrono_literals; namespace { -void add_callback_stats (nano::node & node, std::vector * observer_order = nullptr, std::mutex * mutex = nullptr) +void add_callback_stats (nano::node & node, std::vector * observer_order = nullptr, nano::mutex * mutex = nullptr) { node.observers.blocks.add ([& stats = node.stats, observer_order, mutex](nano::election_status const & status_a, nano::account const &, nano::amount const &, bool) { stats.inc (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out); if (mutex) { - nano::lock_guard guard (*mutex); + nano::lock_guard guard (*mutex); debug_assert (observer_order); observer_order->push_back (status_a.winner->hash ()); } @@ -1006,7 +1006,7 @@ TEST (confirmation_height, callback_confirmed_history) ASSERT_TIMELY (10s, node->active.size () == 0); ASSERT_EQ (0, node->active.list_recently_cemented ().size ()); { - nano::lock_guard guard (node->active.mutex); + nano::lock_guard guard (node->active.mutex); ASSERT_EQ (0, node->active.blocks.size ()); } @@ -1151,7 +1151,7 @@ TEST (confirmation_height, cemented_gap_below_receive) } std::vector observer_order; - std::mutex mutex; + nano::mutex mutex; add_callback_stats (*node, &observer_order, &mutex); node->block_confirm (open1); @@ -1172,7 +1172,7 @@ TEST (confirmation_height, cemented_gap_below_receive) // Check that the order of callbacks is correct std::vector expected_order = { send.hash (), open.hash (), send1.hash (), receive1.hash (), send2.hash (), dummy_send.hash (), receive2.hash (), dummy_send1.hash (), send3.hash (), open1->hash () }; - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); ASSERT_EQ (observer_order, expected_order); }; diff --git a/nano/core_test/confirmation_solicitor.cpp b/nano/core_test/confirmation_solicitor.cpp index dbcf32902c..8a27ecf8f5 100644 --- a/nano/core_test/confirmation_solicitor.cpp +++ b/nano/core_test/confirmation_solicitor.cpp @@ -31,7 +31,7 @@ TEST (confirmation_solicitor, batches) auto send (std::make_shared (nano::genesis_hash, nano::keypair ().pub, nano::genesis_amount - 100, nano::dev_genesis_key.prv, nano::dev_genesis_key.pub, *system.work.generate (nano::genesis_hash))); send->sideband_set ({}); { - nano::lock_guard guard (node2.active.mutex); + nano::lock_guard guard (node2.active.mutex); for (size_t i (0); i < nano::network::confirm_req_hashes_max; ++i) { auto election (std::make_shared (node2, send, nullptr, nullptr, false, nano::election_behavior::normal)); @@ -115,7 +115,7 @@ TEST (confirmation_solicitor, bypass_max_requests_cap) // Add a vote for something else, not the winner for (auto const & rep : representatives) { - nano::lock_guard guard (election->mutex); + nano::lock_guard guard (election->mutex); election->last_votes[rep.account] = { std::chrono::steady_clock::now (), 1, 1 }; } ASSERT_FALSE (solicitor.add (*election)); diff --git a/nano/core_test/conflicts.cpp b/nano/core_test/conflicts.cpp index a7912336fb..6db6315434 100644 --- a/nano/core_test/conflicts.cpp +++ b/nano/core_test/conflicts.cpp @@ -166,7 +166,7 @@ TEST (conflicts, reprioritize) node1.process_active (send1); node1.block_processor.flush (); { - nano::lock_guard guard (node1.active.mutex); + nano::lock_guard guard (node1.active.mutex); auto existing1 (node1.active.roots.find (send1->qualified_root ())); ASSERT_NE (node1.active.roots.end (), existing1); ASSERT_EQ (multiplier1, existing1->multiplier); @@ -177,7 +177,7 @@ TEST (conflicts, reprioritize) node1.process_active (std::make_shared (send1_copy)); node1.block_processor.flush (); { - nano::lock_guard guard (node1.active.mutex); + nano::lock_guard guard (node1.active.mutex); auto existing2 (node1.active.roots.find (send1->qualified_root ())); ASSERT_NE (node1.active.roots.end (), existing2); ASSERT_EQ (multiplier2, existing2->multiplier); diff --git a/nano/core_test/election.cpp b/nano/core_test/election.cpp index 39807af31b..631bc2b81e 100644 --- a/nano/core_test/election.cpp +++ b/nano/core_test/election.cpp @@ -239,7 +239,7 @@ TEST (election, quorum_minimum_update_weight_before_quorum_checks) ASSERT_TIMELY (10s, !node1.rep_crawler.response (channel, vote2)); ASSERT_FALSE (election.election->confirmed ()); { - nano::lock_guard guard (node1.online_reps.mutex); + nano::lock_guard guard (node1.online_reps.mutex); // Modify online_m for online_reps to more than is available, this checks that voting below updates it to current online reps. node1.online_reps.online_m = node_config.online_weight_minimum.number () + 20; } diff --git a/nano/core_test/frontiers_confirmation.cpp b/nano/core_test/frontiers_confirmation.cpp index e38f1c8e51..18b57f45b8 100644 --- a/nano/core_test/frontiers_confirmation.cpp +++ b/nano/core_test/frontiers_confirmation.cpp @@ -168,7 +168,7 @@ TEST (frontiers_confirmation, prioritize_frontiers_max_optimistic_elections) } { - nano::unique_lock lk (node->active.mutex); + nano::unique_lock lk (node->active.mutex); node->active.frontiers_confirmation (lk); } @@ -179,7 +179,7 @@ TEST (frontiers_confirmation, prioritize_frontiers_max_optimistic_elections) // Call frontiers confirmation again and confirm that next_frontier_account hasn't changed { - nano::unique_lock lk (node->active.mutex); + nano::unique_lock lk (node->active.mutex); node->active.frontiers_confirmation (lk); } diff --git a/nano/core_test/gap_cache.cpp b/nano/core_test/gap_cache.cpp index f22cd23cb9..d87a071220 100644 --- a/nano/core_test/gap_cache.cpp +++ b/nano/core_test/gap_cache.cpp @@ -19,7 +19,7 @@ TEST (gap_cache, add_existing) nano::gap_cache cache (*system.nodes[0]); auto block1 (std::make_shared (0, 1, 2, nano::keypair ().prv, 4, 5)); cache.add (block1->hash ()); - nano::unique_lock lock (cache.mutex); + nano::unique_lock lock (cache.mutex); auto existing1 (cache.blocks.get<1> ().find (block1->hash ())); ASSERT_NE (cache.blocks.get<1> ().end (), existing1); auto arrival (existing1->arrival); @@ -39,7 +39,7 @@ TEST (gap_cache, comparison) nano::gap_cache cache (*system.nodes[0]); auto block1 (std::make_shared (1, 0, 2, nano::keypair ().prv, 4, 5)); cache.add (block1->hash ()); - nano::unique_lock lock (cache.mutex); + nano::unique_lock lock (cache.mutex); auto existing1 (cache.blocks.get<1> ().find (block1->hash ())); ASSERT_NE (cache.blocks.get<1> ().end (), existing1); auto arrival (existing1->arrival); diff --git a/nano/core_test/ledger.cpp b/nano/core_test/ledger.cpp index 04772dc726..531b8d8126 100644 --- a/nano/core_test/ledger.cpp +++ b/nano/core_test/ledger.cpp @@ -798,7 +798,7 @@ TEST (votes, add_one) auto existing1 (votes1.find (nano::dev_genesis_key.pub)); ASSERT_NE (votes1.end (), existing1); ASSERT_EQ (send1->hash (), existing1->second.hash); - nano::lock_guard guard (node1.active.mutex); + nano::lock_guard guard (node1.active.mutex); auto winner (*election1.election->tally ().begin ()); ASSERT_EQ (*send1, *winner.second); ASSERT_EQ (nano::genesis_amount - 100, winner.first); @@ -872,7 +872,7 @@ TEST (votes, add_existing) node1.work_generate_blocking (*send2); auto vote2 (std::make_shared (nano::dev_genesis_key.pub, nano::dev_genesis_key.prv, 2, send2)); // Pretend we've waited the timeout - nano::unique_lock lock (election1.election->mutex); + nano::unique_lock lock (election1.election->mutex); election1.election->last_votes[nano::dev_genesis_key.pub].time = std::chrono::steady_clock::now () - std::chrono::seconds (20); lock.unlock (); ASSERT_EQ (nano::vote_code::vote, node1.active.vote (vote2)); @@ -911,7 +911,7 @@ TEST (votes, add_old) node1.work_generate_blocking (*send2); auto vote2 (std::make_shared (nano::dev_genesis_key.pub, nano::dev_genesis_key.prv, 1, send2)); { - nano::lock_guard lock (election1.election->mutex); + nano::lock_guard lock (election1.election->mutex); election1.election->last_votes[nano::dev_genesis_key.pub].time = std::chrono::steady_clock::now () - std::chrono::seconds (20); } node1.vote_processor.vote_blocking (vote2, channel); diff --git a/nano/core_test/locks.cpp b/nano/core_test/locks.cpp index 89cb6d9ffb..8f680312b6 100644 --- a/nano/core_test/locks.cpp +++ b/nano/core_test/locks.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -6,7 +7,7 @@ #include #include -#if NANO_TIMED_LOCKS > 0 +#if USING_NANO_TIMED_LOCKS namespace { unsigned num_matches (std::string const & str) @@ -30,11 +31,11 @@ TEST (locks, no_conflicts) std::stringstream ss; nano::cout_redirect (ss.rdbuf ()); - std::mutex guard_mutex; - nano::lock_guard guard (guard_mutex); + nano::mutex guard_mutex; + nano::lock_guard guard (guard_mutex); - std::mutex lk_mutex; - nano::unique_lock lk (lk_mutex); + nano::mutex lk_mutex; + nano::unique_lock lk (lk_mutex); // This could fail if NANO_TIMED_LOCKS is such a low value that the above mutexes are held longer than that before reaching this statement ASSERT_EQ (ss.str (), ""); @@ -48,24 +49,21 @@ TEST (locks, lock_guard) std::stringstream ss; nano::cout_redirect redirect (ss.rdbuf ()); - std::mutex mutex; + nano::mutex mutex{ xstr (NANO_TIMED_LOCKS_FILTER) }; // Depending on timing the mutex could be reached first in std::promise promise; - std::thread t; - { - t = std::thread ([&mutex, &promise] { - nano::lock_guard guard (mutex); - promise.set_value (); - // Tries to make sure that the other guard to held for a minimum of NANO_TIMED_LOCKS, may need to increase this for low NANO_TIMED_LOCKS values - std::this_thread::sleep_for (std::chrono::milliseconds (NANO_TIMED_LOCKS * 2)); - }); - } + std::thread t ([&mutex, &promise] { + nano::lock_guard guard (mutex); + promise.set_value (); + // Tries to make sure that the other guard to held for a minimum of NANO_TIMED_LOCKS, may need to increase this for low NANO_TIMED_LOCKS values + std::this_thread::sleep_for (std::chrono::milliseconds (NANO_TIMED_LOCKS * 2)); + }); // Wait until the lock_guard has been reached in the other thread promise.get_future ().wait (); { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); t.join (); } @@ -85,12 +83,12 @@ TEST (locks, unique_lock) std::stringstream ss; nano::cout_redirect redirect (ss.rdbuf ()); - std::mutex mutex; + nano::mutex mutex{ xstr (NANO_TIMED_LOCKS_FILTER) }; // Depending on timing the mutex could be reached first in std::promise promise; std::thread t ([&mutex, &promise] { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); std::this_thread::sleep_for (std::chrono::milliseconds (NANO_TIMED_LOCKS)); lk.unlock (); lk.lock (); @@ -103,7 +101,7 @@ TEST (locks, unique_lock) // Wait until the lock_guard has been reached in the other thread promise.get_future ().wait (); { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); t.join (); } @@ -124,7 +122,7 @@ TEST (locks, condition_variable_wait) nano::cout_redirect redirect (ss.rdbuf ()); nano::condition_variable cv; - std::mutex mutex; + nano::mutex mutex; std::atomic notified{ false }; std::atomic finished{ false }; std::thread t ([&] { @@ -136,7 +134,7 @@ TEST (locks, condition_variable_wait) } }); - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); std::this_thread::sleep_for (std::chrono::milliseconds (NANO_TIMED_LOCKS)); cv.wait (lk, [¬ified] { return notified.load (); @@ -157,11 +155,11 @@ TEST (locks, condition_variable_wait_until) nano::cout_redirect redirect (ss.rdbuf ()); nano::condition_variable cv; - std::mutex mutex; + nano::mutex mutex; auto impl = [&](auto time_to_sleep) { std::atomic notified{ false }; std::atomic finished{ false }; - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); std::this_thread::sleep_for (std::chrono::milliseconds (time_to_sleep)); std::thread t ([&] { while (!finished) @@ -189,8 +187,8 @@ TEST (locks, condition_variable_wait_until) TEST (locks, defer_lock) { - std::mutex mutex; - nano::unique_lock lock (mutex, std::defer_lock); + nano::mutex mutex; + nano::unique_lock lock (mutex, std::defer_lock); ASSERT_FALSE (lock.owns_lock ()); ASSERT_TRUE (lock.try_lock ()); ASSERT_TRUE (lock.owns_lock ()); diff --git a/nano/core_test/network.cpp b/nano/core_test/network.cpp index e451fe9497..c03515637c 100644 --- a/nano/core_test/network.cpp +++ b/nano/core_test/network.cpp @@ -762,7 +762,7 @@ TEST (tcp_listener, tcp_listener_timeout_empty) while (!disconnected) { { - nano::lock_guard guard (node0->bootstrap.mutex); + nano::lock_guard guard (node0->bootstrap.mutex); disconnected = node0->bootstrap.connections.empty (); } ASSERT_NO_ERROR (system.poll ()); @@ -786,7 +786,7 @@ TEST (tcp_listener, tcp_listener_timeout_node_id_handshake) }); ASSERT_TIMELY (5s, node0->stats.count (nano::stat::type::message, nano::stat::detail::node_id_handshake) != 0); { - nano::lock_guard guard (node0->bootstrap.mutex); + nano::lock_guard guard (node0->bootstrap.mutex); ASSERT_EQ (node0->bootstrap.connections.size (), 1); } bool disconnected (false); @@ -794,7 +794,7 @@ TEST (tcp_listener, tcp_listener_timeout_node_id_handshake) while (!disconnected) { { - nano::lock_guard guard (node0->bootstrap.mutex); + nano::lock_guard guard (node0->bootstrap.mutex); disconnected = node0->bootstrap.connections.empty (); } ASSERT_NO_ERROR (system.poll ()); diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index ce606bd10e..de657305a8 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -424,7 +424,7 @@ TEST (node, search_pending_confirmed) system.wallet (0)->insert_adhoc (key2.prv); ASSERT_FALSE (system.wallet (0)->search_pending (system.wallet (0)->wallets.tx_begin_read ())); { - nano::lock_guard guard (node->active.mutex); + nano::lock_guard guard (node->active.mutex); auto existing1 (node->active.blocks.find (send1->hash ())); ASSERT_EQ (node->active.blocks.end (), existing1); auto existing2 (node->active.blocks.find (send2->hash ())); @@ -470,7 +470,7 @@ TEST (node, search_pending_pruned) system.wallet (1)->insert_adhoc (key2.prv); ASSERT_FALSE (system.wallet (1)->search_pending (system.wallet (1)->wallets.tx_begin_read ())); { - nano::lock_guard guard (node2->active.mutex); + nano::lock_guard guard (node2->active.mutex); auto existing1 (node2->active.blocks.find (send1->hash ())); ASSERT_EQ (node2->active.blocks.end (), existing1); auto existing2 (node2->active.blocks.find (send2->hash ())); @@ -2018,7 +2018,7 @@ TEST (node, bootstrap_confirm_frontiers) ASSERT_NO_ERROR (system1.poll ()); } { - nano::lock_guard guard (node1->active.mutex); + nano::lock_guard guard (node1->active.mutex); auto existing1 (node1->active.blocks.find (send0.hash ())); ASSERT_NE (node1->active.blocks.end (), existing1); } @@ -2445,7 +2445,7 @@ TEST (node, online_reps_rep_crawler) ASSERT_EQ (0, node1.online_reps.online ()); // After inserting to rep crawler { - nano::lock_guard guard (node1.rep_crawler.probable_reps_mutex); + nano::lock_guard guard (node1.rep_crawler.probable_reps_mutex); node1.rep_crawler.active.insert (nano::genesis_hash); } node1.vote_processor.vote_blocking (vote, std::make_shared (node1)); @@ -3101,7 +3101,7 @@ TEST (node, epoch_conflict_confirm) nano::blocks_confirm (*node0, { change, epoch_open }); ASSERT_EQ (2, node0->active.size ()); { - nano::lock_guard lock (node0->active.mutex); + nano::lock_guard lock (node0->active.mutex); ASSERT_TRUE (node0->active.blocks.find (change->hash ()) != node0->active.blocks.end ()); ASSERT_TRUE (node0->active.blocks.find (epoch_open->hash ()) != node0->active.blocks.end ()); } @@ -3894,7 +3894,7 @@ TEST (active_difficulty, recalculate_work) ASSERT_TIMELY (2s, !node1.active.empty ()); auto sum (std::accumulate (node1.active.multipliers_cb.begin (), node1.active.multipliers_cb.end (), double(0))); ASSERT_EQ (node1.active.active_difficulty (), nano::difficulty::from_multiplier (sum / node1.active.multipliers_cb.size (), node1.network_params.network.publish_thresholds.epoch_2)); - nano::unique_lock lock (node1.active.mutex); + nano::unique_lock lock (node1.active.mutex); // Fake history records to force work recalculation for (auto i (0); i < node1.active.multipliers_cb.size (); i++) { @@ -4178,7 +4178,7 @@ TEST (node, dependency_graph) ASSERT_NO_ERROR (system.poll_until_true (15s, [&] { // Not many blocks should be active simultaneously EXPECT_LT (node.active.size (), 6); - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); // Ensure that active blocks have their ancestors confirmed auto error = std::any_of (dependency_graph.cbegin (), dependency_graph.cend (), [&](auto entry) { @@ -4456,7 +4456,7 @@ TEST (node, deferred_dependent_elections) // Frontier confirmation also starts elections ASSERT_NO_ERROR (system.poll_until_true (5s, [&node, &send2] { - nano::unique_lock lock (node.active.mutex); + nano::unique_lock lock (node.active.mutex); node.active.frontiers_confirmation (lock); lock.unlock (); return node.active.election (send2->qualified_root ()) != nullptr; @@ -4536,7 +4536,7 @@ TEST (rep_crawler, local) auto loopback = std::make_shared (node); auto vote = std::make_shared (nano::dev_genesis_key.pub, nano::dev_genesis_key.prv, 0, std::vector{ nano::genesis_hash }); { - nano::lock_guard guard (node.rep_crawler.probable_reps_mutex); + nano::lock_guard guard (node.rep_crawler.probable_reps_mutex); node.rep_crawler.active.insert (nano::genesis_hash); node.rep_crawler.responses.emplace_back (loopback, vote); } diff --git a/nano/core_test/telemetry.cpp b/nano/core_test/telemetry.cpp index 96b9036f53..bd365a71cc 100644 --- a/nano/core_test/telemetry.cpp +++ b/nano/core_test/telemetry.cpp @@ -565,7 +565,7 @@ TEST (telemetry, remove_peer_different_genesis) ASSERT_EQ (node0->stats.count (nano::stat::type::message, nano::stat::detail::node_id_handshake, nano::stat::dir::out), 1); ASSERT_EQ (node1->stats.count (nano::stat::type::message, nano::stat::detail::node_id_handshake, nano::stat::dir::out), 1); - nano::lock_guard guard (node0->network.excluded_peers.mutex); + nano::lock_guard guard (node0->network.excluded_peers.mutex); ASSERT_EQ (1, node0->network.excluded_peers.peers.get ().count (node1->network.endpoint ().address ())); ASSERT_EQ (1, node1->network.excluded_peers.peers.get ().count (node0->network.endpoint ().address ())); } @@ -609,7 +609,7 @@ TEST (telemetry, remove_peer_different_genesis_udp) ASSERT_EQ (node0->network.tcp_channels.size (), 0); ASSERT_EQ (node1->network.tcp_channels.size (), 0); - nano::lock_guard guard (node0->network.excluded_peers.mutex); + nano::lock_guard guard (node0->network.excluded_peers.mutex); ASSERT_EQ (1, node0->network.excluded_peers.peers.get ().count (node1->network.endpoint ().address ())); ASSERT_EQ (1, node1->network.excluded_peers.peers.get ().count (node0->network.endpoint ().address ())); } @@ -636,7 +636,7 @@ TEST (telemetry, remove_peer_invalid_signature) ASSERT_TIMELY (10s, node->stats.count (nano::stat::type::telemetry, nano::stat::detail::invalid_signature) > 0); ASSERT_NO_ERROR (system.poll_until_true (3s, [&node, address = channel->get_endpoint ().address ()]() -> bool { - nano::lock_guard guard (node->network.excluded_peers.mutex); + nano::lock_guard guard (node->network.excluded_peers.mutex); return node->network.excluded_peers.peers.get ().count (address); })); } diff --git a/nano/core_test/utility.cpp b/nano/core_test/utility.cpp index 7696f1b762..0ae8a5a3f1 100644 --- a/nano/core_test/utility.cpp +++ b/nano/core_test/utility.cpp @@ -120,16 +120,16 @@ TEST (thread_pool_alarm, one) { nano::thread_pool workers (1u, nano::thread_role::name::unknown); std::atomic done (false); - std::mutex mutex; + nano::mutex mutex; nano::condition_variable condition; workers.add_timed_task (std::chrono::steady_clock::now (), [&]() { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); done = true; } condition.notify_one (); }); - nano::unique_lock unique (mutex); + nano::unique_lock unique (mutex); condition.wait (unique, [&]() { return !!done; }); } @@ -137,19 +137,19 @@ TEST (thread_pool_alarm, many) { nano::thread_pool workers (50u, nano::thread_role::name::unknown); std::atomic count (0); - std::mutex mutex; + nano::mutex mutex; nano::condition_variable condition; for (auto i (0); i < 50; ++i) { workers.add_timed_task (std::chrono::steady_clock::now (), [&]() { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); count += 1; } condition.notify_one (); }); } - nano::unique_lock unique (mutex); + nano::unique_lock unique (mutex); condition.wait (unique, [&]() { return count == 50; }); } @@ -158,20 +158,20 @@ TEST (thread_pool_alarm, top_execution) nano::thread_pool workers (1u, nano::thread_role::name::unknown); int value1 (0); int value2 (0); - std::mutex mutex; + nano::mutex mutex; std::promise promise; workers.add_timed_task (std::chrono::steady_clock::now (), [&]() { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); value1 = 1; value2 = 1; }); workers.add_timed_task (std::chrono::steady_clock::now () + std::chrono::milliseconds (1), [&]() { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); value2 = 2; promise.set_value (false); }); promise.get_future ().get (); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); ASSERT_EQ (1, value1); ASSERT_EQ (2, value2); } diff --git a/nano/core_test/voting.cpp b/nano/core_test/voting.cpp index ab367f9613..d42e9d09d6 100644 --- a/nano/core_test/voting.cpp +++ b/nano/core_test/voting.cpp @@ -208,23 +208,23 @@ TEST (vote_spacing, rapid) wallet.insert_adhoc (nano::dev_genesis_key.prv); nano::state_block_builder builder; auto send1 = builder.make_block () - .account (nano::dev_genesis_key.pub) - .previous (nano::genesis_hash) - .representative (nano::dev_genesis_key.pub) - .balance (nano::genesis_amount - nano::Gxrb_ratio) - .link (nano::dev_genesis_key.pub) - .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) - .work (*system.work.generate (nano::genesis_hash)) - .build_shared (); + .account (nano::dev_genesis_key.pub) + .previous (nano::genesis_hash) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - nano::Gxrb_ratio) + .link (nano::dev_genesis_key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (nano::genesis_hash)) + .build_shared (); auto send2 = builder.make_block () - .account (nano::dev_genesis_key.pub) - .previous (nano::genesis_hash) - .representative (nano::dev_genesis_key.pub) - .balance (nano::genesis_amount - nano::Gxrb_ratio - 1) - .link (nano::dev_genesis_key.pub) - .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) - .work (*system.work.generate (nano::genesis_hash)) - .build_shared (); + .account (nano::dev_genesis_key.pub) + .previous (nano::genesis_hash) + .representative (nano::dev_genesis_key.pub) + .balance (nano::genesis_amount - nano::Gxrb_ratio - 1) + .link (nano::dev_genesis_key.pub) + .sign (nano::dev_genesis_key.prv, nano::dev_genesis_key.pub) + .work (*system.work.generate (nano::genesis_hash)) + .build_shared (); ASSERT_EQ (nano::process_result::progress, node.ledger.process (node.store.tx_begin_write (), *send1).code); node.active.generator.add (nano::genesis_hash, send1->hash ()); ASSERT_TIMELY (3s, node.stats.count (nano::stat::type::vote_generator, nano::stat::detail::generator_broadcasts) == 1); diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index b39d478d9b..44a4581a7b 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -1014,7 +1014,7 @@ TEST (wallet, limited_difficulty) wallet.insert_adhoc (nano::dev_genesis_key.prv, false); { // Force active difficulty to an impossibly high value - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); node.active.trended_active_multiplier = 1024 * 1024 * 1024; } ASSERT_EQ (node.max_work_generate_difficulty (nano::work_version::work_1), node.active.limited_active_difficulty (*genesis.open)); @@ -1102,7 +1102,7 @@ TEST (wallet, epoch_2_receive_propagation) // Receiving should use the lower difficulty { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); node.active.trended_active_multiplier = 1.0; } auto receive2 = wallet.receive_action (send2->hash (), key.pub, amount, send2->link ().as_account (), 1); @@ -1152,7 +1152,7 @@ TEST (wallet, epoch_2_receive_unopened) // Receiving should use the lower difficulty { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); node.active.trended_active_multiplier = 1.0; } auto receive1 = wallet.receive_action (send1->hash (), key.pub, amount, send1->link ().as_account (), 1); diff --git a/nano/core_test/wallets.cpp b/nano/core_test/wallets.cpp index 3579ea3ae5..398bb5ee17 100644 --- a/nano/core_test/wallets.cpp +++ b/nano/core_test/wallets.cpp @@ -106,7 +106,7 @@ TEST (wallets, reload) ASSERT_FALSE (error); ASSERT_EQ (1, node1.wallets.items.size ()); { - nano::lock_guard lock_wallet (node1.wallets.mutex); + nano::lock_guard lock_wallet (node1.wallets.mutex); nano::inactive_node node (node1.application_path, nano::inactive_node_flag_defaults ()); auto wallet (node.node->wallets.create (one)); ASSERT_NE (wallet, nullptr); @@ -132,7 +132,7 @@ TEST (wallets, vote_minimum) nano::state_block open2 (key2.pub, 0, key2.pub, node1.config.vote_minimum.number () - 1, send2.hash (), key2.prv, key2.pub, *system.work.generate (key2.pub)); ASSERT_EQ (nano::process_result::progress, node1.process (open2).code); auto wallet (node1.wallets.items.begin ()->second); - nano::unique_lock representatives_lk (wallet->representatives_mutex); + nano::unique_lock representatives_lk (wallet->representatives_mutex); ASSERT_EQ (0, wallet->representatives.size ()); representatives_lk.unlock (); wallet->insert_adhoc (nano::dev_genesis_key.prv); @@ -180,7 +180,7 @@ TEST (wallets, search_pending) flags.disable_search_pending = true; auto & node (*system.add_node (config, flags)); - nano::unique_lock lk (node.wallets.mutex); + nano::unique_lock lk (node.wallets.mutex); auto wallets = node.wallets.get_wallets (); lk.unlock (); ASSERT_EQ (1, wallets.size ()); diff --git a/nano/core_test/websocket.cpp b/nano/core_test/websocket.cpp index ea0f20a9c6..a6039e7f34 100644 --- a/nano/core_test/websocket.cpp +++ b/nano/core_test/websocket.cpp @@ -79,7 +79,7 @@ TEST (websocket, active_difficulty) // Fake history records and force a trended_active_multiplier change { - nano::unique_lock lock (node1->active.mutex); + nano::unique_lock lock (node1->active.mutex); node1->active.multipliers_cb.push_front (10.); node1->active.update_active_multiplier (lock); } diff --git a/nano/core_test/work_watcher.cpp b/nano/core_test/work_watcher.cpp index 443038b329..37e79df87b 100644 --- a/nano/core_test/work_watcher.cpp +++ b/nano/core_test/work_watcher.cpp @@ -26,14 +26,14 @@ TEST (work_watcher, update) auto multiplier2 (nano::normalized_multiplier (nano::difficulty::to_multiplier (difficulty2, nano::work_threshold (block2->work_version (), nano::block_details (nano::epoch::epoch_0, true, false, false))), node.network_params.network.publish_thresholds.epoch_1)); double updated_multiplier1{ multiplier1 }, updated_multiplier2{ multiplier2 }, target_multiplier{ std::max (multiplier1, multiplier2) + 1e-6 }; { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); node.active.trended_active_multiplier = target_multiplier; } system.deadline_set (20s); while (updated_multiplier1 == multiplier1 || updated_multiplier2 == multiplier2) { { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); { auto const existing (node.active.roots.find (block1->qualified_root ())); //if existing is junk the block has been confirmed already @@ -74,7 +74,7 @@ TEST (work_watcher, propagate) auto updated_multiplier{ multiplier }; auto propagated_multiplier{ multiplier }; { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); node.active.trended_active_multiplier = multiplier * 1.001; } bool updated{ false }; @@ -83,7 +83,7 @@ TEST (work_watcher, propagate) while (!(updated && propagated)) { { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); { auto const existing (node.active.roots.find (block->qualified_root ())); ASSERT_NE (existing, node.active.roots.end ()); @@ -91,7 +91,7 @@ TEST (work_watcher, propagate) } } { - nano::lock_guard guard (node_passive.active.mutex); + nano::lock_guard guard (node_passive.active.mutex); { auto const existing (node_passive.active.roots.find (block->qualified_root ())); ASSERT_NE (existing, node_passive.active.roots.end ()); @@ -165,13 +165,13 @@ TEST (work_watcher, generation_disabled) auto multiplier = nano::normalized_multiplier (nano::difficulty::to_multiplier (difficulty, nano::work_threshold (block->work_version (), nano::block_details (nano::epoch::epoch_0, true, false, false))), node.network_params.network.publish_thresholds.epoch_1); double updated_multiplier{ multiplier }; { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); node.active.trended_active_multiplier = multiplier * 10; } std::this_thread::sleep_for (2s); ASSERT_TRUE (node.wallets.watcher->is_watched (block->qualified_root ())); { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); auto const existing (node.active.roots.find (block->qualified_root ())); ASSERT_NE (existing, node.active.roots.end ()); updated_multiplier = existing->multiplier; @@ -194,7 +194,7 @@ TEST (work_watcher, cancel) auto work1 (node.work_generate_blocking (nano::dev_genesis_key.pub)); auto const block1 (wallet.send_action (nano::dev_genesis_key.pub, key.pub, 100, *work1, false)); { - nano::unique_lock lock (node.active.mutex); + nano::unique_lock lock (node.active.mutex); // Prevent active difficulty repopulating multipliers node.network_params.network.request_interval_ms = 10000; // Fill multipliers_cb and update active difficulty; @@ -240,7 +240,7 @@ TEST (work_watcher, confirm_while_generating) auto work1 (node.work_generate_blocking (nano::dev_genesis_key.pub)); auto const block1 (wallet.send_action (nano::dev_genesis_key.pub, key.pub, 100, *work1, false)); { - nano::unique_lock lock (node.active.mutex); + nano::unique_lock lock (node.active.mutex); // Prevent active difficulty repopulating multipliers node.network_params.network.request_interval_ms = 10000; // Fill multipliers_cb and update active difficulty; diff --git a/nano/crypto_lib/random_pool.cpp b/nano/crypto_lib/random_pool.cpp index baaa7e7813..4b57d14ff8 100644 --- a/nano/crypto_lib/random_pool.cpp +++ b/nano/crypto_lib/random_pool.cpp @@ -7,21 +7,21 @@ std::mutex nano::random_pool::mutex; void nano::random_pool::generate_block (unsigned char * output, size_t size) { auto & pool = get_pool (); - std::lock_guard guard (mutex); + std::lock_guard guard (mutex); pool.GenerateBlock (output, size); } unsigned nano::random_pool::generate_word32 (unsigned min, unsigned max) { auto & pool = get_pool (); - std::lock_guard guard (mutex); + std::lock_guard guard (mutex); return pool.GenerateWord32 (min, max); } unsigned char nano::random_pool::generate_byte () { auto & pool = get_pool (); - std::lock_guard guard (mutex); + std::lock_guard guard (mutex); return pool.GenerateByte (); } diff --git a/nano/crypto_lib/random_pool_shuffle.hpp b/nano/crypto_lib/random_pool_shuffle.hpp index 848127efee..0033481041 100644 --- a/nano/crypto_lib/random_pool_shuffle.hpp +++ b/nano/crypto_lib/random_pool_shuffle.hpp @@ -9,7 +9,7 @@ namespace nano template void random_pool_shuffle (Iter begin, Iter end) { - std::lock_guard guard (random_pool::mutex); + std::lock_guard guard (random_pool::mutex); random_pool::get_pool ().Shuffle (begin, end); } } diff --git a/nano/lib/blocks.cpp b/nano/lib/blocks.cpp index 09b3ffca93..16ac85eadb 100644 --- a/nano/lib/blocks.cpp +++ b/nano/lib/blocks.cpp @@ -1851,7 +1851,7 @@ std::shared_ptr nano::block_uniquer::unique (std::shared_ptrfull_hash ()); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto & existing (blocks[key]); if (auto block_l = existing.lock ()) { @@ -1888,7 +1888,7 @@ std::shared_ptr nano::block_uniquer::unique (std::shared_ptr lock (mutex); + nano::lock_guard lock (mutex); return blocks.size (); } diff --git a/nano/lib/blocks.hpp b/nano/lib/blocks.hpp index a63408632d..29011bacaf 100644 --- a/nano/lib/blocks.hpp +++ b/nano/lib/blocks.hpp @@ -408,7 +408,7 @@ class block_uniquer size_t size (); private: - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::block_uniquer) }; std::unordered_map, value_type::second_type> blocks; static unsigned constexpr cleanup_count = 2; }; diff --git a/nano/lib/locks.cpp b/nano/lib/locks.cpp index a62279a5e8..88a4de21ec 100644 --- a/nano/lib/locks.cpp +++ b/nano/lib/locks.cpp @@ -1,20 +1,25 @@ -#if NANO_TIMED_LOCKS > 0 +#include #include #include +#include + +#include #include +#if USING_NANO_TIMED_LOCKS namespace nano { +// These mutexes must have std::mutex interface in addition to "const char* get_name ()" method template void output (const char * str, std::chrono::milliseconds time, Mutex & mutex) { - static std::mutex cout_mutex; + static nano::mutex cout_mutex; auto stacktrace = nano::generate_stacktrace (); // Guard standard out to keep the output from being interleaved - std::lock_guard guard (cout_mutex); - std::cout << std::addressof (mutex) << " Mutex " << str << " for: " << time.count () << "ms\n" - << stacktrace << std::endl; + std::lock_guard guard (cout_mutex); + std::cout << (boost::format ("%1% Mutex %2% %3% for %4%ms\n%5%") % std::addressof (mutex) % mutex.get_name () % str % time.count () % stacktrace).str () + << std::endl; } template @@ -23,7 +28,12 @@ void output_if_held_long_enough (nano::timer & timer, auto time_held = timer.since_start (); if (time_held >= std::chrono::milliseconds (NANO_TIMED_LOCKS)) { - output ("held", time_held, mutex); + std::unique_lock lk (nano::mutex_to_filter_mutex); + if (!nano::any_filters_registered () || (nano::mutex_to_filter == &mutex)) + { + lk.unlock (); + output ("held", time_held, mutex); + } } if (timer.current_state () != nano::timer_state::stopped) { @@ -38,19 +48,17 @@ void output_if_blocked_long_enough (nano::timer & tim auto time_blocked = timer.since_start (); if (time_blocked >= std::chrono::milliseconds (NANO_TIMED_LOCKS)) { - output ("blocked", time_blocked, mutex); + std::unique_lock lk (nano::mutex_to_filter_mutex); + if (!nano::any_filters_registered () || (nano::mutex_to_filter == &mutex)) + { + lk.unlock (); + output ("blocked", time_blocked, mutex); + } } } #endif -// Explicit instantations -template void output (const char * str, std::chrono::milliseconds time, std::mutex & mutex); -template void output_if_held_long_enough (nano::timer & timer, std::mutex & mutex); -#ifndef NANO_TIMED_LOCKS_IGNORE_BLOCKED -template void output_if_blocked_long_enough (nano::timer & timer, std::mutex & mutex); -#endif - -lock_guard::lock_guard (std::mutex & mutex) : +lock_guard::lock_guard (nano::mutex & mutex) : mut (mutex) { timer.start (); @@ -61,7 +69,7 @@ mut (mutex) #endif } -lock_guard::~lock_guard () noexcept +lock_guard::~lock_guard () noexcept { mut.unlock (); output_if_held_long_enough (timer, mut); @@ -195,7 +203,7 @@ void unique_lock::validate () const } // Explicit instantiations for allowed types -template class unique_lock; +template class unique_lock; void condition_variable::notify_one () noexcept { @@ -207,7 +215,7 @@ void condition_variable::notify_all () noexcept cnd.notify_all (); } -void condition_variable::wait (nano::unique_lock & lk) +void condition_variable::wait (nano::unique_lock & lk) { if (!lk.mut || !lk.owns) { @@ -220,5 +228,66 @@ void condition_variable::wait (nano::unique_lock & lk) cnd.wait (lk); lk.timer.restart (); } +template class unique_lock; + +nano::mutex * mutex_to_filter{ nullptr }; +nano::mutex mutex_to_filter_mutex; + +bool should_be_filtered (const char * name) +{ + return std::strcmp (name, xstr (NANO_TIMED_LOCKS_FILTER)) == 0; +} + +bool any_filters_registered () +{ + return std::strcmp ("", xstr (NANO_TIMED_LOCKS_FILTER)) != 0; +} } #endif + +char const * nano::mutex_identifier (mutexes mutex) +{ + switch (mutex) + { + case mutexes::active: + return "active"; + case mutexes::block_arrival: + return "block_arrival"; + case mutexes::block_processor: + return "block_processor"; + case mutexes::block_uniquer: + return "block_uniquer"; + case mutexes::blockstore_cache: + return "blockstore_cache"; + case mutexes::confirmation_height_processor: + return "confirmation_height_processor"; + case mutexes::dropped_elections: + return "dropped_elections"; + case mutexes::election_winner_details: + return "election_winner_details"; + case mutexes::gap_cache: + return "gap_cache"; + case mutexes::network_filter: + return "network_filter"; + case mutexes::observer_set: + return "observer_set"; + case mutexes::request_aggregator: + return "request_aggregator"; + case mutexes::state_block_signature_verification: + return "state_block_signature_verification"; + case mutexes::telemetry: + return "telemetry"; + case mutexes::vote_generator: + return "vote_generator"; + case mutexes::vote_processor: + return "vote_processor"; + case mutexes::vote_uniquer: + return "vote_uniquer"; + case mutexes::votes_cache: + return "votes_cache"; + case mutexes::work_pool: + return "work_pool"; + } + + throw std::runtime_error ("Invalid mutexes enum specified"); +} \ No newline at end of file diff --git a/nano/lib/locks.hpp b/nano/lib/locks.hpp index b4394c2bee..d7a2da6eb4 100644 --- a/nano/lib/locks.hpp +++ b/nano/lib/locks.hpp @@ -1,6 +1,8 @@ #pragma once -#if NANO_TIMED_LOCKS > 0 +#define USING_NANO_TIMED_LOCKS (NANO_TIMED_LOCKS > 0) + +#if USING_NANO_TIMED_LOCKS #include #endif @@ -9,15 +11,110 @@ namespace nano { -#if NANO_TIMED_LOCKS > 0 +class mutex; +extern nano::mutex * mutex_to_filter; +extern nano::mutex mutex_to_filter_mutex; +bool should_be_filtered (const char * name); +bool any_filters_registered (); + +enum class mutexes +{ + active, + block_arrival, + block_processor, + block_uniquer, + blockstore_cache, + confirmation_height_processor, + dropped_elections, + election_winner_details, + gap_cache, + network_filter, + observer_set, + request_aggregator, + state_block_signature_verification, + telemetry, + vote_generator, + vote_processor, + vote_uniquer, + votes_cache, + work_pool +}; + +char const * mutex_identifier (mutexes mutex); + +class mutex +{ +public: + mutex () = default; + mutex (const char * name_a) +#if USING_NANO_TIMED_LOCKS + : + name (name_a) +#endif + { +#if USING_NANO_TIMED_LOCKS + // This mutex should be filtered + if (name && should_be_filtered (name)) + { + std::lock_guard guard (mutex_to_filter_mutex); + mutex_to_filter = this; + } +#endif + } + +#if USING_NANO_TIMED_LOCKS + ~mutex () + { + // Unfilter this destroyed mutex + if (name && should_be_filtered (name)) + { + // Unregister the mutex + std::lock_guard guard (mutex_to_filter_mutex); + mutex_to_filter = nullptr; + } + } +#endif + + void lock () + { + mutex_m.lock (); + } + + void unlock () + { + mutex_m.unlock (); + } + + bool try_lock () + { + return mutex_m.try_lock (); + } + +#if USING_NANO_TIMED_LOCKS + const char * get_name () const + { + return name ? name : ""; + } +#endif + +private: +#if USING_NANO_TIMED_LOCKS + const char * name{ nullptr }; +#endif + std::mutex mutex_m; +}; + +#if USING_NANO_TIMED_LOCKS template void output (const char * str, std::chrono::milliseconds time, Mutex & mutex); template void output_if_held_long_enough (nano::timer & timer, Mutex & mutex); +#ifndef NANO_TIMED_LOCKS_IGNORE_BLOCKED template void output_if_blocked_long_enough (nano::timer & timer, Mutex & mutex); +#endif template class lock_guard final @@ -36,21 +133,21 @@ class lock_guard final }; template <> -class lock_guard final +class lock_guard final { public: - explicit lock_guard (std::mutex & mutex_a); + explicit lock_guard (nano::mutex & mutex_a); ~lock_guard () noexcept; lock_guard (const lock_guard &) = delete; lock_guard & operator= (const lock_guard &) = delete; private: - std::mutex & mut; + nano::mutex & mut; nano::timer timer; }; -template ::value>> +template ::value>> class unique_lock final { public: @@ -94,10 +191,10 @@ class condition_variable final void notify_one () noexcept; void notify_all () noexcept; - void wait (nano::unique_lock & lt); + void wait (nano::unique_lock & lt); template - void wait (nano::unique_lock & lk, Pred pred) + void wait (nano::unique_lock & lk, Pred pred) { while (!pred ()) { @@ -106,7 +203,7 @@ class condition_variable final } template - std::cv_status wait_until (nano::unique_lock & lk, std::chrono::time_point const & timeout_time) + std::cv_status wait_until (nano::unique_lock & lk, std::chrono::time_point const & timeout_time) { if (!lk.mut || !lk.owns) { @@ -122,7 +219,7 @@ class condition_variable final } template - bool wait_until (nano::unique_lock & lk, std::chrono::time_point const & timeout_time, Pred pred) + bool wait_until (nano::unique_lock & lk, std::chrono::time_point const & timeout_time, Pred pred) { while (!pred ()) { @@ -135,13 +232,13 @@ class condition_variable final } template - void wait_for (nano::unique_lock & lk, std::chrono::duration const & rel_time) + void wait_for (nano::unique_lock & lk, std::chrono::duration const & rel_time) { wait_until (lk, std::chrono::steady_clock::now () + rel_time); } template - bool wait_for (nano::unique_lock & lk, std::chrono::duration const & rel_time, Pred pred) + bool wait_for (nano::unique_lock & lk, std::chrono::duration const & rel_time, Pred pred) { return wait_until (lk, std::chrono::steady_clock::now () + rel_time, std::move (pred)); } @@ -212,7 +309,7 @@ class locked T & operator= (T const & other) { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); obj = other; return obj; } @@ -230,6 +327,6 @@ class locked private: T obj; - std::mutex mutex; + nano::mutex mutex; }; } diff --git a/nano/lib/logger_mt.hpp b/nano/lib/logger_mt.hpp index 5f7744a4fe..b6e0183c2c 100644 --- a/nano/lib/logger_mt.hpp +++ b/nano/lib/logger_mt.hpp @@ -108,7 +108,7 @@ class logger_mt { auto error (true); auto time_now = std::chrono::steady_clock::now (); - nano::unique_lock lk (last_log_time_mutex); + nano::unique_lock lk (last_log_time_mutex); if (((time_now - last_log_time) > min_log_delta) || last_log_time == std::chrono::steady_clock::time_point{}) { last_log_time = time_now; @@ -132,7 +132,7 @@ class logger_mt std::chrono::milliseconds min_log_delta{ 0 }; private: - std::mutex last_log_time_mutex; + nano::mutex last_log_time_mutex; std::chrono::steady_clock::time_point last_log_time; boost::log::sources::severity_logger_mt boost_logger_mt; }; diff --git a/nano/lib/rate_limiting.cpp b/nano/lib/rate_limiting.cpp index 2baabc833f..5e2a0930cb 100644 --- a/nano/lib/rate_limiting.cpp +++ b/nano/lib/rate_limiting.cpp @@ -20,7 +20,7 @@ nano::rate::token_bucket::token_bucket (size_t max_token_count_a, size_t refill_ bool nano::rate::token_bucket::try_consume (unsigned tokens_required_a) { debug_assert (tokens_required_a <= 1e9); - nano::lock_guard lk (bucket_mutex); + nano::lock_guard lk (bucket_mutex); refill (); bool possible = current_size >= tokens_required_a; if (possible) @@ -48,6 +48,6 @@ void nano::rate::token_bucket::refill () size_t nano::rate::token_bucket::largest_burst () const { - nano::lock_guard lk (bucket_mutex); + nano::lock_guard lk (bucket_mutex); return max_token_count - smallest_size; } diff --git a/nano/lib/rate_limiting.hpp b/nano/lib/rate_limiting.hpp index 9271535fbe..6dd25538d3 100644 --- a/nano/lib/rate_limiting.hpp +++ b/nano/lib/rate_limiting.hpp @@ -49,7 +49,7 @@ namespace rate /** The minimum observed bucket size, from which the largest burst can be derived */ size_t smallest_size{ 0 }; std::chrono::steady_clock::time_point last_refill; - mutable std::mutex bucket_mutex; + mutable nano::mutex bucket_mutex; }; } } diff --git a/nano/lib/rep_weights.cpp b/nano/lib/rep_weights.cpp index b4da06b4db..efdccc598d 100644 --- a/nano/lib/rep_weights.cpp +++ b/nano/lib/rep_weights.cpp @@ -3,7 +3,7 @@ void nano::rep_weights::representation_add (nano::account const & source_rep_a, nano::uint128_t const & amount_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto source_previous (get (source_rep_a)); put (source_rep_a, source_previous + amount_a); } @@ -12,7 +12,7 @@ void nano::rep_weights::representation_add_dual (nano::account const & source_re { if (source_rep_1 != source_rep_2) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto source_previous_1 (get (source_rep_1)); put (source_rep_1, source_previous_1 + amount_1); auto source_previous_2 (get (source_rep_2)); @@ -26,27 +26,27 @@ void nano::rep_weights::representation_add_dual (nano::account const & source_re void nano::rep_weights::representation_put (nano::account const & account_a, nano::uint128_union const & representation_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); put (account_a, representation_a); } nano::uint128_t nano::rep_weights::representation_get (nano::account const & account_a) const { - nano::lock_guard lk (mutex); + nano::lock_guard lk (mutex); return get (account_a); } /** Makes a copy */ std::unordered_map nano::rep_weights::get_rep_amounts () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return rep_amounts; } void nano::rep_weights::copy_from (nano::rep_weights & other_a) { - nano::lock_guard guard_this (mutex); - nano::lock_guard guard_other (other_a.mutex); + nano::lock_guard guard_this (mutex); + nano::lock_guard guard_other (other_a.mutex); for (auto const & entry : other_a.rep_amounts) { auto prev_amount (get (entry.first)); @@ -86,7 +86,7 @@ std::unique_ptr nano::collect_container_info (na size_t rep_amounts_count; { - nano::lock_guard guard (rep_weights.mutex); + nano::lock_guard guard (rep_weights.mutex); rep_amounts_count = rep_weights.rep_amounts.size (); } auto sizeof_element = sizeof (decltype (rep_weights.rep_amounts)::value_type); diff --git a/nano/lib/rep_weights.hpp b/nano/lib/rep_weights.hpp index 36f1684927..cce5140497 100644 --- a/nano/lib/rep_weights.hpp +++ b/nano/lib/rep_weights.hpp @@ -23,7 +23,7 @@ class rep_weights void copy_from (rep_weights & other_a); private: - mutable std::mutex mutex; + mutable nano::mutex mutex; std::unordered_map rep_amounts; void put (nano::account const & account_a, nano::uint128_union const & representation_a); nano::uint128_t get (nano::account const & account_a) const; diff --git a/nano/lib/stats.cpp b/nano/lib/stats.cpp index b78b472c1a..e64b0589bf 100644 --- a/nano/lib/stats.cpp +++ b/nano/lib/stats.cpp @@ -203,7 +203,7 @@ std::shared_ptr nano::stat::get_entry (uint32_t key) std::shared_ptr nano::stat::get_entry (uint32_t key, size_t interval, size_t capacity) { - nano::unique_lock lock (stat_mutex); + nano::unique_lock lock (stat_mutex); return get_entry_impl (key, interval, capacity); } @@ -230,7 +230,7 @@ std::unique_ptr nano::stat::log_sink_json () const void nano::stat::log_counters (stat_log_sink & sink) { - nano::unique_lock lock (stat_mutex); + nano::unique_lock lock (stat_mutex); log_counters_impl (sink); } @@ -265,7 +265,7 @@ void nano::stat::log_counters_impl (stat_log_sink & sink) void nano::stat::log_samples (stat_log_sink & sink) { - nano::unique_lock lock (stat_mutex); + nano::unique_lock lock (stat_mutex); log_samples_impl (sink); } @@ -308,7 +308,7 @@ void nano::stat::update (uint32_t key_a, uint64_t value) auto now (std::chrono::steady_clock::now ()); - nano::unique_lock lock (stat_mutex); + nano::unique_lock lock (stat_mutex); if (!stopped) { auto entry (get_entry_impl (key_a, config.interval, config.capacity)); @@ -360,20 +360,20 @@ void nano::stat::update (uint32_t key_a, uint64_t value) std::chrono::seconds nano::stat::last_reset () { - nano::unique_lock lock (stat_mutex); + nano::unique_lock lock (stat_mutex); auto now (std::chrono::steady_clock::now ()); return std::chrono::duration_cast (now - timestamp); } void nano::stat::stop () { - nano::lock_guard guard (stat_mutex); + nano::lock_guard guard (stat_mutex); stopped = true; } void nano::stat::clear () { - nano::unique_lock lock (stat_mutex); + nano::unique_lock lock (stat_mutex); entries.clear (); timestamp = std::chrono::steady_clock::now (); } @@ -785,14 +785,14 @@ std::string nano::stat::dir_to_string (uint32_t key) nano::stat_datapoint::stat_datapoint (stat_datapoint const & other_a) { - nano::lock_guard lock (other_a.datapoint_mutex); + nano::lock_guard lock (other_a.datapoint_mutex); value = other_a.value; timestamp = other_a.timestamp; } nano::stat_datapoint & nano::stat_datapoint::operator= (stat_datapoint const & other_a) { - nano::lock_guard lock (other_a.datapoint_mutex); + nano::lock_guard lock (other_a.datapoint_mutex); value = other_a.value; timestamp = other_a.timestamp; return *this; @@ -800,32 +800,32 @@ nano::stat_datapoint & nano::stat_datapoint::operator= (stat_datapoint const & o uint64_t nano::stat_datapoint::get_value () const { - nano::lock_guard lock (datapoint_mutex); + nano::lock_guard lock (datapoint_mutex); return value; } void nano::stat_datapoint::set_value (uint64_t value_a) { - nano::lock_guard lock (datapoint_mutex); + nano::lock_guard lock (datapoint_mutex); value = value_a; } std::chrono::system_clock::time_point nano::stat_datapoint::get_timestamp () const { - nano::lock_guard lock (datapoint_mutex); + nano::lock_guard lock (datapoint_mutex); return timestamp; } void nano::stat_datapoint::set_timestamp (std::chrono::system_clock::time_point timestamp_a) { - nano::lock_guard lock (datapoint_mutex); + nano::lock_guard lock (datapoint_mutex); timestamp = timestamp_a; } /** Add \addend to the current value and optionally update the timestamp */ void nano::stat_datapoint::add (uint64_t addend, bool update_timestamp) { - nano::lock_guard lock (datapoint_mutex); + nano::lock_guard lock (datapoint_mutex); value += addend; if (update_timestamp) { diff --git a/nano/lib/stats.hpp b/nano/lib/stats.hpp index 9fd47e27bf..e689213c65 100644 --- a/nano/lib/stats.hpp +++ b/nano/lib/stats.hpp @@ -71,7 +71,7 @@ class stat_datapoint final void add (uint64_t addend, bool update_timestamp = true); private: - mutable std::mutex datapoint_mutex; + mutable nano::mutex datapoint_mutex; /** Value of the sample interval */ uint64_t value{ 0 }; /** When the sample was added. This is wall time (system_clock), suitable for display purposes. */ @@ -545,6 +545,6 @@ class stat final bool stopped{ false }; /** All access to stat is thread safe, including calls from observers on the same thread */ - std::mutex stat_mutex; + nano::mutex stat_mutex; }; } diff --git a/nano/lib/threading.cpp b/nano/lib/threading.cpp index 649c25845e..e11330c233 100644 --- a/nano/lib/threading.cpp +++ b/nano/lib/threading.cpp @@ -210,7 +210,7 @@ nano::thread_pool::~thread_pool () void nano::thread_pool::stop () { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); if (!stopped) { stopped = true; @@ -229,7 +229,7 @@ void nano::thread_pool::stop () void nano::thread_pool::push_task (std::function task) { ++num_tasks; - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); if (!stopped) { boost::asio::post (*thread_pool_m, [this, task]() { @@ -241,7 +241,7 @@ void nano::thread_pool::push_task (std::function task) void nano::thread_pool::add_timed_task (std::chrono::steady_clock::time_point const & expiry_time, std::function task) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); if (!stopped && thread_pool_m) { auto timer = std::make_shared (thread_pool_m->get_executor (), expiry_time); diff --git a/nano/lib/threading.hpp b/nano/lib/threading.hpp index ad13e85c6f..955c8798ff 100644 --- a/nano/lib/threading.hpp +++ b/nano/lib/threading.hpp @@ -184,7 +184,7 @@ class thread_pool final uint64_t num_queued_tasks () const; private: - std::mutex mutex; + nano::mutex mutex; std::atomic stopped{ false }; unsigned num_threads; std::unique_ptr thread_pool_m; diff --git a/nano/lib/utility.hpp b/nano/lib/utility.hpp index 736be268a0..e0ed5943f7 100644 --- a/nano/lib/utility.hpp +++ b/nano/lib/utility.hpp @@ -138,18 +138,18 @@ class observer_set final public: void add (std::function const & observer_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); observers.push_back (observer_a); } void notify (T... args) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); for (auto & i : observers) { i (args...); } } - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::observer_set) }; std::vector> observers; }; @@ -158,7 +158,7 @@ std::unique_ptr collect_container_info (observer_set lock (observer_set.mutex); + nano::lock_guard lock (observer_set.mutex); count = observer_set.observers.size (); } diff --git a/nano/lib/work.cpp b/nano/lib/work.cpp index 1c120ce8e5..d88db09a0d 100644 --- a/nano/lib/work.cpp +++ b/nano/lib/work.cpp @@ -241,7 +241,7 @@ void nano::work_pool::loop (uint64_t thread) uint64_t output; blake2b_state hash; blake2b_init (&hash, sizeof (output)); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto pow_sleep = pow_rate_limiter; while (!done) { @@ -321,7 +321,7 @@ void nano::work_pool::loop (uint64_t thread) void nano::work_pool::cancel (nano::root const & root_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); if (!done) { if (!pending.empty ()) @@ -349,7 +349,7 @@ void nano::work_pool::cancel (nano::root const & root_a) void nano::work_pool::stop () { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); done = true; ++ticket; } @@ -362,7 +362,7 @@ void nano::work_pool::generate (nano::work_version const version_a, nano::root c if (!threads.empty ()) { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); pending.emplace_back (version_a, root_a, difficulty_a, callback_a); } producer_condition.notify_all (); @@ -404,7 +404,7 @@ boost::optional nano::work_pool::generate (nano::work_version const ve size_t nano::work_pool::size () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return pending.size (); } @@ -412,7 +412,7 @@ std::unique_ptr nano::collect_container_info (wo { size_t count; { - nano::lock_guard guard (work_pool.mutex); + nano::lock_guard guard (work_pool.mutex); count = work_pool.pending.size (); } auto sizeof_element = sizeof (decltype (work_pool.pending)::value_type); diff --git a/nano/lib/work.hpp b/nano/lib/work.hpp index 5d3bd4f58b..db4a35b9d3 100644 --- a/nano/lib/work.hpp +++ b/nano/lib/work.hpp @@ -75,7 +75,7 @@ class work_pool final bool done; std::vector threads; std::list pending; - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::work_pool) }; nano::condition_variable producer_condition; std::chrono::nanoseconds pow_rate_limiter; std::function (nano::work_version const, nano::root const &, uint64_t, std::atomic &)> opencl; diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index 2759090548..78ce63e097 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -72,7 +72,6 @@ int main (int argc, char * const * argv) ("help", "Print out options") ("version", "Prints out version") ("config", boost::program_options::value>()->multitoken(), "Pass node configuration values. This takes precedence over any values in the configuration file. This option can be repeated multiple times.") - ("rpcconfig", boost::program_options::value>()->multitoken(), "Pass RPC configuration values. This takes precedence over any values in the RPC configuration file. This option can be repeated multiple times.") ("daemon", "Start node daemon") ("compare_rep_weights", "Display a summarized comparison between the hardcoded bootstrap weights and representative weights from the ledger. Full comparison is output to logs") ("debug_block_count", "Display the number of blocks") @@ -1363,7 +1362,7 @@ int main (int argc, char * const * argv) } threads_count = std::max (1u, threads_count); std::vector threads; - std::mutex mutex; + nano::mutex mutex; nano::condition_variable condition; std::atomic finished (false); std::deque> accounts; @@ -1374,8 +1373,8 @@ int main (int argc, char * const * argv) auto print_error_message = [&silent, &errors](std::string const & error_message_a) { if (!silent) { - static std::mutex cerr_mutex; - nano::lock_guard lock (cerr_mutex); + static nano::mutex cerr_mutex; + nano::lock_guard lock (cerr_mutex); std::cerr << error_message_a; } ++errors; @@ -1386,7 +1385,7 @@ int main (int argc, char * const * argv) { threads.emplace_back ([&function_a, node, &mutex, &condition, &finished, &deque_a]() { auto transaction (node->store.tx_begin_read ()); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (!deque_a.empty () || !finished) { while (deque_a.empty () && !finished) @@ -1638,7 +1637,7 @@ int main (int argc, char * const * argv) for (auto i (node->store.accounts_begin (transaction)), n (node->store.accounts_end ()); i != n; ++i) { { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (accounts.size () > accounts_deque_overflow) { auto wait_ms (250 * accounts.size () / accounts_deque_overflow); @@ -1650,7 +1649,7 @@ int main (int argc, char * const * argv) condition.notify_all (); } { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); finished = true; } condition.notify_all (); @@ -1749,7 +1748,7 @@ int main (int argc, char * const * argv) for (auto i (node->store.pending_begin (transaction)), n (node->store.pending_end ()); i != n; ++i) { { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (pending.size () > pending_deque_overflow) { auto wait_ms (50 * pending.size () / pending_deque_overflow); @@ -1761,7 +1760,7 @@ int main (int argc, char * const * argv) condition.notify_all (); } { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); finished = true; } condition.notify_all (); diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index ee51d36375..4b8e8d596b 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -43,7 +43,7 @@ thread ([this]() { this->block_already_cemented_callback (hash_a); }); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); condition.wait (lock, [& started = started] { return started; }); } @@ -55,7 +55,7 @@ nano::active_transactions::~active_transactions () bool nano::active_transactions::insert_election_from_frontiers_confirmation (std::shared_ptr const & block_a, nano::account const & account_a, nano::uint128_t previous_balance_a, nano::election_behavior election_behavior_a) { bool inserted{ false }; - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (roots.get ().find (block_a->qualified_root ()) == roots.get ().end ()) { std::function const &)> election_confirmation_cb; @@ -124,7 +124,7 @@ void nano::active_transactions::set_next_frontier_check (bool agressive_mode_a) void nano::active_transactions::confirm_prioritized_frontiers (nano::transaction const & transaction_a, uint64_t max_elections_a, uint64_t & elections_count_a) { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); auto start_elections_for_prioritized_frontiers = [&transaction_a, &elections_count_a, max_elections_a, &lk, this](prioritize_num_uncemented & cementable_frontiers) { while (!cementable_frontiers.empty () && !this->stopped && elections_count_a < max_elections_a && optimistic_elections_count < max_optimistic ()) { @@ -193,7 +193,7 @@ void nano::active_transactions::block_cemented_callback (std::shared_ptrhash ()); - nano::unique_lock election_winners_lk (election_winner_details_mutex); + nano::unique_lock election_winners_lk (election_winner_details_mutex); auto existing (election_winner_details.find (hash)); if (existing != election_winner_details.end ()) { @@ -202,7 +202,7 @@ void nano::active_transactions::block_cemented_callback (std::shared_ptrconfirmed () && election->winner ()->hash () == hash) { - nano::unique_lock election_lk (election->mutex); + nano::unique_lock election_lk (election->mutex); auto status_l = election->status; election_lk.unlock (); add_recently_cemented (status_l); @@ -260,13 +260,13 @@ void nano::active_transactions::block_cemented_callback (std::shared_ptr const & election_a) { - nano::lock_guard guard (election_winner_details_mutex); + nano::lock_guard guard (election_winner_details_mutex); election_winner_details.emplace (hash_a, election_a); } void nano::active_transactions::remove_election_winner_details (nano::block_hash const & hash_a) { - nano::lock_guard guard (election_winner_details_mutex); + nano::lock_guard guard (election_winner_details_mutex); election_winner_details.erase (hash_a); } @@ -279,7 +279,7 @@ void nano::active_transactions::block_already_cemented_callback (nano::block_has remove_election_winner_details (hash_a); } -void nano::active_transactions::request_confirm (nano::unique_lock & lock_a) +void nano::active_transactions::request_confirm (nano::unique_lock & lock_a) { debug_assert (lock_a.owns_lock ()); @@ -351,7 +351,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock & } } -void nano::active_transactions::cleanup_election (nano::unique_lock & lock_a, nano::election_cleanup_info const & info_a) +void nano::active_transactions::cleanup_election (nano::unique_lock & lock_a, nano::election_cleanup_info const & info_a) { debug_assert (lock_a.owns_lock ()); @@ -388,7 +388,7 @@ void nano::active_transactions::cleanup_election (nano::unique_lock std::vector> nano::active_transactions::list_active (size_t max_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return list_active_impl (max_a); } @@ -409,7 +409,7 @@ std::vector> nano::active_transactions::list_act void nano::active_transactions::add_expired_optimistic_election (nano::election const & election_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto account = election_a.status.winner->account (); if (account.is_zero ()) { @@ -443,7 +443,7 @@ unsigned nano::active_transactions::max_optimistic () return node.ledger.cache.cemented_count < node.ledger.bootstrap_weight_max_blocks ? std::numeric_limits::max () : 50u; } -void nano::active_transactions::frontiers_confirmation (nano::unique_lock & lock_a) +void nano::active_transactions::frontiers_confirmation (nano::unique_lock & lock_a) { // Spend some time prioritizing accounts with the most uncemented blocks to reduce voting traffic auto request_interval = std::chrono::milliseconds (node.network_params.network.request_interval_ms); @@ -560,7 +560,7 @@ bool nano::active_transactions::should_do_frontiers_confirmation () const void nano::active_transactions::request_loop () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); started = true; lock.unlock (); condition.notify_all (); @@ -606,7 +606,7 @@ bool nano::active_transactions::prioritize_account_for_confirmation (nano::activ if (info_a.block_count > confirmation_height_a && !confirmation_height_processor.is_processing_block (info_a.head)) { auto num_uncemented = info_a.block_count - confirmation_height_a; - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto it = cementable_frontiers_a.get ().find (account_a); if (it != cementable_frontiers_a.get ().end ()) { @@ -652,7 +652,7 @@ void nano::active_transactions::prioritize_frontiers_for_confirmation (nano::tra size_t priority_cementable_frontiers_size; size_t priority_wallet_cementable_frontiers_size; { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); priority_cementable_frontiers_size = priority_cementable_frontiers.size (); priority_wallet_cementable_frontiers_size = priority_wallet_cementable_frontiers.size (); } @@ -673,7 +673,7 @@ void nano::active_transactions::prioritize_frontiers_for_confirmation (nano::tra { // Prioritize wallet accounts first { - nano::lock_guard lock (node.wallets.mutex); + nano::lock_guard lock (node.wallets.mutex); auto wallet_transaction (node.wallets.tx_begin_read ()); auto const & items = node.wallets.items; if (items.empty ()) @@ -707,7 +707,7 @@ void nano::active_transactions::prioritize_frontiers_for_confirmation (nano::tra auto it = priority_cementable_frontiers.find (account); if (it != priority_cementable_frontiers.end ()) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); priority_cementable_frontiers.erase (it); priority_cementable_frontiers_size = priority_cementable_frontiers.size (); } @@ -780,7 +780,7 @@ void nano::active_transactions::prioritize_frontiers_for_confirmation (nano::tra void nano::active_transactions::stop () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (!started) { condition.wait (lock, [& started = started] { return started; }); @@ -797,7 +797,7 @@ void nano::active_transactions::stop () roots.clear (); } -nano::election_insertion_result nano::active_transactions::insert_impl (nano::unique_lock & lock_a, std::shared_ptr const & block_a, boost::optional const & previous_balance_a, nano::election_behavior election_behavior_a, std::function const &)> const & confirmation_action_a) +nano::election_insertion_result nano::active_transactions::insert_impl (nano::unique_lock & lock_a, std::shared_ptr const & block_a, boost::optional const & previous_balance_a, nano::election_behavior election_behavior_a, std::function const &)> const & confirmation_action_a) { debug_assert (lock_a.owns_lock ()); debug_assert (block_a->has_sideband ()); @@ -861,7 +861,7 @@ nano::election_insertion_result nano::active_transactions::insert_impl (nano::un nano::election_insertion_result nano::active_transactions::insert (std::shared_ptr const & block_a, boost::optional const & previous_balance_a, nano::election_behavior election_behavior_a, std::function const &)> const & confirmation_action_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); return insert_impl (lock, block_a, previous_balance_a, election_behavior_a, confirmation_action_a); } @@ -873,7 +873,7 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr con unsigned recently_confirmed_counter (0); std::vector, nano::block_hash>> process; { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); for (auto vote_block : vote_a->blocks) { auto & recently_confirmed_by_hash (recently_confirmed.get ()); @@ -945,20 +945,20 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr con bool nano::active_transactions::active (nano::qualified_root const & root_a) { - nano::lock_guard guard (mutex); + nano::lock_guard lock (mutex); return roots.get ().find (root_a) != roots.get ().end (); } bool nano::active_transactions::active (nano::block const & block_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return roots.get ().find (block_a.qualified_root ()) != roots.get ().end () && blocks.find (block_a.hash ()) != blocks.end (); } std::shared_ptr nano::active_transactions::election (nano::qualified_root const & root_a) const { std::shared_ptr result; - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto existing = roots.get ().find (root_a); if (existing != roots.get ().end ()) { @@ -970,7 +970,7 @@ std::shared_ptr nano::active_transactions::election (nano::quali std::shared_ptr nano::active_transactions::winner (nano::block_hash const & hash_a) const { std::shared_ptr result; - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto existing = blocks.find (hash_a); if (existing != blocks.end ()) { @@ -1011,7 +1011,7 @@ nano::election_insertion_result nano::active_transactions::activate (nano::accou bool nano::active_transactions::update_difficulty (nano::block const & block_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto existing_election (roots.get ().find (block_a.qualified_root ())); bool error = existing_election == roots.get ().end () || update_difficulty_impl (existing_election, block_a); return error; @@ -1116,7 +1116,7 @@ double nano::active_transactions::normalized_multiplier (nano::block const & blo return multiplier; } -void nano::active_transactions::update_active_multiplier (nano::unique_lock & lock_a) +void nano::active_transactions::update_active_multiplier (nano::unique_lock & lock_a) { debug_assert (!mutex.try_lock ()); last_prioritized_multiplier.reset (); @@ -1188,13 +1188,13 @@ double nano::active_transactions::active_multiplier () std::deque nano::active_transactions::list_recently_cemented () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return recently_cemented; } void nano::active_transactions::add_recently_cemented (nano::election_status const & status_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); recently_cemented.push_back (status_a); if (recently_cemented.size () > node.config.confirmation_history_size) { @@ -1204,7 +1204,7 @@ void nano::active_transactions::add_recently_cemented (nano::election_status con void nano::active_transactions::add_recently_confirmed (nano::qualified_root const & root_a, nano::block_hash const & hash_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); recently_confirmed.get ().emplace_back (root_a, hash_a); if (recently_confirmed.size () > recently_confirmed_size) { @@ -1214,13 +1214,13 @@ void nano::active_transactions::add_recently_confirmed (nano::qualified_root con void nano::active_transactions::erase_recently_confirmed (nano::block_hash const & hash_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); recently_confirmed.get ().erase (hash_a); } void nano::active_transactions::erase (nano::block const & block_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto root_it (roots.get ().find (block_a.qualified_root ())); if (root_it != roots.get ().end ()) { @@ -1234,7 +1234,7 @@ void nano::active_transactions::erase (nano::block const & block_a) void nano::active_transactions::erase (nano::qualified_root const & root_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto root_it (roots.get ().find (root_a)); if (root_it != roots.get ().end ()) { @@ -1246,26 +1246,26 @@ void nano::active_transactions::erase (nano::qualified_root const & root_a) void nano::active_transactions::erase_hash (nano::block_hash const & hash_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); [[maybe_unused]] auto erased (blocks.erase (hash_a)); debug_assert (erased == 1); } bool nano::active_transactions::empty () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return roots.empty (); } size_t nano::active_transactions::size () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return roots.size (); } bool nano::active_transactions::publish (std::shared_ptr const & block_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto existing (roots.get ().find (block_a->qualified_root ())); auto result (true); if (existing != roots.get ().end ()) @@ -1291,13 +1291,13 @@ bool nano::active_transactions::publish (std::shared_ptr const & bl boost::optional nano::active_transactions::confirm_block (nano::transaction const & transaction_a, std::shared_ptr const & block_a) { auto hash (block_a->hash ()); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto existing (blocks.find (hash)); boost::optional status_type; if (existing != blocks.end ()) { lock.unlock (); - nano::unique_lock election_lock (existing->second->mutex); + nano::unique_lock election_lock (existing->second->mutex); if (existing->second->status.winner && existing->second->status.winner->hash () == hash) { if (!existing->second->confirmed ()) @@ -1308,7 +1308,7 @@ boost::optional nano::active_transactions::confirm_b else { #ifndef NDEBUG - nano::unique_lock election_winners_lk (election_winner_details_mutex); + nano::unique_lock election_winners_lk (election_winner_details_mutex); debug_assert (election_winner_details.find (hash) != election_winner_details.cend ()); #endif status_type = nano::election_status_type::active_confirmed_quorum; @@ -1329,29 +1329,29 @@ boost::optional nano::active_transactions::confirm_b size_t nano::active_transactions::priority_cementable_frontiers_size () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return priority_cementable_frontiers.size (); } size_t nano::active_transactions::priority_wallet_cementable_frontiers_size () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return priority_wallet_cementable_frontiers.size (); } boost::circular_buffer nano::active_transactions::difficulty_trend () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return multipliers_cb; } size_t nano::active_transactions::inactive_votes_cache_size () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return inactive_votes_cache.size (); } -void nano::active_transactions::add_inactive_votes_cache (nano::unique_lock & lock_a, nano::block_hash const & hash_a, nano::account const & representative_a) +void nano::active_transactions::add_inactive_votes_cache (nano::unique_lock & lock_a, nano::block_hash const & hash_a, nano::account const & representative_a) { // Check principal representative status if (node.ledger.weight (representative_a) > node.minimum_principal_weight ()) @@ -1421,7 +1421,7 @@ void nano::active_transactions::add_inactive_votes_cache (nano::unique_lock const & block_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto const status = find_inactive_votes_cache_impl (block_a->hash ()).status; if (status.election_started) { @@ -1431,7 +1431,7 @@ void nano::active_transactions::trigger_inactive_votes_cache_election (std::shar nano::inactive_cache_information nano::active_transactions::find_inactive_votes_cache (nano::block_hash const & hash_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return find_inactive_votes_cache_impl (hash_a); } @@ -1454,14 +1454,14 @@ void nano::active_transactions::erase_inactive_votes_cache (nano::block_hash con inactive_votes_cache.get ().erase (hash_a); } -nano::inactive_cache_status nano::active_transactions::inactive_votes_bootstrap_check (nano::unique_lock & lock_a, nano::account const & voter_a, nano::block_hash const & hash_a, nano::inactive_cache_status const & previously_a) +nano::inactive_cache_status nano::active_transactions::inactive_votes_bootstrap_check (nano::unique_lock & lock_a, nano::account const & voter_a, nano::block_hash const & hash_a, nano::inactive_cache_status const & previously_a) { debug_assert (lock_a.owns_lock ()); lock_a.unlock (); return inactive_votes_bootstrap_check_impl (lock_a, node.ledger.weight (voter_a), 1, hash_a, previously_a); } -nano::inactive_cache_status nano::active_transactions::inactive_votes_bootstrap_check (nano::unique_lock & lock_a, std::vector const & voters_a, nano::block_hash const & hash_a, nano::inactive_cache_status const & previously_a) +nano::inactive_cache_status nano::active_transactions::inactive_votes_bootstrap_check (nano::unique_lock & lock_a, std::vector const & voters_a, nano::block_hash const & hash_a, nano::inactive_cache_status const & previously_a) { /** Perform checks on accumulated tally from inactive votes * These votes are generally either for unconfirmed blocks or old confirmed blocks @@ -1479,7 +1479,7 @@ nano::inactive_cache_status nano::active_transactions::inactive_votes_bootstrap_ return inactive_votes_bootstrap_check_impl (lock_a, tally, voters_a.size (), hash_a, previously_a); } -nano::inactive_cache_status nano::active_transactions::inactive_votes_bootstrap_check_impl (nano::unique_lock & lock_a, nano::uint128_t const & tally_a, size_t voters_size_a, nano::block_hash const & hash_a, nano::inactive_cache_status const & previously_a) +nano::inactive_cache_status nano::active_transactions::inactive_votes_bootstrap_check_impl (nano::unique_lock & lock_a, nano::uint128_t const & tally_a, size_t voters_size_a, nano::block_hash const & hash_a, nano::inactive_cache_status const & previously_a) { debug_assert (!lock_a.owns_lock ()); nano::inactive_cache_status status (previously_a); @@ -1532,7 +1532,7 @@ bool nano::purge_singleton_inactive_votes_cache_pool_memory () size_t nano::active_transactions::election_winner_details_size () { - nano::lock_guard guard (election_winner_details_mutex); + nano::lock_guard guard (election_winner_details_mutex); return election_winner_details.size (); } @@ -1560,7 +1560,7 @@ std::unique_ptr nano::collect_container_info (ac size_t recently_cemented_count; { - nano::lock_guard guard (active_transactions.mutex); + nano::lock_guard guard (active_transactions.mutex); roots_count = active_transactions.roots.size (); blocks_count = active_transactions.blocks.size (); recently_confirmed_count = active_transactions.recently_confirmed.size (); @@ -1590,7 +1590,7 @@ stats (stats_a) void nano::dropped_elections::add (nano::qualified_root const & root_a) { stats.inc (nano::stat::type::election, nano::stat::detail::election_drop); - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto & items_by_sequence = items.get (); items_by_sequence.emplace_back (nano::election_timepoint{ std::chrono::steady_clock::now (), root_a }); if (items.size () > capacity) @@ -1601,13 +1601,13 @@ void nano::dropped_elections::add (nano::qualified_root const & root_a) void nano::dropped_elections::erase (nano::qualified_root const & root_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); items.get ().erase (root_a); } std::chrono::steady_clock::time_point nano::dropped_elections::find (nano::qualified_root const & root_a) const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto & items_by_root = items.get (); auto existing (items_by_root.find (root_a)); if (existing != items_by_root.end ()) @@ -1622,6 +1622,6 @@ std::chrono::steady_clock::time_point nano::dropped_elections::find (nano::quali size_t nano::dropped_elections::size () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return items.size (); } diff --git a/nano/node/active_transactions.hpp b/nano/node/active_transactions.hpp index fd7ba41b9a..4757f1b189 100644 --- a/nano/node/active_transactions.hpp +++ b/nano/node/active_transactions.hpp @@ -128,7 +128,8 @@ class dropped_elections final private: ordered_dropped items; - mutable std::mutex mutex; + mutable nano::mutex mutex{ mutex_identifier (mutexes::dropped_elections) }; + nano::stat & stats; }; @@ -203,7 +204,7 @@ class active_transactions final // Returns a list of elections sorted by difficulty std::vector> list_active (size_t = std::numeric_limits::max ()); double normalized_multiplier (nano::block const &, boost::optional const & = boost::none) const; - void update_active_multiplier (nano::unique_lock &); + void update_active_multiplier (nano::unique_lock &); uint64_t active_difficulty (); uint64_t limited_active_difficulty (nano::block const &); uint64_t limited_active_difficulty (nano::work_version const, uint64_t const); @@ -226,14 +227,14 @@ class active_transactions final void add_recently_cemented (nano::election_status const &); void add_recently_confirmed (nano::qualified_root const &, nano::block_hash const &); void erase_recently_confirmed (nano::block_hash const &); - void add_inactive_votes_cache (nano::unique_lock &, nano::block_hash const &, nano::account const &); + void add_inactive_votes_cache (nano::unique_lock &, nano::block_hash const &, nano::account const &); // Inserts an election if conditions are met void trigger_inactive_votes_cache_election (std::shared_ptr const &); nano::inactive_cache_information find_inactive_votes_cache (nano::block_hash const &); void erase_inactive_votes_cache (nano::block_hash const &); nano::confirmation_height_processor & confirmation_height_processor; nano::node & node; - mutable std::mutex mutex; + mutable nano::mutex mutex{ mutex_identifier (mutexes::active) }; boost::circular_buffer multipliers_cb; std::atomic trended_active_multiplier; size_t priority_cementable_frontiers_size (); @@ -262,20 +263,21 @@ class active_transactions final // clang-format on private: - std::mutex election_winner_details_mutex; + nano::mutex election_winner_details_mutex{ mutex_identifier (mutexes::election_winner_details) }; + std::unordered_map> election_winner_details; // Call action with confirmed block, may be different than what we started with // clang-format off - nano::election_insertion_result insert_impl (nano::unique_lock &, std::shared_ptr const&, boost::optional const & = boost::none, nano::election_behavior = nano::election_behavior::normal, std::functionconst&)> const & = nullptr); + nano::election_insertion_result insert_impl (nano::unique_lock &, std::shared_ptr const&, boost::optional const & = boost::none, nano::election_behavior = nano::election_behavior::normal, std::functionconst&)> const & = nullptr); // clang-format on // Returns false if the election difficulty was updated bool update_difficulty_impl (roots_iterator const &, nano::block const &); void request_loop (); - void request_confirm (nano::unique_lock &); + void request_confirm (nano::unique_lock &); void erase (nano::qualified_root const &); // Erase all blocks from active and, if not confirmed, clear digests from network filters - void cleanup_election (nano::unique_lock &, nano::election_cleanup_info const &); + void cleanup_election (nano::unique_lock &, nano::election_cleanup_info const &); // Returns a list of elections sorted by difficulty, mutex must be locked std::vector> list_active_impl (size_t) const; @@ -328,7 +330,7 @@ class active_transactions final nano::frontiers_confirmation_info get_frontiers_confirmation_info (); void confirm_prioritized_frontiers (nano::transaction const &, uint64_t, uint64_t &); void confirm_expired_frontiers_pessimistically (nano::transaction const &, uint64_t, uint64_t &); - void frontiers_confirmation (nano::unique_lock &); + void frontiers_confirmation (nano::unique_lock &); bool insert_election_from_frontiers_confirmation (std::shared_ptr const &, nano::account const &, nano::uint128_t, nano::election_behavior); nano::account next_frontier_account{ 0 }; std::chrono::steady_clock::time_point next_frontier_check{ std::chrono::steady_clock::now () }; @@ -349,9 +351,9 @@ class active_transactions final static size_t constexpr confirmed_frontiers_max_pending_size{ 10000 }; static std::chrono::minutes constexpr expired_optimistic_election_info_cutoff{ 30 }; ordered_cache inactive_votes_cache; - nano::inactive_cache_status inactive_votes_bootstrap_check (nano::unique_lock &, std::vector const &, nano::block_hash const &, nano::inactive_cache_status const &); - nano::inactive_cache_status inactive_votes_bootstrap_check (nano::unique_lock &, nano::account const &, nano::block_hash const &, nano::inactive_cache_status const &); - nano::inactive_cache_status inactive_votes_bootstrap_check_impl (nano::unique_lock &, nano::uint128_t const &, size_t, nano::block_hash const &, nano::inactive_cache_status const &); + nano::inactive_cache_status inactive_votes_bootstrap_check (nano::unique_lock &, std::vector const &, nano::block_hash const &, nano::inactive_cache_status const &); + nano::inactive_cache_status inactive_votes_bootstrap_check (nano::unique_lock &, nano::account const &, nano::block_hash const &, nano::inactive_cache_status const &); + nano::inactive_cache_status inactive_votes_bootstrap_check_impl (nano::unique_lock &, nano::uint128_t const &, size_t, nano::block_hash const &, nano::inactive_cache_status const &); nano::inactive_cache_information find_inactive_votes_cache_impl (nano::block_hash const &); boost::thread thread; diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index 399ce9f4f0..0542c647c4 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -39,7 +39,7 @@ state_block_signature_verification (node.checker, node.ledger.network_params.led { { // Prevent a race with condition.wait in block_processor::flush - nano::lock_guard guard (this->mutex); + nano::lock_guard guard (this->mutex); } this->condition.notify_all (); } @@ -54,7 +54,7 @@ nano::block_processor::~block_processor () void nano::block_processor::stop () { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); stopped = true; } condition.notify_all (); @@ -65,7 +65,7 @@ void nano::block_processor::flush () { node.checker.flush (); flushing = true; - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (!stopped && (have_blocks () || active || state_block_signature_verification.is_active ())) { condition.wait (lock); @@ -75,7 +75,7 @@ void nano::block_processor::flush () size_t nano::block_processor::size () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); return (blocks.size () + state_block_signature_verification.size () + forced.size ()); } @@ -109,7 +109,7 @@ void nano::block_processor::add (nano::unchecked_info const & info_a, const bool It's designed to help with realtime blocks traffic if block processor is not performing large task like bootstrap. If deque is a quarter full then push back to allow other blocks processing. */ { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); blocks.emplace_front (info_a, false); } condition.notify_all (); @@ -117,7 +117,7 @@ void nano::block_processor::add (nano::unchecked_info const & info_a, const bool else { { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); blocks.emplace_front (info_a, false); } condition.notify_all (); @@ -134,7 +134,7 @@ void nano::block_processor::add_local (nano::unchecked_info const & info_a, bool void nano::block_processor::force (std::shared_ptr const & block_a) { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); forced.push_back (block_a); } condition.notify_all (); @@ -143,7 +143,7 @@ void nano::block_processor::force (std::shared_ptr const & block_a) void nano::block_processor::update (std::shared_ptr const & block_a) { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); updates.push_back (block_a); } condition.notify_all (); @@ -151,13 +151,13 @@ void nano::block_processor::update (std::shared_ptr const & block_a void nano::block_processor::wait_write () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); awaiting_write = true; } void nano::block_processor::process_blocks () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (!stopped) { if (have_blocks_ready ()) @@ -203,7 +203,7 @@ bool nano::block_processor::have_blocks () void nano::block_processor::process_verified_state_blocks (std::deque> & items, std::vector const & verifications, std::vector const & hashes, std::vector const & blocks_signatures) { { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); for (auto i (0); i < verifications.size (); ++i) { debug_assert (verifications[i] == 1 || verifications[i] == 0); @@ -239,7 +239,7 @@ void nano::block_processor::process_verified_state_blocks (std::deque & lock_a) +void nano::block_processor::process_batch (nano::unique_lock & lock_a) { auto scoped_write_guard = write_database_queue.wait (nano::writer::process_batch); block_post_events post_events ([& store = node.store] { return store.tx_begin_read (); }); @@ -567,7 +567,7 @@ std::unique_ptr nano::collect_container_info (bl size_t forced_count; { - nano::lock_guard guard (block_processor.mutex); + nano::lock_guard guard (block_processor.mutex); blocks_count = block_processor.blocks.size (); forced_count = block_processor.forced.size (); } diff --git a/nano/node/blockprocessor.hpp b/nano/node/blockprocessor.hpp index 0e8071ab55..44f3612c06 100644 --- a/nano/node/blockprocessor.hpp +++ b/nano/node/blockprocessor.hpp @@ -71,7 +71,7 @@ class block_processor final private: void queue_unchecked (nano::write_transaction const &, nano::block_hash const &); - void process_batch (nano::unique_lock &); + void process_batch (nano::unique_lock &); void process_live (nano::transaction const &, nano::block_hash const &, std::shared_ptr const &, nano::process_return const &, const bool = false, nano::block_origin const = nano::block_origin::remote); void process_old (nano::transaction const &, std::shared_ptr const &, nano::block_origin const); void requeue_invalid (nano::block_hash const &, nano::unchecked_info const &); @@ -86,7 +86,7 @@ class block_processor final nano::condition_variable condition; nano::node & node; nano::write_database_queue & write_database_queue; - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::block_processor) }; nano::state_block_signature_verification state_block_signature_verification; friend std::unique_ptr collect_container_info (block_processor & block_processor, std::string const & name); diff --git a/nano/node/bootstrap/bootstrap.cpp b/nano/node/bootstrap/bootstrap.cpp index 2795fcc1be..d500ad5691 100644 --- a/nano/node/bootstrap/bootstrap.cpp +++ b/nano/node/bootstrap/bootstrap.cpp @@ -37,7 +37,7 @@ void nano::bootstrap_initiator::bootstrap (bool force, std::string id_a) { stop_attempts (); } - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (!stopped && find_attempt (nano::bootstrap_mode::legacy) == nullptr) { node.stats.inc (nano::stat::type::bootstrap, nano::stat::detail::initiate, nano::stat::dir::out); @@ -66,7 +66,7 @@ void nano::bootstrap_initiator::bootstrap (nano::endpoint const & endpoint_a, bo { stop_attempts (); node.stats.inc (nano::stat::type::bootstrap, nano::stat::detail::initiate, nano::stat::dir::out); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto legacy_attempt (std::make_shared (node.shared (), attempts.incremental++, id_a)); attempts_list.push_back (legacy_attempt); attempts.add (legacy_attempt); @@ -93,7 +93,7 @@ void nano::bootstrap_initiator::bootstrap_lazy (nano::hash_or_account const & ha stop_attempts (); } node.stats.inc (nano::stat::type::bootstrap, nano::stat::detail::initiate_lazy, nano::stat::dir::out); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); if (!stopped && find_attempt (nano::bootstrap_mode::lazy) == nullptr) { lazy_attempt = std::make_shared (node.shared (), attempts.incremental++, id_a.empty () ? hash_or_account_a.to_string () : id_a); @@ -116,7 +116,7 @@ void nano::bootstrap_initiator::bootstrap_wallet (std::deque & ac node.stats.inc (nano::stat::type::bootstrap, nano::stat::detail::initiate_wallet_lazy, nano::stat::dir::out); if (wallet_attempt == nullptr) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); std::string id (!accounts_a.empty () ? accounts_a[0].to_account () : ""); wallet_attempt = std::make_shared (node.shared (), attempts.incremental++, id); attempts_list.push_back (wallet_attempt); @@ -132,7 +132,7 @@ void nano::bootstrap_initiator::bootstrap_wallet (std::deque & ac void nano::bootstrap_initiator::run_bootstrap () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (!stopped) { if (has_new_attempts ()) @@ -164,13 +164,13 @@ void nano::bootstrap_initiator::lazy_requeue (nano::block_hash const & hash_a, n void nano::bootstrap_initiator::add_observer (std::function const & observer_a) { - nano::lock_guard lock (observers_mutex); + nano::lock_guard lock (observers_mutex); observers.push_back (observer_a); } bool nano::bootstrap_initiator::in_progress () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return !attempts_list.empty (); } @@ -188,7 +188,7 @@ std::shared_ptr nano::bootstrap_initiator::find_attempt void nano::bootstrap_initiator::remove_attempt (std::shared_ptr attempt_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto attempt (std::find (attempts_list.begin (), attempts_list.end (), attempt_a)); if (attempt != attempts_list.end ()) { @@ -232,25 +232,25 @@ bool nano::bootstrap_initiator::has_new_attempts () std::shared_ptr nano::bootstrap_initiator::current_attempt () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return find_attempt (nano::bootstrap_mode::legacy); } std::shared_ptr nano::bootstrap_initiator::current_lazy_attempt () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return find_attempt (nano::bootstrap_mode::lazy); } std::shared_ptr nano::bootstrap_initiator::current_wallet_attempt () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return find_attempt (nano::bootstrap_mode::wallet_lazy); } void nano::bootstrap_initiator::stop_attempts () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); std::vector> copy_attempts; copy_attempts.swap (attempts_list); attempts.clear (); @@ -281,7 +281,7 @@ void nano::bootstrap_initiator::stop () void nano::bootstrap_initiator::notify_listeners (bool in_progress_a) { - nano::lock_guard lock (observers_mutex); + nano::lock_guard lock (observers_mutex); for (auto & i : observers) { i (in_progress_a); @@ -293,11 +293,11 @@ std::unique_ptr nano::collect_container_info (bo size_t count; size_t cache_count; { - nano::lock_guard guard (bootstrap_initiator.observers_mutex); + nano::lock_guard guard (bootstrap_initiator.observers_mutex); count = bootstrap_initiator.observers.size (); } { - nano::lock_guard guard (bootstrap_initiator.cache.pulls_cache_mutex); + nano::lock_guard guard (bootstrap_initiator.cache.pulls_cache_mutex); cache_count = bootstrap_initiator.cache.cache.size (); } @@ -313,7 +313,7 @@ void nano::pulls_cache::add (nano::pull_info const & pull_a) { if (pull_a.processed > 500) { - nano::lock_guard guard (pulls_cache_mutex); + nano::lock_guard guard (pulls_cache_mutex); // Clean old pull if (cache.size () > cache_size_max) { @@ -342,7 +342,7 @@ void nano::pulls_cache::add (nano::pull_info const & pull_a) void nano::pulls_cache::update_pull (nano::pull_info & pull_a) { - nano::lock_guard guard (pulls_cache_mutex); + nano::lock_guard guard (pulls_cache_mutex); nano::uint512_union head_512 (pull_a.account_or_head, pull_a.head_original); auto existing (cache.get ().find (head_512)); if (existing != cache.get ().end ()) @@ -353,32 +353,32 @@ void nano::pulls_cache::update_pull (nano::pull_info & pull_a) void nano::pulls_cache::remove (nano::pull_info const & pull_a) { - nano::lock_guard guard (pulls_cache_mutex); + nano::lock_guard guard (pulls_cache_mutex); nano::uint512_union head_512 (pull_a.account_or_head, pull_a.head_original); cache.get ().erase (head_512); } void nano::bootstrap_attempts::add (std::shared_ptr attempt_a) { - nano::lock_guard lock (bootstrap_attempts_mutex); + nano::lock_guard lock (bootstrap_attempts_mutex); attempts.emplace (attempt_a->incremental_id, attempt_a); } void nano::bootstrap_attempts::remove (uint64_t incremental_id_a) { - nano::lock_guard lock (bootstrap_attempts_mutex); + nano::lock_guard lock (bootstrap_attempts_mutex); attempts.erase (incremental_id_a); } void nano::bootstrap_attempts::clear () { - nano::lock_guard lock (bootstrap_attempts_mutex); + nano::lock_guard lock (bootstrap_attempts_mutex); attempts.clear (); } std::shared_ptr nano::bootstrap_attempts::find (uint64_t incremental_id_a) { - nano::lock_guard lock (bootstrap_attempts_mutex); + nano::lock_guard lock (bootstrap_attempts_mutex); auto find_attempt (attempts.find (incremental_id_a)); if (find_attempt != attempts.end ()) { @@ -392,6 +392,6 @@ std::shared_ptr nano::bootstrap_attempts::find (uint64_ size_t nano::bootstrap_attempts::size () { - nano::lock_guard lock (bootstrap_attempts_mutex); + nano::lock_guard lock (bootstrap_attempts_mutex); return attempts.size (); } diff --git a/nano/node/bootstrap/bootstrap.hpp b/nano/node/bootstrap/bootstrap.hpp index be9ac877c0..8d6708663b 100644 --- a/nano/node/bootstrap/bootstrap.hpp +++ b/nano/node/bootstrap/bootstrap.hpp @@ -48,7 +48,7 @@ class pulls_cache final void add (nano::pull_info const &); void update_pull (nano::pull_info &); void remove (nano::pull_info const &); - std::mutex pulls_cache_mutex; + nano::mutex pulls_cache_mutex; class account_head_tag { }; @@ -72,7 +72,7 @@ class bootstrap_attempts final std::shared_ptr find (uint64_t); size_t size (); std::atomic incremental{ 0 }; - std::mutex bootstrap_attempts_mutex; + nano::mutex bootstrap_attempts_mutex; std::map> attempts; }; @@ -107,9 +107,9 @@ class bootstrap_initiator final void stop_attempts (); std::vector> attempts_list; std::atomic stopped{ false }; - std::mutex mutex; + nano::mutex mutex; nano::condition_variable condition; - std::mutex observers_mutex; + nano::mutex observers_mutex; std::vector> observers; std::vector bootstrap_initiator_threads; diff --git a/nano/node/bootstrap/bootstrap_attempt.cpp b/nano/node/bootstrap/bootstrap_attempt.cpp index 6e1ad3a8fe..d85c05cb7d 100644 --- a/nano/node/bootstrap/bootstrap_attempt.cpp +++ b/nano/node/bootstrap/bootstrap_attempt.cpp @@ -51,7 +51,7 @@ nano::bootstrap_attempt::~bootstrap_attempt () bool nano::bootstrap_attempt::should_log () { - nano::lock_guard guard (next_log_mutex); + nano::lock_guard guard (next_log_mutex); auto result (false); auto now (std::chrono::steady_clock::now ()); if (next_log < now) @@ -73,7 +73,7 @@ bool nano::bootstrap_attempt::still_pulling () void nano::bootstrap_attempt::pull_started () { { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); ++pulling; } condition.notify_all (); @@ -82,7 +82,7 @@ void nano::bootstrap_attempt::pull_started () void nano::bootstrap_attempt::pull_finished () { { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); --pulling; } condition.notify_all (); @@ -91,7 +91,7 @@ void nano::bootstrap_attempt::pull_finished () void nano::bootstrap_attempt::stop () { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); stopped = true; } condition.notify_all (); diff --git a/nano/node/bootstrap/bootstrap_attempt.hpp b/nano/node/bootstrap/bootstrap_attempt.hpp index 6ad361857d..ffdb9feca6 100644 --- a/nano/node/bootstrap/bootstrap_attempt.hpp +++ b/nano/node/bootstrap/bootstrap_attempt.hpp @@ -39,7 +39,7 @@ class bootstrap_attempt : public std::enable_shared_from_this virtual void wallet_start (std::deque &); virtual size_t wallet_size (); virtual void get_information (boost::property_tree::ptree &) = 0; - std::mutex next_log_mutex; + nano::mutex next_log_mutex; std::chrono::steady_clock::time_point next_log{ std::chrono::steady_clock::now () }; std::atomic pulling{ 0 }; std::shared_ptr node; @@ -53,7 +53,7 @@ class bootstrap_attempt : public std::enable_shared_from_this std::atomic frontiers_received{ false }; std::atomic frontiers_confirmed{ false }; nano::bootstrap_mode mode; - std::mutex mutex; + nano::mutex mutex; nano::condition_variable condition; }; } diff --git a/nano/node/bootstrap/bootstrap_connections.cpp b/nano/node/bootstrap/bootstrap_connections.cpp index f401b07cdc..6ff484ceb0 100644 --- a/nano/node/bootstrap/bootstrap_connections.cpp +++ b/nano/node/bootstrap/bootstrap_connections.cpp @@ -40,13 +40,13 @@ double nano::bootstrap_client::sample_block_rate () void nano::bootstrap_client::set_start_time (std::chrono::steady_clock::time_point start_time_a) { - nano::lock_guard guard (start_time_mutex); + nano::lock_guard guard (start_time_mutex); start_time_m = start_time_a; } double nano::bootstrap_client::elapsed_seconds () const { - nano::lock_guard guard (start_time_mutex); + nano::lock_guard guard (start_time_mutex); return std::chrono::duration_cast> (std::chrono::steady_clock::now () - start_time_m).count (); } @@ -66,7 +66,7 @@ node (node_a) std::shared_ptr nano::bootstrap_connections::connection (std::shared_ptr const & attempt_a, bool use_front_connection) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); condition.wait (lock, [& stopped = stopped, &idle = idle, &new_connections_empty = new_connections_empty] { return stopped || !idle.empty () || new_connections_empty; }); std::shared_ptr result; if (!stopped && !idle.empty ()) @@ -93,7 +93,7 @@ std::shared_ptr nano::bootstrap_connections::connection void nano::bootstrap_connections::pool_connection (std::shared_ptr const & client_a, bool new_client, bool push_front) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto const & socket_l = client_a->socket; if (!stopped && !client_a->pending_stop && !node.network.excluded_peers.check (client_a->channel->get_tcp_endpoint ())) { @@ -127,7 +127,7 @@ void nano::bootstrap_connections::add_connection (nano::endpoint const & endpoin std::shared_ptr nano::bootstrap_connections::find_connection (nano::tcp_endpoint const & endpoint_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); std::shared_ptr result; for (auto i (idle.begin ()), end (idle.end ()); i != end && !stopped; ++i) { @@ -209,7 +209,7 @@ void nano::bootstrap_connections::populate_connections (bool repeat) std::priority_queue, std::vector>, block_rate_cmp> sorted_connections; std::unordered_set endpoints; { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); num_pulls = pulls.size (); std::deque> new_clients; for (auto & c : clients) @@ -288,13 +288,13 @@ void nano::bootstrap_connections::populate_connections (bool repeat) { connect_client (endpoint); endpoints.insert (endpoint); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); new_connections_empty = false; } else if (connections_count == 0) { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); new_connections_empty = true; } condition.notify_all (); @@ -326,13 +326,13 @@ void nano::bootstrap_connections::add_pull (nano::pull_info const & pull_a) nano::pull_info pull (pull_a); node.bootstrap_initiator.cache.update_pull (pull); { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); pulls.push_back (pull); } condition.notify_all (); } -void nano::bootstrap_connections::request_pull (nano::unique_lock & lock_a) +void nano::bootstrap_connections::request_pull (nano::unique_lock & lock_a) { lock_a.unlock (); auto connection_l (connection ()); @@ -399,7 +399,7 @@ void nano::bootstrap_connections::requeue_pull (nano::pull_info const & pull_a, if (attempt_l->mode == nano::bootstrap_mode::legacy && (pull.attempts < pull.retry_limit + (pull.processed / nano::bootstrap_limits::requeued_pulls_processed_blocks_factor))) { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); pulls.push_front (pull); } attempt_l->pull_started (); @@ -411,7 +411,7 @@ void nano::bootstrap_connections::requeue_pull (nano::pull_info const & pull_a, if (!attempt_l->lazy_processed_or_exists (pull.account_or_head.as_block_hash ())) { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); pulls.push_back (pull); } attempt_l->pull_started (); @@ -441,7 +441,7 @@ void nano::bootstrap_connections::requeue_pull (nano::pull_info const & pull_a, void nano::bootstrap_connections::clear_pulls (uint64_t bootstrap_id_a) { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto i (pulls.begin ()); while (i != pulls.end ()) { @@ -461,7 +461,7 @@ void nano::bootstrap_connections::clear_pulls (uint64_t bootstrap_id_a) void nano::bootstrap_connections::run () { start_populate_connections (); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (!stopped) { if (!pulls.empty ()) @@ -480,7 +480,7 @@ void nano::bootstrap_connections::run () void nano::bootstrap_connections::stop () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); stopped = true; lock.unlock (); condition.notify_all (); diff --git a/nano/node/bootstrap/bootstrap_connections.hpp b/nano/node/bootstrap/bootstrap_connections.hpp index 54db3e5701..dbc63940cb 100644 --- a/nano/node/bootstrap/bootstrap_connections.hpp +++ b/nano/node/bootstrap/bootstrap_connections.hpp @@ -39,7 +39,7 @@ class bootstrap_client final : public std::enable_shared_from_this hard_stop{ false }; private: - mutable std::mutex start_time_mutex; + mutable nano::mutex start_time_mutex; std::chrono::steady_clock::time_point start_time_m; }; @@ -57,7 +57,7 @@ class bootstrap_connections final : public std::enable_shared_from_this & lock_a); + void request_pull (nano::unique_lock & lock_a); void requeue_pull (nano::pull_info const & pull_a, bool network_error = false); void clear_pulls (uint64_t); void run (); @@ -70,7 +70,7 @@ class bootstrap_connections final : public std::enable_shared_from_this populate_connections_started{ false }; std::atomic new_connections_empty{ false }; std::atomic stopped{ false }; - std::mutex mutex; + nano::mutex mutex; nano::condition_variable condition; }; } diff --git a/nano/node/bootstrap/bootstrap_lazy.cpp b/nano/node/bootstrap/bootstrap_lazy.cpp index 95030a3c6a..5f7c89ab34 100644 --- a/nano/node/bootstrap/bootstrap_lazy.cpp +++ b/nano/node/bootstrap/bootstrap_lazy.cpp @@ -28,7 +28,7 @@ nano::bootstrap_attempt_lazy::~bootstrap_attempt_lazy () void nano::bootstrap_attempt_lazy::lazy_start (nano::hash_or_account const & hash_or_account_a, bool confirmed) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); // Add start blocks, limit 1024 (4k with disabled legacy bootstrap) size_t max_keys (node->flags.disable_legacy_bootstrap ? 4 * 1024 : 1024); if (lazy_keys.size () < max_keys && lazy_keys.find (hash_or_account_a.as_block_hash ()) == lazy_keys.end () && !lazy_blocks_processed (hash_or_account_a.as_block_hash ())) @@ -53,13 +53,13 @@ void nano::bootstrap_attempt_lazy::lazy_add (nano::hash_or_account const & hash_ void nano::bootstrap_attempt_lazy::lazy_add (nano::pull_info const & pull_a) { debug_assert (pull_a.account_or_head == pull_a.head); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); lazy_add (pull_a.account_or_head, pull_a.retry_limit); } void nano::bootstrap_attempt_lazy::lazy_requeue (nano::block_hash const & hash_a, nano::block_hash const & previous_a, bool confirmed_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); // Add only known blocks if (lazy_blocks_processed (hash_a)) { @@ -88,7 +88,7 @@ uint32_t nano::bootstrap_attempt_lazy::lazy_batch_size () return result; } -void nano::bootstrap_attempt_lazy::lazy_pull_flush (nano::unique_lock & lock_a) +void nano::bootstrap_attempt_lazy::lazy_pull_flush (nano::unique_lock & lock_a) { static size_t const max_pulls (static_cast (nano::bootstrap_limits::bootstrap_connection_scale_target_blocks) * 3); if (pulling < max_pulls) @@ -187,7 +187,7 @@ void nano::bootstrap_attempt_lazy::run () debug_assert (!node->flags.disable_lazy_bootstrap); node->bootstrap_initiator.connections->populate_connections (false); lazy_start_time = std::chrono::steady_clock::now (); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while ((still_pulling () || !lazy_finished ()) && !lazy_has_expired ()) { unsigned iterations (0); @@ -248,7 +248,7 @@ bool nano::bootstrap_attempt_lazy::process_block_lazy (std::shared_ptrhash ()); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); // Processing new blocks if (!lazy_blocks_processed (hash)) { @@ -488,7 +488,7 @@ bool nano::bootstrap_attempt_lazy::lazy_blocks_processed (nano::block_hash const bool nano::bootstrap_attempt_lazy::lazy_processed_or_exists (nano::block_hash const & hash_a) { bool result (false); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (lazy_blocks_processed (hash_a)) { result = true; @@ -518,7 +518,7 @@ unsigned nano::bootstrap_attempt_lazy::lazy_retry_limit_confirmed () void nano::bootstrap_attempt_lazy::get_information (boost::property_tree::ptree & tree_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); tree_a.put ("lazy_blocks", std::to_string (lazy_blocks.size ())); tree_a.put ("lazy_state_backlog", std::to_string (lazy_state_backlog.size ())); tree_a.put ("lazy_balances", std::to_string (lazy_balances.size ())); @@ -543,7 +543,7 @@ nano::bootstrap_attempt_wallet::~bootstrap_attempt_wallet () node->bootstrap_initiator.notify_listeners (false); } -void nano::bootstrap_attempt_wallet::request_pending (nano::unique_lock & lock_a) +void nano::bootstrap_attempt_wallet::request_pending (nano::unique_lock & lock_a) { lock_a.unlock (); auto connection_l (node->bootstrap_initiator.connections->connection (shared_from_this ())); @@ -567,7 +567,7 @@ void nano::bootstrap_attempt_wallet::requeue_pending (nano::account const & acco { auto account (account_a); { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); wallet_accounts.push_front (account); } condition.notify_all (); @@ -576,7 +576,7 @@ void nano::bootstrap_attempt_wallet::requeue_pending (nano::account const & acco void nano::bootstrap_attempt_wallet::wallet_start (std::deque & accounts_a) { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); wallet_accounts.swap (accounts_a); } condition.notify_all (); @@ -598,7 +598,7 @@ void nano::bootstrap_attempt_wallet::run () node->bootstrap_initiator.connections->populate_connections (false); auto start_time (std::chrono::steady_clock::now ()); auto max_time (std::chrono::minutes (10)); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (wallet_finished () && std::chrono::steady_clock::now () - start_time < max_time) { if (!wallet_accounts.empty ()) @@ -621,12 +621,12 @@ void nano::bootstrap_attempt_wallet::run () size_t nano::bootstrap_attempt_wallet::wallet_size () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return wallet_accounts.size (); } void nano::bootstrap_attempt_wallet::get_information (boost::property_tree::ptree & tree_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); tree_a.put ("wallet_accounts", std::to_string (wallet_accounts.size ())); } diff --git a/nano/node/bootstrap/bootstrap_lazy.hpp b/nano/node/bootstrap/bootstrap_lazy.hpp index 8617c0896b..4d7e65092e 100644 --- a/nano/node/bootstrap/bootstrap_lazy.hpp +++ b/nano/node/bootstrap/bootstrap_lazy.hpp @@ -43,7 +43,7 @@ class bootstrap_attempt_lazy final : public bootstrap_attempt bool lazy_finished (); bool lazy_has_expired () const override; uint32_t lazy_batch_size () override; - void lazy_pull_flush (nano::unique_lock & lock_a); + void lazy_pull_flush (nano::unique_lock & lock_a); bool process_block_lazy (std::shared_ptr const &, nano::account const &, uint64_t, nano::bulk_pull::count_t, unsigned); void lazy_block_state (std::shared_ptr const &, unsigned); void lazy_block_state_backlog_check (std::shared_ptr const &, nano::block_hash const &); @@ -90,7 +90,7 @@ class bootstrap_attempt_wallet final : public bootstrap_attempt public: explicit bootstrap_attempt_wallet (std::shared_ptr const & node_a, uint64_t incremental_id_a, std::string id_a = ""); ~bootstrap_attempt_wallet (); - void request_pending (nano::unique_lock &); + void request_pending (nano::unique_lock &); void requeue_pending (nano::account const &) override; void run () override; void wallet_start (std::deque &) override; diff --git a/nano/node/bootstrap/bootstrap_legacy.cpp b/nano/node/bootstrap/bootstrap_legacy.cpp index 7ee04c6e74..1b68815791 100644 --- a/nano/node/bootstrap/bootstrap_legacy.cpp +++ b/nano/node/bootstrap/bootstrap_legacy.cpp @@ -27,7 +27,7 @@ bool nano::bootstrap_attempt_legacy::consume_future (std::future & future_ void nano::bootstrap_attempt_legacy::stop () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); stopped = true; lock.unlock (); condition.notify_all (); @@ -56,7 +56,7 @@ void nano::bootstrap_attempt_legacy::stop () node->bootstrap_initiator.connections->clear_pulls (incremental_id); } -void nano::bootstrap_attempt_legacy::request_push (nano::unique_lock & lock_a) +void nano::bootstrap_attempt_legacy::request_push (nano::unique_lock & lock_a) { bool error (false); lock_a.unlock (); @@ -89,19 +89,19 @@ void nano::bootstrap_attempt_legacy::request_push (nano::unique_lock void nano::bootstrap_attempt_legacy::add_frontier (nano::pull_info const & pull_a) { nano::pull_info pull (pull_a); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); frontier_pulls.push_back (pull); } void nano::bootstrap_attempt_legacy::add_bulk_push_target (nano::block_hash const & head, nano::block_hash const & end) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); bulk_push_targets.emplace_back (head, end); } bool nano::bootstrap_attempt_legacy::request_bulk_push_target (std::pair & current_target_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto empty (bulk_push_targets.empty ()); if (!empty) { @@ -113,7 +113,7 @@ bool nano::bootstrap_attempt_legacy::request_bulk_push_target (std::pair lock (mutex); + nano::lock_guard lock (mutex); recent_pulls_head.push_back (head_a); if (recent_pulls_head.size () > nano::bootstrap_limits::bootstrap_max_confirm_frontiers) { @@ -133,7 +133,7 @@ void nano::bootstrap_attempt_legacy::restart_condition () } } -void nano::bootstrap_attempt_legacy::attempt_restart_check (nano::unique_lock & lock_a) +void nano::bootstrap_attempt_legacy::attempt_restart_check (nano::unique_lock & lock_a) { if (frontiers_confirmation_pending) { @@ -175,7 +175,7 @@ void nano::bootstrap_attempt_legacy::attempt_restart_check (nano::unique_lock & lock_a) +bool nano::bootstrap_attempt_legacy::confirm_frontiers (nano::unique_lock & lock_a) { bool confirmed (false); debug_assert (!frontiers_confirmed); @@ -183,7 +183,7 @@ bool nano::bootstrap_attempt_legacy::confirm_frontiers (nano::unique_lock frontiers; lock_a.unlock (); - nano::unique_lock pulls_lock (node->bootstrap_initiator.connections->mutex); + nano::unique_lock pulls_lock (node->bootstrap_initiator.connections->mutex); for (auto i (node->bootstrap_initiator.connections->pulls.begin ()), end (node->bootstrap_initiator.connections->pulls.end ()); i != end && frontiers.size () != nano::bootstrap_limits::bootstrap_max_confirm_frontiers; ++i) { if (!i->head.is_zero () && i->bootstrap_id == incremental_id && std::find (frontiers.begin (), frontiers.end (), i->head) == frontiers.end ()) @@ -307,7 +307,7 @@ bool nano::bootstrap_attempt_legacy::confirm_frontiers (nano::unique_lock & lock_a, bool first_attempt) +bool nano::bootstrap_attempt_legacy::request_frontier (nano::unique_lock & lock_a, bool first_attempt) { auto result (true); lock_a.unlock (); @@ -370,7 +370,7 @@ bool nano::bootstrap_attempt_legacy::request_frontier (nano::unique_lock & lock_a) +void nano::bootstrap_attempt_legacy::run_start (nano::unique_lock & lock_a) { frontiers_received = false; frontiers_confirmed = false; @@ -392,7 +392,7 @@ void nano::bootstrap_attempt_legacy::run () debug_assert (started); debug_assert (!node->flags.disable_legacy_bootstrap); node->bootstrap_initiator.connections->populate_connections (false); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); run_start (lock); while (still_pulling ()) { @@ -429,7 +429,7 @@ void nano::bootstrap_attempt_legacy::run () void nano::bootstrap_attempt_legacy::get_information (boost::property_tree::ptree & tree_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); tree_a.put ("frontier_pulls", std::to_string (frontier_pulls.size ())); tree_a.put ("frontiers_received", static_cast (frontiers_received)); tree_a.put ("frontiers_confirmed", static_cast (frontiers_confirmed)); diff --git a/nano/node/bootstrap/bootstrap_legacy.hpp b/nano/node/bootstrap/bootstrap_legacy.hpp index 47958ae948..4b298282ce 100644 --- a/nano/node/bootstrap/bootstrap_legacy.hpp +++ b/nano/node/bootstrap/bootstrap_legacy.hpp @@ -20,16 +20,16 @@ class bootstrap_attempt_legacy : public bootstrap_attempt void run () override; bool consume_future (std::future &); void stop () override; - bool request_frontier (nano::unique_lock &, bool = false); - void request_push (nano::unique_lock &); + bool request_frontier (nano::unique_lock &, bool = false); + void request_push (nano::unique_lock &); void add_frontier (nano::pull_info const &) override; void add_bulk_push_target (nano::block_hash const &, nano::block_hash const &) override; bool request_bulk_push_target (std::pair &) override; void add_recent_pull (nano::block_hash const &) override; - void run_start (nano::unique_lock &); + void run_start (nano::unique_lock &); void restart_condition () override; - void attempt_restart_check (nano::unique_lock &); - bool confirm_frontiers (nano::unique_lock &); + void attempt_restart_check (nano::unique_lock &); + bool confirm_frontiers (nano::unique_lock &); void get_information (boost::property_tree::ptree &) override; nano::tcp_endpoint endpoint_frontier_request; std::weak_ptr frontiers; diff --git a/nano/node/bootstrap/bootstrap_server.cpp b/nano/node/bootstrap/bootstrap_server.cpp index cd7d78a7fb..3c491f113f 100644 --- a/nano/node/bootstrap/bootstrap_server.cpp +++ b/nano/node/bootstrap/bootstrap_server.cpp @@ -15,7 +15,7 @@ port (port_a) void nano::bootstrap_listener::start () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); on = true; listening_socket = std::make_shared (node, boost::asio::ip::tcp::endpoint (boost::asio::ip::address_v6::any (), port), node.config.tcp_incoming_connections_max); boost::system::error_code ec; @@ -45,13 +45,13 @@ void nano::bootstrap_listener::stop () { decltype (connections) connections_l; { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); on = false; connections_l.swap (connections); } if (listening_socket) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); listening_socket->close (); listening_socket = nullptr; } @@ -59,7 +59,7 @@ void nano::bootstrap_listener::stop () size_t nano::bootstrap_listener::connection_count () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return connections.size (); } @@ -68,7 +68,7 @@ void nano::bootstrap_listener::accept_action (boost::system::error_code const & if (!node.network.excluded_peers.check (socket_a->remote_endpoint ())) { auto connection (std::make_shared (socket_a, node.shared ())); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); connections[connection.get ()] = connection; connection->receive (); } @@ -84,7 +84,7 @@ void nano::bootstrap_listener::accept_action (boost::system::error_code const & boost::asio::ip::tcp::endpoint nano::bootstrap_listener::endpoint () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); if (on && listening_socket) { return boost::asio::ip::tcp::endpoint (boost::asio::ip::address_v6::loopback (), listening_socket->listening_port ()); @@ -133,7 +133,7 @@ nano::bootstrap_server::~bootstrap_server () } } stop (); - nano::lock_guard lock (node->bootstrap.mutex); + nano::lock_guard lock (node->bootstrap.mutex); node->bootstrap.connections.erase (this); } @@ -547,7 +547,7 @@ void nano::bootstrap_server::receive_node_id_handshake_action (boost::system::er void nano::bootstrap_server::add_request (std::unique_ptr message_a) { debug_assert (message_a != nullptr); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto start (requests.empty ()); requests.push (std::move (message_a)); if (start) @@ -558,7 +558,7 @@ void nano::bootstrap_server::add_request (std::unique_ptr message void nano::bootstrap_server::finish_request () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); requests.pop (); if (!requests.empty ()) { @@ -598,7 +598,7 @@ void nano::bootstrap_server::timeout () node->logger.try_log ("Closing incoming tcp / bootstrap server by timeout"); } { - nano::lock_guard lock (node->bootstrap.mutex); + nano::lock_guard lock (node->bootstrap.mutex); node->bootstrap.connections.erase (this); } socket->close (); @@ -606,7 +606,7 @@ void nano::bootstrap_server::timeout () } else { - nano::lock_guard lock (node->bootstrap.mutex); + nano::lock_guard lock (node->bootstrap.mutex); node->bootstrap.connections.erase (this); } } @@ -726,7 +726,7 @@ class request_response_visitor : public nano::message_visitor }; } -void nano::bootstrap_server::run_next (nano::unique_lock & lock_a) +void nano::bootstrap_server::run_next (nano::unique_lock & lock_a) { debug_assert (!requests.empty ()); request_response_visitor visitor (shared_from_this ()); diff --git a/nano/node/bootstrap/bootstrap_server.hpp b/nano/node/bootstrap/bootstrap_server.hpp index 2a27ac1e1a..b024062146 100644 --- a/nano/node/bootstrap/bootstrap_server.hpp +++ b/nano/node/bootstrap/bootstrap_server.hpp @@ -18,7 +18,7 @@ class bootstrap_listener final void accept_action (boost::system::error_code const &, std::shared_ptr const &); size_t connection_count (); - std::mutex mutex; + nano::mutex mutex; std::unordered_map> connections; nano::tcp_endpoint endpoint (); nano::node & node; @@ -62,13 +62,13 @@ class bootstrap_server final : public std::enable_shared_from_this & lock_a); + void run_next (nano::unique_lock & lock_a); bool is_bootstrap_connection (); bool is_realtime_connection (); std::shared_ptr> receive_buffer; std::shared_ptr socket; std::shared_ptr node; - std::mutex mutex; + nano::mutex mutex; std::queue> requests; std::atomic stopped{ false }; std::atomic type{ nano::bootstrap_server_type::undefined }; diff --git a/nano/node/cli.cpp b/nano/node/cli.cpp index 8199f592bf..ecf17bf754 100644 --- a/nano/node/cli.cpp +++ b/nano/node/cli.cpp @@ -1048,7 +1048,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map { bool error (true); { - nano::lock_guard lock (node->wallets.mutex); + nano::lock_guard lock (node->wallets.mutex); auto transaction (node->wallets.tx_begin_write ()); nano::wallet wallet (error, transaction, node->wallets, wallet_id.to_string (), contents.str ()); } @@ -1060,7 +1060,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map else { node->wallets.reload (); - nano::lock_guard lock (node->wallets.mutex); + nano::lock_guard lock (node->wallets.mutex); release_assert (node->wallets.items.find (wallet_id) != node->wallets.items.end ()); std::cout << "Import completed\n"; } diff --git a/nano/node/confirmation_height_processor.cpp b/nano/node/confirmation_height_processor.cpp index 04eb8b3544..e47a784297 100644 --- a/nano/node/confirmation_height_processor.cpp +++ b/nano/node/confirmation_height_processor.cpp @@ -35,7 +35,7 @@ nano::confirmation_height_processor::~confirmation_height_processor () void nano::confirmation_height_processor::stop () { { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); stopped = true; } condition.notify_one (); @@ -47,7 +47,7 @@ void nano::confirmation_height_processor::stop () void nano::confirmation_height_processor::run (confirmation_height_mode mode_a) { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); while (!stopped) { if (!paused && !awaiting_processing.empty ()) @@ -139,14 +139,14 @@ void nano::confirmation_height_processor::run (confirmation_height_mode mode_a) // Pausing only affects processing new blocks, not the current one being processed. Currently only used in tests void nano::confirmation_height_processor::pause () { - nano::lock_guard lk (mutex); + nano::lock_guard lk (mutex); paused = true; } void nano::confirmation_height_processor::unpause () { { - nano::lock_guard lk (mutex); + nano::lock_guard lk (mutex); paused = false; } condition.notify_one (); @@ -155,7 +155,7 @@ void nano::confirmation_height_processor::unpause () void nano::confirmation_height_processor::add (std::shared_ptr const & block_a) { { - nano::lock_guard lk (mutex); + nano::lock_guard lk (mutex); awaiting_processing.get ().emplace_back (block_a); } condition.notify_one (); @@ -163,7 +163,7 @@ void nano::confirmation_height_processor::add (std::shared_ptr cons void nano::confirmation_height_processor::set_next_hash () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); debug_assert (!awaiting_processing.empty ()); original_block = awaiting_processing.get ().front ().block; original_hashes_pending.insert (original_block->hash ()); @@ -217,13 +217,13 @@ std::unique_ptr nano::collect_container_info (co size_t nano::confirmation_height_processor::awaiting_processing_size () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return awaiting_processing.size (); } bool nano::confirmation_height_processor::is_processing_added_block (nano::block_hash const & hash_a) const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return original_hashes_pending.count (hash_a) > 0 || awaiting_processing.get ().count (hash_a) > 0; } @@ -234,6 +234,6 @@ bool nano::confirmation_height_processor::is_processing_block (nano::block_hash nano::block_hash nano::confirmation_height_processor::current () const { - nano::lock_guard lk (mutex); + nano::lock_guard lk (mutex); return original_block ? original_block->hash () : 0; } diff --git a/nano/node/confirmation_height_processor.hpp b/nano/node/confirmation_height_processor.hpp index bafcb6761d..35c2135e83 100644 --- a/nano/node/confirmation_height_processor.hpp +++ b/nano/node/confirmation_height_processor.hpp @@ -48,7 +48,7 @@ class confirmation_height_processor final void add_block_already_cemented_observer (std::function const &); private: - mutable std::mutex mutex; + mutable nano::mutex mutex{ mutex_identifier (mutexes::confirmation_height_processor) }; // Hashes which have been added to the confirmation height processor, but not yet processed // clang-format off struct block_wrapper diff --git a/nano/node/confirmation_height_unbounded.cpp b/nano/node/confirmation_height_unbounded.cpp index ee9d327acd..937032bf35 100644 --- a/nano/node/confirmation_height_unbounded.cpp +++ b/nano/node/confirmation_height_unbounded.cpp @@ -65,7 +65,7 @@ void nano::confirmation_height_unbounded::process () debug_assert (current == original_block->hash ()); // This is the original block passed so can use it directly block = original_block; - nano::lock_guard guard (block_cache_mutex); + nano::lock_guard guard (block_cache_mutex); block_cache[original_block->hash ()] = original_block; } else @@ -207,7 +207,7 @@ void nano::confirmation_height_unbounded::collect_unconfirmed_receive_and_source { debug_assert (hash == hash_a); block = block_a; - nano::lock_guard guard (block_cache_mutex); + nano::lock_guard guard (block_cache_mutex); block_cache[hash] = block_a; } else @@ -412,7 +412,7 @@ void nano::confirmation_height_unbounded::cement_blocks (nano::write_guard & sco // Reverse it so that the callbacks start from the lowest newly cemented block and move upwards std::reverse (pending.block_callback_data.begin (), pending.block_callback_data.end ()); - nano::lock_guard guard (block_cache_mutex); + nano::lock_guard guard (block_cache_mutex); std::transform (pending.block_callback_data.begin (), pending.block_callback_data.end (), std::back_inserter (cemented_blocks), [& block_cache = block_cache](auto const & hash_a) { debug_assert (block_cache.count (hash_a) == 1); return block_cache.at (hash_a); @@ -440,7 +440,7 @@ void nano::confirmation_height_unbounded::cement_blocks (nano::write_guard & sco std::shared_ptr nano::confirmation_height_unbounded::get_block_and_sideband (nano::block_hash const & hash_a, nano::transaction const & transaction_a) { - nano::lock_guard guard (block_cache_mutex); + nano::lock_guard guard (block_cache_mutex); auto block_cache_it = block_cache.find (hash_a); if (block_cache_it != block_cache.cend ()) { @@ -468,20 +468,20 @@ void nano::confirmation_height_unbounded::clear_process_vars () implicit_receive_cemented_mapping.clear (); implicit_receive_cemented_mapping_size = 0; { - nano::lock_guard guard (block_cache_mutex); + nano::lock_guard guard (block_cache_mutex); block_cache.clear (); } } bool nano::confirmation_height_unbounded::has_iterated_over_block (nano::block_hash const & hash_a) const { - nano::lock_guard guard (block_cache_mutex); + nano::lock_guard guard (block_cache_mutex); return block_cache.count (hash_a) == 1; } uint64_t nano::confirmation_height_unbounded::block_cache_size () const { - nano::lock_guard guard (block_cache_mutex); + nano::lock_guard guard (block_cache_mutex); return block_cache.size (); } diff --git a/nano/node/confirmation_height_unbounded.hpp b/nano/node/confirmation_height_unbounded.hpp index ad09f0dfbd..7a7554cca4 100644 --- a/nano/node/confirmation_height_unbounded.hpp +++ b/nano/node/confirmation_height_unbounded.hpp @@ -71,7 +71,7 @@ class confirmation_height_unbounded final std::unordered_map> implicit_receive_cemented_mapping; nano::relaxed_atomic_integral implicit_receive_cemented_mapping_size{ 0 }; - mutable std::mutex block_cache_mutex; + mutable nano::mutex block_cache_mutex; std::unordered_map> block_cache; uint64_t block_cache_size () const; diff --git a/nano/node/distributed_work.cpp b/nano/node/distributed_work.cpp index 7339eccedc..00fcfc9f08 100644 --- a/nano/node/distributed_work.cpp +++ b/nano/node/distributed_work.cpp @@ -129,7 +129,7 @@ void nano::distributed_work::do_request (nano::tcp_endpoint const & endpoint_a) auto this_l (shared_from_this ()); auto connection (std::make_shared (node.io_ctx, endpoint_a)); { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); connections.emplace_back (connection); } connection->socket.async_connect (connection->endpoint, @@ -271,7 +271,7 @@ void nano::distributed_work::stop_once (bool const local_stop_a) { if (!stopped.exchange (true)) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); if (local_stop_a && node.local_work_generation_enabled ()) { node.work.cancel (request.root); @@ -389,6 +389,6 @@ void nano::distributed_work::handle_failure () void nano::distributed_work::add_bad_peer (nano::tcp_endpoint const & endpoint_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); bad_peers.emplace_back (boost::str (boost::format ("%1%:%2%") % endpoint_a.address () % endpoint_a.port ())); } diff --git a/nano/node/distributed_work.hpp b/nano/node/distributed_work.hpp index 29a26ba8c5..eb0e916686 100644 --- a/nano/node/distributed_work.hpp +++ b/nano/node/distributed_work.hpp @@ -104,7 +104,7 @@ class distributed_work final : public std::enable_shared_from_this bad_peers; // websocket std::string winner; // websocket - std::mutex mutex; + nano::mutex mutex; std::atomic resolved_extra{ 0 }; std::atomic failures{ 0 }; std::atomic finished{ false }; diff --git a/nano/node/distributed_work_factory.cpp b/nano/node/distributed_work_factory.cpp index 858537ef55..307e62115d 100644 --- a/nano/node/distributed_work_factory.cpp +++ b/nano/node/distributed_work_factory.cpp @@ -27,7 +27,7 @@ bool nano::distributed_work_factory::make (std::chrono::seconds const & backoff_ { auto distributed (std::make_shared (node, request_a, backoff_a)); { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); items.emplace (request_a.root, distributed); } distributed->start (); @@ -39,7 +39,7 @@ bool nano::distributed_work_factory::make (std::chrono::seconds const & backoff_ void nano::distributed_work_factory::cancel (nano::root const & root_a) { - nano::lock_guard guard_l (mutex); + nano::lock_guard guard_l (mutex); auto root_items_l = items.equal_range (root_a); std::for_each (root_items_l.first, root_items_l.second, [](auto item_l) { if (auto distributed_l = item_l.second.lock ()) @@ -53,7 +53,7 @@ void nano::distributed_work_factory::cancel (nano::root const & root_a) void nano::distributed_work_factory::cleanup_finished () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); // std::erase_if in c++20 auto erase_if = [](decltype (items) & container, auto pred) { for (auto it = container.begin (), end = container.end (); it != end;) @@ -76,7 +76,7 @@ void nano::distributed_work_factory::stop () if (!stopped.exchange (true)) { // Cancel any ongoing work - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); for (auto & item_l : items) { if (auto distributed_l = item_l.second.lock ()) @@ -90,7 +90,7 @@ void nano::distributed_work_factory::stop () size_t nano::distributed_work_factory::size () const { - nano::lock_guard guard_l (mutex); + nano::lock_guard guard_l (mutex); return items.size (); } diff --git a/nano/node/distributed_work_factory.hpp b/nano/node/distributed_work_factory.hpp index f8713c31a0..7c626b2834 100644 --- a/nano/node/distributed_work_factory.hpp +++ b/nano/node/distributed_work_factory.hpp @@ -32,7 +32,7 @@ class distributed_work_factory final std::unordered_multimap> items; nano::node & node; - mutable std::mutex mutex; + mutable nano::mutex mutex; std::atomic stopped{ false }; friend std::unique_ptr collect_container_info (distributed_work_factory &, const std::string &); diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 0d34b50da9..8976e31713 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -33,11 +33,11 @@ qualified_root (block_a->qualified_root ()) last_blocks.emplace (block_a->hash (), block_a); } -void nano::election::confirm_once (nano::unique_lock & lock_a, nano::election_status_type type_a) +void nano::election::confirm_once (nano::unique_lock & lock_a, nano::election_status_type type_a) { debug_assert (lock_a.owns_lock ()); // This must be kept above the setting of election state, as dependent confirmed elections require up to date changes to election_winner_details - nano::unique_lock election_winners_lk (node.active.election_winner_details_mutex); + nano::unique_lock election_winners_lk (node.active.election_winner_details_mutex); if (state_m.exchange (nano::election::state_t::confirmed) != nano::election::state_t::confirmed && (node.active.election_winner_details.count (status.winner->hash ()) == 0)) { node.active.election_winner_details.emplace (status.winner->hash (), shared_from_this ()); @@ -140,7 +140,7 @@ void nano::election::send_confirm_req (nano::confirmation_solicitor & solicitor_ { if ((base_latency () * (optimistic () ? 10 : 5)) < (std::chrono::steady_clock::now () - last_req)) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); if (!solicitor_a.add (*this)) { last_req = std::chrono::steady_clock::now (); @@ -168,7 +168,7 @@ void nano::election::broadcast_block (nano::confirmation_solicitor & solicitor_a { if (base_latency () * 15 < std::chrono::steady_clock::now () - last_block) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); if (!solicitor_a.broadcast (*this)) { last_block = std::chrono::steady_clock::now (); @@ -214,7 +214,7 @@ bool nano::election::transition_time (nano::confirmation_solicitor & solicitor_a auto const expire_time = std::chrono::milliseconds (optimistic () ? optimistic_expiration_time : 5 * 60 * 1000); if (!confirmed () && expire_time < std::chrono::steady_clock::now () - election_start) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); // It is possible the election confirmed while acquiring the mutex // state_change returning true would indicate it if (!state_change (state_m.load (), nano::election::state_t::expired_unconfirmed)) @@ -243,7 +243,7 @@ bool nano::election::have_quorum (nano::tally_t const & tally_a) const nano::tally_t nano::election::tally () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return tally_impl (); } @@ -267,7 +267,7 @@ nano::tally_t nano::election::tally_impl () const return result; } -void nano::election::confirm_if_quorum (nano::unique_lock & lock_a) +void nano::election::confirm_if_quorum (nano::unique_lock & lock_a) { debug_assert (lock_a.owns_lock ()); auto tally_l (tally_impl ()); @@ -320,7 +320,7 @@ void nano::election::log_votes (nano::tally_t const & tally_a, std::string const std::shared_ptr nano::election::find (nano::block_hash const & hash_a) const { std::shared_ptr result; - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); if (auto existing = last_blocks.find (hash_a); existing != last_blocks.end ()) { result = existing->second; @@ -350,7 +350,7 @@ nano::election_vote_result nano::election::vote (nano::account const & rep, uint cooldown = 1; } - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto last_vote_it (last_votes.find (rep)); if (last_vote_it == last_votes.end ()) @@ -385,7 +385,7 @@ nano::election_vote_result nano::election::vote (nano::account const & rep, uint bool nano::election::publish (std::shared_ptr const & block_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); // Do not insert new blocks if already confirmed auto result (confirmed ()); @@ -426,7 +426,7 @@ bool nano::election::publish (std::shared_ptr const & block_a) nano::election_cleanup_info nano::election::cleanup_info () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return cleanup_info_impl (); } @@ -442,7 +442,7 @@ nano::election_cleanup_info nano::election::cleanup_info_impl () const size_t nano::election::insert_inactive_votes_cache (nano::inactive_cache_information const & cache_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); for (auto const & rep : cache_a.voters) { auto inserted (last_votes.emplace (rep, nano::vote_info{ std::chrono::steady_clock::time_point::min (), 0, cache_a.hash })); @@ -492,7 +492,7 @@ void nano::election::prioritize (nano::vote_generator_session & generator_sessio nano::election_extended_status nano::election::current_status () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); nano::election_status status_l = status; status_l.confirmation_request_count = confirmation_request_count; status_l.block_count = nano::narrow_cast (last_blocks.size ()); @@ -502,7 +502,7 @@ nano::election_extended_status nano::election::current_status () const std::shared_ptr nano::election::winner () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return status.winner; } @@ -554,7 +554,7 @@ void nano::election::remove_block (nano::block_hash const & hash_a) } } -bool nano::election::replace_by_weight (nano::unique_lock & lock_a, nano::block_hash const & hash_a) +bool nano::election::replace_by_weight (nano::unique_lock & lock_a, nano::block_hash const & hash_a) { debug_assert (lock_a.owns_lock ()); nano::block_hash replaced_block (0); @@ -612,20 +612,20 @@ bool nano::election::replace_by_weight (nano::unique_lock & lock_a, void nano::election::force_confirm (nano::election_status_type type_a) { release_assert (node.network_params.network.is_dev_network ()); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); confirm_once (lock, type_a); } std::unordered_map> nano::election::blocks () const { debug_assert (node.network_params.network.is_dev_network ()); - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return last_blocks; } std::unordered_map nano::election::votes () const { debug_assert (node.network_params.network.is_dev_network ()); - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return last_votes; } diff --git a/nano/node/election.hpp b/nano/node/election.hpp index 13124dfd30..1c08ace3b1 100644 --- a/nano/node/election.hpp +++ b/nano/node/election.hpp @@ -109,7 +109,7 @@ class election final : public std::enable_shared_from_this bool publish (std::shared_ptr const & block_a); size_t insert_inactive_votes_cache (nano::inactive_cache_information const &); // Confirm this block if quorum is met - void confirm_if_quorum (nano::unique_lock &); + void confirm_if_quorum (nano::unique_lock &); void prioritize (nano::vote_generator_session &); nano::election_cleanup_info cleanup_info () const; @@ -121,14 +121,14 @@ class election final : public std::enable_shared_from_this private: nano::tally_t tally_impl () const; // lock_a does not own the mutex on return - void confirm_once (nano::unique_lock & lock_a, nano::election_status_type = nano::election_status_type::active_confirmed_quorum); + void confirm_once (nano::unique_lock & lock_a, nano::election_status_type = nano::election_status_type::active_confirmed_quorum); void broadcast_block (nano::confirmation_solicitor &); void send_confirm_req (nano::confirmation_solicitor &); // Calculate votes for local representatives void generate_votes () const; void remove_votes (nano::block_hash const &); void remove_block (nano::block_hash const &); - bool replace_by_weight (nano::unique_lock & lock_a, nano::block_hash const &); + bool replace_by_weight (nano::unique_lock & lock_a, nano::block_hash const &); nano::election_cleanup_info cleanup_info_impl () const; private: @@ -140,7 +140,7 @@ class election final : public std::enable_shared_from_this std::chrono::steady_clock::time_point const election_start = { std::chrono::steady_clock::now () }; nano::node & node; - mutable std::mutex mutex; + mutable nano::mutex mutex; static std::chrono::seconds constexpr late_blocks_delay{ 5 }; static size_t constexpr max_blocks{ 10 }; diff --git a/nano/node/gap_cache.cpp b/nano/node/gap_cache.cpp index 4fdf5cd1c0..e76f858a18 100644 --- a/nano/node/gap_cache.cpp +++ b/nano/node/gap_cache.cpp @@ -11,7 +11,7 @@ node (node_a) void nano::gap_cache::add (nano::block_hash const & hash_a, std::chrono::steady_clock::time_point time_point_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto existing (blocks.get ().find (hash_a)); if (existing != blocks.get ().end ()) { @@ -31,13 +31,13 @@ void nano::gap_cache::add (nano::block_hash const & hash_a, std::chrono::steady_ void nano::gap_cache::erase (nano::block_hash const & hash_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); blocks.get ().erase (hash_a); } void nano::gap_cache::vote (std::shared_ptr const & vote_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); for (auto hash : *vote_a) { auto & gap_blocks_by_hash (blocks.get ()); @@ -123,7 +123,7 @@ nano::uint128_t nano::gap_cache::bootstrap_threshold () size_t nano::gap_cache::size () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return blocks.size (); } diff --git a/nano/node/gap_cache.hpp b/nano/node/gap_cache.hpp index dc36931844..ab26008815 100644 --- a/nano/node/gap_cache.hpp +++ b/nano/node/gap_cache.hpp @@ -54,7 +54,7 @@ class gap_cache final ordered_gaps blocks; // clang-format on size_t const max = 256; - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::gap_cache) }; nano::node & node; }; diff --git a/nano/node/ipc/ipc_access_config.cpp b/nano/node/ipc/ipc_access_config.cpp index 8ff6ef3df2..97fabbc897 100644 --- a/nano/node/ipc/ipc_access_config.cpp +++ b/nano/node/ipc/ipc_access_config.cpp @@ -92,7 +92,7 @@ void nano::ipc::access::clear () nano::error nano::ipc::access::deserialize_toml (nano::tomlconfig & toml) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); clear (); nano::error error; @@ -208,7 +208,7 @@ nano::error nano::ipc::access::deserialize_toml (nano::tomlconfig & toml) bool nano::ipc::access::has_access (std::string const & credentials_a, nano::ipc::access_permission permssion_a) const { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); bool permitted = false; auto user = users.find (credentials_a); if (user != users.end ()) @@ -224,7 +224,7 @@ bool nano::ipc::access::has_access (std::string const & credentials_a, nano::ipc bool nano::ipc::access::has_access_to_all (std::string const & credentials_a, std::initializer_list permissions_a) const { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); bool permitted = false; auto user = users.find (credentials_a); if (user != users.end ()) @@ -243,7 +243,7 @@ bool nano::ipc::access::has_access_to_all (std::string const & credentials_a, st bool nano::ipc::access::has_access_to_oneof (std::string const & credentials_a, std::initializer_list permissions_a) const { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); bool permitted = false; auto user = users.find (credentials_a); if (user != users.end ()) diff --git a/nano/node/ipc/ipc_access_config.hpp b/nano/node/ipc/ipc_access_config.hpp index 076c4d1dc0..eba06d6480 100644 --- a/nano/node/ipc/ipc_access_config.hpp +++ b/nano/node/ipc/ipc_access_config.hpp @@ -2,8 +2,8 @@ #include #include +#include -#include #include #include #include @@ -125,7 +125,7 @@ namespace ipc */ access_user default_user; /** The config can be externally reloaded and concurrently accessed */ - mutable std::mutex mutex; + mutable nano::mutex mutex; }; nano::error read_access_config_toml (boost::filesystem::path const & data_path_a, nano::ipc::access & config_a); diff --git a/nano/node/ipc/ipc_server.cpp b/nano/node/ipc/ipc_server.cpp index cf7af155dd..c0869f5e90 100644 --- a/nano/node/ipc/ipc_server.cpp +++ b/nano/node/ipc/ipc_server.cpp @@ -128,8 +128,8 @@ class session final : public nano::ipc::socket_base, public std::enable_shared_f std::weak_ptr session_m; }; - static std::mutex subscriber_mutex; - nano::unique_lock lock (subscriber_mutex); + static nano::mutex subscriber_mutex; + nano::unique_lock lock (subscriber_mutex); if (!subscriber) { diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index ed28ca203f..477b2c8f28 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -1766,7 +1766,7 @@ void nano::json_handler::bootstrap_status () response_l.put ("total_attempts_count", std::to_string (node.bootstrap_initiator.attempts.incremental)); boost::property_tree::ptree connections; { - nano::lock_guard connections_lock (node.bootstrap_initiator.connections->mutex); + nano::lock_guard connections_lock (node.bootstrap_initiator.connections->mutex); connections.put ("clients", std::to_string (node.bootstrap_initiator.connections->clients.size ())); connections.put ("connections", std::to_string (node.bootstrap_initiator.connections->connections_count)); connections.put ("idle", std::to_string (node.bootstrap_initiator.connections->idle.size ())); @@ -1776,7 +1776,7 @@ void nano::json_handler::bootstrap_status () response_l.add_child ("connections", connections); boost::property_tree::ptree attempts; { - nano::lock_guard attempts_lock (node.bootstrap_initiator.attempts.bootstrap_attempts_mutex); + nano::lock_guard attempts_lock (node.bootstrap_initiator.attempts.bootstrap_attempts_mutex); for (auto i : node.bootstrap_initiator.attempts.attempts) { boost::property_tree::ptree entry; diff --git a/nano/node/lmdb/lmdb_txn.cpp b/nano/node/lmdb/lmdb_txn.cpp index 7d60a62c71..42fc019a8e 100644 --- a/nano/node/lmdb/lmdb_txn.cpp +++ b/nano/node/lmdb/lmdb_txn.cpp @@ -138,7 +138,7 @@ void nano::mdb_txn_tracker::serialize_json (boost::property_tree::ptree & json, std::vector copy_stats; std::vector are_writes; { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); copy_stats = stats; are_writes.reserve (stats.size ()); std::transform (stats.cbegin (), stats.cend (), std::back_inserter (are_writes), [](auto & mdb_txn_stat) { @@ -209,7 +209,7 @@ void nano::mdb_txn_tracker::log_if_held_long_enough (nano::mdb_txn_stats const & void nano::mdb_txn_tracker::add (const nano::transaction_impl * transaction_impl) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); debug_assert (std::find_if (stats.cbegin (), stats.cend (), matches_txn (transaction_impl)) == stats.cend ()); stats.emplace_back (transaction_impl); } @@ -217,7 +217,7 @@ void nano::mdb_txn_tracker::add (const nano::transaction_impl * transaction_impl /** Can be called without error if transaction does not exist */ void nano::mdb_txn_tracker::erase (const nano::transaction_impl * transaction_impl) { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); auto it = std::find_if (stats.begin (), stats.end (), matches_txn (transaction_impl)); if (it != stats.end ()) { diff --git a/nano/node/lmdb/lmdb_txn.hpp b/nano/node/lmdb/lmdb_txn.hpp index 34b68902fd..9d23a65e78 100644 --- a/nano/node/lmdb/lmdb_txn.hpp +++ b/nano/node/lmdb/lmdb_txn.hpp @@ -73,7 +73,7 @@ class mdb_txn_tracker void erase (const nano::transaction_impl * transaction_impl); private: - std::mutex mutex; + nano::mutex mutex; std::vector stats; nano::logger_mt & logger; nano::txn_tracking_config txn_tracking_config; diff --git a/nano/node/network.cpp b/nano/node/network.cpp index 1cfc170072..e9ae7ea33a 100644 --- a/nano/node/network.cpp +++ b/nano/node/network.cpp @@ -816,7 +816,7 @@ stopped (false) nano::message_buffer * nano::message_buffer_manager::allocate () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (!stopped && free.empty () && full.empty ()) { stats.inc (nano::stat::type::udp, nano::stat::detail::blocking, nano::stat::dir::in); @@ -842,7 +842,7 @@ void nano::message_buffer_manager::enqueue (nano::message_buffer * data_a) { debug_assert (data_a != nullptr); { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); full.push_back (data_a); } condition.notify_all (); @@ -850,7 +850,7 @@ void nano::message_buffer_manager::enqueue (nano::message_buffer * data_a) nano::message_buffer * nano::message_buffer_manager::dequeue () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (!stopped && full.empty ()) { condition.wait (lock); @@ -868,7 +868,7 @@ void nano::message_buffer_manager::release (nano::message_buffer * data_a) { debug_assert (data_a != nullptr); { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); free.push_back (data_a); } condition.notify_all (); @@ -877,7 +877,7 @@ void nano::message_buffer_manager::release (nano::message_buffer * data_a) void nano::message_buffer_manager::stop () { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); stopped = true; } condition.notify_all (); @@ -892,7 +892,7 @@ max_entries (incoming_connections_max_a * nano::tcp_message_manager::max_entries void nano::tcp_message_manager::put_message (nano::tcp_message_item const & item_a) { { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (entries.size () >= max_entries && !stopped) { producer_condition.wait (lock); @@ -905,7 +905,7 @@ void nano::tcp_message_manager::put_message (nano::tcp_message_item const & item nano::tcp_message_item nano::tcp_message_manager::get_message () { nano::tcp_message_item result; - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (entries.empty () && !stopped) { consumer_condition.wait (lock); @@ -927,7 +927,7 @@ nano::tcp_message_item nano::tcp_message_manager::get_message () void nano::tcp_message_manager::stop () { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); stopped = true; } consumer_condition.notify_all (); @@ -943,7 +943,7 @@ boost::optional nano::syn_cookies::assign (nano::endpoint c { auto ip_addr (endpoint_a.address ()); debug_assert (ip_addr.is_v6 ()); - nano::lock_guard lock (syn_cookie_mutex); + nano::lock_guard lock (syn_cookie_mutex); unsigned & ip_cookies = cookies_per_ip[ip_addr]; boost::optional result; if (ip_cookies < max_cookies_per_ip) @@ -965,7 +965,7 @@ bool nano::syn_cookies::validate (nano::endpoint const & endpoint_a, nano::accou { auto ip_addr (endpoint_a.address ()); debug_assert (ip_addr.is_v6 ()); - nano::lock_guard lock (syn_cookie_mutex); + nano::lock_guard lock (syn_cookie_mutex); auto result (true); auto cookie_it (cookies.find (endpoint_a)); if (cookie_it != cookies.end () && !nano::validate_message (node_id, cookie_it->second.cookie, sig)) @@ -987,7 +987,7 @@ bool nano::syn_cookies::validate (nano::endpoint const & endpoint_a, nano::accou void nano::syn_cookies::purge (std::chrono::steady_clock::time_point const & cutoff_a) { - nano::lock_guard lock (syn_cookie_mutex); + nano::lock_guard lock (syn_cookie_mutex); auto it (cookies.begin ()); while (it != cookies.end ()) { @@ -1014,7 +1014,7 @@ void nano::syn_cookies::purge (std::chrono::steady_clock::time_point const & cut size_t nano::syn_cookies::cookies_size () { - nano::lock_guard lock (syn_cookie_mutex); + nano::lock_guard lock (syn_cookie_mutex); return cookies.size (); } @@ -1033,7 +1033,7 @@ std::unique_ptr nano::syn_cookies::collect_conta size_t syn_cookies_count; size_t syn_cookies_per_ip_count; { - nano::lock_guard syn_cookie_guard (syn_cookie_mutex); + nano::lock_guard syn_cookie_guard (syn_cookie_mutex); syn_cookies_count = cookies.size (); syn_cookies_per_ip_count = cookies_per_ip.size (); } diff --git a/nano/node/network.hpp b/nano/node/network.hpp index 3620801216..b09bc2e50d 100644 --- a/nano/node/network.hpp +++ b/nano/node/network.hpp @@ -58,7 +58,7 @@ class message_buffer_manager final private: nano::stat & stats; - std::mutex mutex; + nano::mutex mutex; nano::condition_variable condition; boost::circular_buffer free; boost::circular_buffer full; @@ -76,7 +76,7 @@ class tcp_message_manager final void stop (); private: - std::mutex mutex; + nano::mutex mutex; nano::condition_variable producer_condition; nano::condition_variable consumer_condition; std::deque entries; @@ -110,7 +110,7 @@ class syn_cookies final nano::uint256_union cookie; std::chrono::steady_clock::time_point created_at; }; - mutable std::mutex syn_cookie_mutex; + mutable nano::mutex syn_cookie_mutex; std::unordered_map cookies; std::unordered_map cookies_per_ip; size_t max_cookies_per_ip; diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 19bab75da2..b74b893bab 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -69,7 +69,7 @@ std::unique_ptr nano::collect_container_info (re { size_t count; { - nano::lock_guard guard (rep_crawler.active_mutex); + nano::lock_guard guard (rep_crawler.active_mutex); count = rep_crawler.active.size (); } @@ -908,7 +908,7 @@ void nano::node::bootstrap_wallet () { std::deque accounts; { - nano::lock_guard lock (wallets.mutex); + nano::lock_guard lock (wallets.mutex); auto transaction (wallets.tx_begin_read ()); for (auto i (wallets.items.begin ()), n (wallets.items.end ()); i != n && accounts.size () < 128; ++i) { @@ -1280,7 +1280,7 @@ void nano::node::ongoing_online_weight_calculation () void nano::node::receive_confirmed (nano::transaction const & block_transaction_a, nano::block_hash const & hash_a, nano::account const & destination_a) { - nano::unique_lock lk (wallets.mutex); + nano::unique_lock lk (wallets.mutex); auto wallets_l = wallets.get_wallets (); auto wallet_transaction = wallets.tx_begin_read (); lk.unlock (); @@ -1383,7 +1383,7 @@ void nano::node::process_confirmed (nano::election_status const & status_a, uint bool nano::block_arrival::add (nano::block_hash const & hash_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto now (std::chrono::steady_clock::now ()); auto inserted (arrival.get ().emplace_back (nano::block_arrival_info{ now, hash_a })); auto result (!inserted.second); @@ -1392,7 +1392,7 @@ bool nano::block_arrival::add (nano::block_hash const & hash_a) bool nano::block_arrival::recent (nano::block_hash const & hash_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto now (std::chrono::steady_clock::now ()); while (arrival.size () > arrival_size_min && arrival.get ().front ().arrival + arrival_time_min < now) { @@ -1405,7 +1405,7 @@ std::unique_ptr nano::collect_container_info (bl { size_t count = 0; { - nano::lock_guard guard (block_arrival.mutex); + nano::lock_guard guard (block_arrival.mutex); count = block_arrival.arrival.size (); } @@ -1477,7 +1477,7 @@ void nano::node::epoch_upgrader_impl (nano::private_key const & prv_a, nano::epo auto signer (nano::pub_key (prv_a)); debug_assert (signer == ledger.epoch_signer (link)); - std::mutex upgrader_mutex; + nano::mutex upgrader_mutex; nano::condition_variable upgrader_condition; class account_upgrade_item final @@ -1553,7 +1553,7 @@ void nano::node::epoch_upgrader_impl (nano::private_key const & prv_a, nano::epo if (threads != 0) { { - nano::unique_lock lock (upgrader_mutex); + nano::unique_lock lock (upgrader_mutex); ++workers; while (workers > threads) { @@ -1563,7 +1563,7 @@ void nano::node::epoch_upgrader_impl (nano::private_key const & prv_a, nano::epo this->workers.push_task ([node_l = shared_from_this (), &upgrader_process, &upgrader_mutex, &upgrader_condition, &upgraded_accounts, &workers, epoch, difficulty, signer, root, account]() { upgrader_process (*node_l, upgraded_accounts, epoch, difficulty, signer, root, account); { - nano::lock_guard lock (upgrader_mutex); + nano::lock_guard lock (upgrader_mutex); --workers; } upgrader_condition.notify_all (); @@ -1576,7 +1576,7 @@ void nano::node::epoch_upgrader_impl (nano::private_key const & prv_a, nano::epo } } { - nano::unique_lock lock (upgrader_mutex); + nano::unique_lock lock (upgrader_mutex); while (workers > 0) { upgrader_condition.wait (lock); @@ -1632,7 +1632,7 @@ void nano::node::epoch_upgrader_impl (nano::private_key const & prv_a, nano::epo if (threads != 0) { { - nano::unique_lock lock (upgrader_mutex); + nano::unique_lock lock (upgrader_mutex); ++workers; while (workers > threads) { @@ -1642,7 +1642,7 @@ void nano::node::epoch_upgrader_impl (nano::private_key const & prv_a, nano::epo this->workers.push_task ([node_l = shared_from_this (), &upgrader_process, &upgrader_mutex, &upgrader_condition, &upgraded_pending, &workers, epoch, difficulty, signer, root, account]() { upgrader_process (*node_l, upgraded_pending, epoch, difficulty, signer, root, account); { - nano::lock_guard lock (upgrader_mutex); + nano::lock_guard lock (upgrader_mutex); --workers; } upgrader_condition.notify_all (); @@ -1677,7 +1677,7 @@ void nano::node::epoch_upgrader_impl (nano::private_key const & prv_a, nano::epo } } { - nano::unique_lock lock (upgrader_mutex); + nano::unique_lock lock (upgrader_mutex); while (workers > 0) { upgrader_condition.wait (lock); diff --git a/nano/node/node.hpp b/nano/node/node.hpp index 53b06dc09b..e5bbc4d361 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -73,7 +73,7 @@ class block_arrival final boost::multi_index::member>>> arrival; // clang-format on - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::block_arrival) }; static size_t constexpr arrival_size_min = 8 * 1024; static std::chrono::seconds constexpr arrival_time_min = std::chrono::seconds (300); }; diff --git a/nano/node/online_reps.cpp b/nano/node/online_reps.cpp index 47c7f76512..533b2ccb21 100644 --- a/nano/node/online_reps.cpp +++ b/nano/node/online_reps.cpp @@ -18,7 +18,7 @@ void nano::online_reps::observe (nano::account const & rep_a) { if (ledger.weight (rep_a) > 0) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto now = std::chrono::steady_clock::now (); auto new_insert = reps.get ().erase (rep_a) == 0; reps.insert ({ now, rep_a }); @@ -34,7 +34,7 @@ void nano::online_reps::observe (nano::account const & rep_a) void nano::online_reps::sample () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); nano::uint128_t online_l = online_m; lock.unlock (); nano::uint128_t trend_l; @@ -83,19 +83,19 @@ nano::uint128_t nano::online_reps::calculate_trend (nano::transaction & transact nano::uint128_t nano::online_reps::trended () const { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return trended_m; } nano::uint128_t nano::online_reps::online () const { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return online_m; } nano::uint128_t nano::online_reps::delta () const { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); // Using a larger container to ensure maximum precision auto weight = static_cast (std::max ({ online_m, trended_m, config.online_weight_minimum.number () })); return ((weight * online_weight_quorum) / 100).convert_to (); @@ -104,14 +104,14 @@ nano::uint128_t nano::online_reps::delta () const std::vector nano::online_reps::list () { std::vector result; - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); std::for_each (reps.begin (), reps.end (), [&result](rep_info const & info_a) { result.push_back (info_a.account); }); return result; } void nano::online_reps::clear () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); reps.clear (); online_m = 0; } @@ -120,7 +120,7 @@ std::unique_ptr nano::collect_container_info (on { size_t count; { - nano::lock_guard guard (online_reps.mutex); + nano::lock_guard guard (online_reps.mutex); count = online_reps.reps.size (); } diff --git a/nano/node/online_reps.hpp b/nano/node/online_reps.hpp index d638581983..9e327ef3de 100644 --- a/nano/node/online_reps.hpp +++ b/nano/node/online_reps.hpp @@ -54,7 +54,7 @@ class online_reps final }; nano::uint128_t calculate_trend (nano::transaction &) const; nano::uint128_t calculate_online () const; - mutable std::mutex mutex; + mutable nano::mutex mutex; nano::ledger & ledger; nano::node_config const & config; boost::multi_index_container nano::opencl_work::generate_work (nano::work_version c boost::optional nano::opencl_work::generate_work (nano::work_version const version_a, nano::root const & root_a, uint64_t const difficulty_a, std::atomic & ticket_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); bool error (false); int ticket_l (ticket_a); uint64_t result (0); diff --git a/nano/node/openclwork.hpp b/nano/node/openclwork.hpp index c06f977caa..b7e4b5041c 100644 --- a/nano/node/openclwork.hpp +++ b/nano/node/openclwork.hpp @@ -46,7 +46,7 @@ class opencl_work boost::optional generate_work (nano::work_version const, nano::root const &, uint64_t const, std::atomic &); static std::unique_ptr create (bool, nano::opencl_config const &, nano::logger_mt &); nano::opencl_config const & config; - std::mutex mutex; + nano::mutex mutex; cl_context context; cl_mem attempt_buffer; cl_mem result_buffer; diff --git a/nano/node/peer_exclusion.cpp b/nano/node/peer_exclusion.cpp index 02a6b5d7e4..bb3122f1c5 100644 --- a/nano/node/peer_exclusion.cpp +++ b/nano/node/peer_exclusion.cpp @@ -8,7 +8,7 @@ constexpr double nano::peer_exclusion::peers_percentage_limit; uint64_t nano::peer_exclusion::add (nano::tcp_endpoint const & endpoint_a, size_t const network_peers_count_a) { uint64_t result (0); - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); // Clean old excluded peers auto limited = limited_size (network_peers_count_a); while (peers.size () > 1 && peers.size () > limited) @@ -49,7 +49,7 @@ uint64_t nano::peer_exclusion::add (nano::tcp_endpoint const & endpoint_a, size_ bool nano::peer_exclusion::check (nano::tcp_endpoint const & endpoint_a) { bool excluded (false); - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto & peers_by_endpoint (peers.get ()); auto existing (peers_by_endpoint.find (endpoint_a.address ())); if (existing != peers_by_endpoint.end () && existing->score >= score_limit) @@ -68,7 +68,7 @@ bool nano::peer_exclusion::check (nano::tcp_endpoint const & endpoint_a) void nano::peer_exclusion::remove (nano::tcp_endpoint const & endpoint_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); peers.get ().erase (endpoint_a.address ()); } @@ -79,7 +79,7 @@ size_t nano::peer_exclusion::limited_size (size_t const network_peers_count_a) c size_t nano::peer_exclusion::size () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return peers.size (); } diff --git a/nano/node/peer_exclusion.hpp b/nano/node/peer_exclusion.hpp index a20227a214..cadaab5cb7 100644 --- a/nano/node/peer_exclusion.hpp +++ b/nano/node/peer_exclusion.hpp @@ -37,7 +37,7 @@ class peer_exclusion final private: ordered_endpoints peers; - mutable std::mutex mutex; + mutable nano::mutex mutex; public: constexpr static size_t size_max = 5000; diff --git a/nano/node/portmapping.cpp b/nano/node/portmapping.cpp index 408faf12ec..f38a0542c8 100644 --- a/nano/node/portmapping.cpp +++ b/nano/node/portmapping.cpp @@ -48,7 +48,7 @@ void nano::port_mapping::refresh_devices () } } // Update port mapping - nano::lock_guard guard_l (mutex); + nano::lock_guard guard_l (mutex); upnp = std::move (upnp_l); if (igd_error_l == 1 || igd_error_l == 2) { @@ -61,7 +61,7 @@ void nano::port_mapping::refresh_devices () nano::endpoint nano::port_mapping::external_address () { nano::endpoint result_l (boost::asio::ip::address_v6{}, 0); - nano::lock_guard guard_l (mutex); + nano::lock_guard guard_l (mutex); for (auto & protocol : protocols | boost::adaptors::filtered ([](auto const & p) { return p.enabled; })) { if (protocol.external_port != 0) @@ -77,7 +77,7 @@ void nano::port_mapping::refresh_mapping () debug_assert (!network_params.network.is_dev_network ()); if (on) { - nano::lock_guard guard_l (mutex); + nano::lock_guard guard_l (mutex); auto node_port_l (std::to_string (node.network.endpoint ().port ())); auto config_port_l (get_config_port (node_port_l)); @@ -114,7 +114,7 @@ bool nano::port_mapping::check_mapping () // Long discovery time and fast setup/teardown make this impractical for testing debug_assert (!network_params.network.is_dev_network ()); bool result_l (true); - nano::lock_guard guard_l (mutex); + nano::lock_guard guard_l (mutex); auto node_port_l (std::to_string (node.network.endpoint ().port ())); auto config_port_l (get_config_port (node_port_l)); for (auto & protocol : protocols | boost::adaptors::filtered ([](auto const & p) { return p.enabled; })) @@ -187,7 +187,7 @@ void nano::port_mapping::check_mapping_loop () void nano::port_mapping::stop () { on = false; - nano::lock_guard guard_l (mutex); + nano::lock_guard guard_l (mutex); for (auto & protocol : protocols | boost::adaptors::filtered ([](auto const & p) { return p.enabled; })) { if (protocol.external_port != 0) diff --git a/nano/node/portmapping.hpp b/nano/node/portmapping.hpp index 198805868f..ae36cc0937 100644 --- a/nano/node/portmapping.hpp +++ b/nano/node/portmapping.hpp @@ -60,6 +60,6 @@ class port_mapping std::array protocols; uint64_t check_count{ 0 }; std::atomic on{ false }; - std::mutex mutex; + nano::mutex mutex; }; } diff --git a/nano/node/repcrawler.cpp b/nano/node/repcrawler.cpp index 9e31422753..c30888b999 100644 --- a/nano/node/repcrawler.cpp +++ b/nano/node/repcrawler.cpp @@ -16,7 +16,7 @@ node (node_a) void nano::rep_crawler::remove (nano::block_hash const & hash_a) { - nano::lock_guard lock (active_mutex); + nano::lock_guard lock (active_mutex); active.erase (hash_a); } @@ -29,7 +29,7 @@ void nano::rep_crawler::validate () { decltype (responses) responses_l; { - nano::lock_guard lock (active_mutex); + nano::lock_guard lock (active_mutex); responses_l.swap (responses); } auto minimum = node.minimum_principal_weight (); @@ -44,7 +44,7 @@ void nano::rep_crawler::validate () if (rep_weight > minimum) { auto updated_or_inserted = false; - nano::unique_lock lock (probable_reps_mutex); + nano::unique_lock lock (probable_reps_mutex); auto existing (probable_reps.find (vote->account)); if (existing != probable_reps.end ()) { @@ -128,7 +128,7 @@ void nano::rep_crawler::query (std::vector lock (active_mutex); + nano::lock_guard lock (active_mutex); // Don't send same block multiple times in tests if (node.network_params.network.is_dev_network ()) { @@ -169,7 +169,7 @@ void nano::rep_crawler::query (std::shared_ptr const & bool nano::rep_crawler::is_pr (nano::transport::channel const & channel_a) const { - nano::lock_guard lock (probable_reps_mutex); + nano::lock_guard lock (probable_reps_mutex); auto existing = probable_reps.get ().find (channel_a); bool result = false; if (existing != probable_reps.get ().end ()) @@ -182,7 +182,7 @@ bool nano::rep_crawler::is_pr (nano::transport::channel const & channel_a) const bool nano::rep_crawler::response (std::shared_ptr const & channel_a, std::shared_ptr const & vote_a) { bool error = true; - nano::lock_guard lock (active_mutex); + nano::lock_guard lock (active_mutex); for (auto i = vote_a->begin (), n = vote_a->end (); i != n; ++i) { if (active.count (*i) != 0) @@ -197,7 +197,7 @@ bool nano::rep_crawler::response (std::shared_ptr cons nano::uint128_t nano::rep_crawler::total_weight () const { - nano::lock_guard lock (probable_reps_mutex); + nano::lock_guard lock (probable_reps_mutex); nano::uint128_t result (0); for (auto i (probable_reps.get ().begin ()), n (probable_reps.get ().end ()); i != n; ++i) { @@ -216,7 +216,7 @@ nano::uint128_t nano::rep_crawler::total_weight () const void nano::rep_crawler::on_rep_request (std::shared_ptr const & channel_a) { - nano::lock_guard lock (probable_reps_mutex); + nano::lock_guard lock (probable_reps_mutex); if (channel_a->get_tcp_endpoint ().address () != boost::asio::ip::address_v6::any ()) { probably_rep_t::index::type & channel_ref_index = probable_reps.get (); @@ -237,7 +237,7 @@ void nano::rep_crawler::cleanup_reps () std::vector> channels; { // Check known rep channels - nano::lock_guard lock (probable_reps_mutex); + nano::lock_guard lock (probable_reps_mutex); auto iterator (probable_reps.get ().begin ()); while (iterator != probable_reps.get ().end ()) { @@ -275,7 +275,7 @@ void nano::rep_crawler::cleanup_reps () } if (!equal) { - nano::lock_guard lock (probable_reps_mutex); + nano::lock_guard lock (probable_reps_mutex); probable_reps.get ().erase (*i); } } @@ -283,7 +283,7 @@ void nano::rep_crawler::cleanup_reps () void nano::rep_crawler::update_weights () { - nano::lock_guard lock (probable_reps_mutex); + nano::lock_guard lock (probable_reps_mutex); for (auto i (probable_reps.get ().begin ()), n (probable_reps.get ().end ()); i != n;) { auto weight (node.ledger.weight (i->account)); @@ -309,7 +309,7 @@ std::vector nano::rep_crawler::representatives (size_t cou { auto version_min (opt_version_min_a.value_or (node.network_params.protocol.protocol_version_min ())); std::vector result; - nano::lock_guard lock (probable_reps_mutex); + nano::lock_guard lock (probable_reps_mutex); for (auto i (probable_reps.get ().begin ()), n (probable_reps.get ().end ()); i != n && result.size () < count_a; ++i) { if (i->weight > weight_a && i->channel->get_network_version () >= version_min) @@ -339,6 +339,6 @@ std::vector> nano::rep_crawler::repres /** Total number of representatives */ size_t nano::rep_crawler::representative_count () { - nano::lock_guard lock (probable_reps_mutex); + nano::lock_guard lock (probable_reps_mutex); return probable_reps.size (); } diff --git a/nano/node/repcrawler.hpp b/nano/node/repcrawler.hpp index 55eac5a225..aeb2c843dd 100644 --- a/nano/node/repcrawler.hpp +++ b/nano/node/repcrawler.hpp @@ -118,7 +118,7 @@ class rep_crawler final nano::node & node; /** Protects the active-hash container */ - std::mutex active_mutex; + nano::mutex active_mutex; /** We have solicted votes for these random blocks */ std::unordered_set active; @@ -142,7 +142,7 @@ class rep_crawler final void update_weights (); /** Protects the probable_reps container */ - mutable std::mutex probable_reps_mutex; + mutable nano::mutex probable_reps_mutex; /** Probable representatives */ probably_rep_t probable_reps; diff --git a/nano/node/request_aggregator.cpp b/nano/node/request_aggregator.cpp index 0daaffeb67..815d6b4ee2 100644 --- a/nano/node/request_aggregator.cpp +++ b/nano/node/request_aggregator.cpp @@ -26,7 +26,7 @@ thread ([this]() { run (); }) generator.set_reply_action ([this](std::shared_ptr const & vote_a, std::shared_ptr const & channel_a) { this->reply_action (vote_a, channel_a); }); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); condition.wait (lock, [& started = started] { return started; }); } @@ -35,7 +35,7 @@ void nano::request_aggregator::add (std::shared_ptr co debug_assert (wallets.reps ().voting > 0); bool error = true; auto const endpoint (nano::transport::map_endpoint_to_v6 (channel_a->get_endpoint ())); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); // Protecting from ever-increasing memory usage when request are consumed slower than generated // Reject request if the oldest request has not yet been processed after its deadline + a modest margin if (requests.empty () || (requests.get ().begin ()->deadline + 2 * this->max_delay > std::chrono::steady_clock::now ())) @@ -69,7 +69,7 @@ void nano::request_aggregator::add (std::shared_ptr co void nano::request_aggregator::run () { nano::thread_role::set (nano::thread_role::name::request_aggregator); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); started = true; lock.unlock (); condition.notify_all (); @@ -117,7 +117,7 @@ void nano::request_aggregator::run () void nano::request_aggregator::stop () { { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); stopped = true; } condition.notify_all (); @@ -129,7 +129,7 @@ void nano::request_aggregator::stop () std::size_t nano::request_aggregator::size () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); return requests.size (); } diff --git a/nano/node/request_aggregator.hpp b/nano/node/request_aggregator.hpp index f3d06aa389..6e9edfe6f6 100644 --- a/nano/node/request_aggregator.hpp +++ b/nano/node/request_aggregator.hpp @@ -100,7 +100,7 @@ class request_aggregator final bool stopped{ false }; bool started{ false }; nano::condition_variable condition; - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::request_aggregator) }; std::thread thread; friend std::unique_ptr collect_container_info (request_aggregator &, const std::string &); diff --git a/nano/node/rocksdb/rocksdb.hpp b/nano/node/rocksdb/rocksdb.hpp index 71a7874e69..14cfaadef8 100644 --- a/nano/node/rocksdb/rocksdb.hpp +++ b/nano/node/rocksdb/rocksdb.hpp @@ -72,7 +72,7 @@ class rocksdb_store : public block_store_partial std::unique_ptr db; std::vector> handles; std::shared_ptr small_table_factory; - std::unordered_map write_lock_mutexes; + std::unordered_map write_lock_mutexes; nano::rocksdb_config rocksdb_config; unsigned const max_block_write_batch_num_m; diff --git a/nano/node/rocksdb/rocksdb_txn.cpp b/nano/node/rocksdb/rocksdb_txn.cpp index 815c2e7575..e3ab6f1582 100644 --- a/nano/node/rocksdb/rocksdb_txn.cpp +++ b/nano/node/rocksdb/rocksdb_txn.cpp @@ -32,7 +32,7 @@ void * nano::read_rocksdb_txn::get_handle () const return (void *)&options; } -nano::write_rocksdb_txn::write_rocksdb_txn (rocksdb::OptimisticTransactionDB * db_a, std::vector const & tables_requiring_locks_a, std::vector const & tables_no_locks_a, std::unordered_map & mutexes_a) : +nano::write_rocksdb_txn::write_rocksdb_txn (rocksdb::OptimisticTransactionDB * db_a, std::vector const & tables_requiring_locks_a, std::vector const & tables_no_locks_a, std::unordered_map & mutexes_a) : db (db_a), tables_requiring_locks (tables_requiring_locks_a), tables_no_locks (tables_no_locks_a), diff --git a/nano/node/rocksdb/rocksdb_txn.hpp b/nano/node/rocksdb/rocksdb_txn.hpp index 859d877e0e..14b8565c71 100644 --- a/nano/node/rocksdb/rocksdb_txn.hpp +++ b/nano/node/rocksdb/rocksdb_txn.hpp @@ -28,7 +28,7 @@ class read_rocksdb_txn final : public read_transaction_impl class write_rocksdb_txn final : public write_transaction_impl { public: - write_rocksdb_txn (rocksdb::OptimisticTransactionDB * db_a, std::vector const & tables_requiring_locks_a, std::vector const & tables_no_locks_a, std::unordered_map & mutexes_a); + write_rocksdb_txn (rocksdb::OptimisticTransactionDB * db_a, std::vector const & tables_requiring_locks_a, std::vector const & tables_no_locks_a, std::unordered_map & mutexes_a); ~write_rocksdb_txn (); void commit () override; void renew () override; @@ -40,7 +40,7 @@ class write_rocksdb_txn final : public write_transaction_impl rocksdb::OptimisticTransactionDB * db; std::vector tables_requiring_locks; std::vector tables_no_locks; - std::unordered_map & mutexes; + std::unordered_map & mutexes; bool active{ true }; void lock (); diff --git a/nano/node/state_block_signature_verification.cpp b/nano/node/state_block_signature_verification.cpp index cfe6ba6715..e6ed7f854c 100644 --- a/nano/node/state_block_signature_verification.cpp +++ b/nano/node/state_block_signature_verification.cpp @@ -29,7 +29,7 @@ nano::state_block_signature_verification::~state_block_signature_verification () void nano::state_block_signature_verification::stop () { { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); stopped = true; } @@ -42,7 +42,7 @@ void nano::state_block_signature_verification::stop () void nano::state_block_signature_verification::run (uint64_t state_block_signature_verification_size) { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); while (!stopped) { if (!state_blocks.empty ()) @@ -70,14 +70,14 @@ void nano::state_block_signature_verification::run (uint64_t state_block_signatu bool nano::state_block_signature_verification::is_active () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return active; } void nano::state_block_signature_verification::add (nano::unchecked_info const & info_a, bool watch_work_a) { { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); state_blocks.emplace_back (info_a, watch_work_a); } condition.notify_one (); @@ -85,7 +85,7 @@ void nano::state_block_signature_verification::add (nano::unchecked_info const & size_t nano::state_block_signature_verification::size () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return state_blocks.size (); } diff --git a/nano/node/state_block_signature_verification.hpp b/nano/node/state_block_signature_verification.hpp index cd50f46f81..b534d56980 100644 --- a/nano/node/state_block_signature_verification.hpp +++ b/nano/node/state_block_signature_verification.hpp @@ -33,7 +33,7 @@ class state_block_signature_verification nano::node_config & node_config; nano::logger_mt & logger; - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::state_block_signature_verification) }; bool stopped{ false }; bool active{ false }; std::deque> state_blocks; diff --git a/nano/node/telemetry.cpp b/nano/node/telemetry.cpp index d97b55e3d4..5617aae838 100644 --- a/nano/node/telemetry.cpp +++ b/nano/node/telemetry.cpp @@ -46,7 +46,7 @@ void nano::telemetry::set (nano::telemetry_ack const & message_a, nano::transpor { if (!stopped) { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); nano::endpoint endpoint = channel_a.get_endpoint (); auto it = recent_or_initial_request_telemetry_data.find (endpoint); if (it == recent_or_initial_request_telemetry_data.cend () || !it->undergoing_request) @@ -181,7 +181,7 @@ void nano::telemetry::ongoing_req_all_peers (std::chrono::milliseconds next_requ { // Cleanup any stale saved telemetry data for non-existent peers - nano::lock_guard guard (this_l->mutex); + nano::lock_guard guard (this_l->mutex); for (auto it = this_l->recent_or_initial_request_telemetry_data.begin (); it != this_l->recent_or_initial_request_telemetry_data.end ();) { if (!it->undergoing_request && !this_l->within_cache_cutoff (*it) && peers.count (it->endpoint) == 0) @@ -218,7 +218,7 @@ void nano::telemetry::ongoing_req_all_peers (std::chrono::milliseconds next_requ } // Schedule the next request; Use the default request time unless a telemetry request cache expires sooner - nano::lock_guard guard (this_l->mutex); + nano::lock_guard guard (this_l->mutex); long long next_round = std::chrono::duration_cast (this_l->cache_cutoff + this_l->response_time_cutoff).count (); if (!this_l->recent_or_initial_request_telemetry_data.empty ()) { @@ -249,7 +249,7 @@ std::unordered_map nano::telemetry::get_me { std::unordered_map telemetry_data; - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto range = boost::make_iterator_range (recent_or_initial_request_telemetry_data); // clang-format off nano::transform_if (range.begin (), range.end (), std::inserter (telemetry_data, telemetry_data.end ()), @@ -286,7 +286,7 @@ void nano::telemetry::get_metrics_single_peer_async (std::shared_ptr guard (mutex); + nano::lock_guard guard (mutex); auto it = recent_or_initial_request_telemetry_data.find (channel_a->get_endpoint ()); if (it != recent_or_initial_request_telemetry_data.cend () && within_cache_cutoff (*it)) { @@ -361,7 +361,7 @@ void nano::telemetry::fire_request_message (std::shared_ptrstats.inc (nano::stat::type::telemetry, nano::stat::detail::failed_send_telemetry_req); - nano::lock_guard guard (this_l->mutex); + nano::lock_guard guard (this_l->mutex); this_l->channel_processed (endpoint, true); } else @@ -370,7 +370,7 @@ void nano::telemetry::fire_request_message (std::shared_ptrworkers.add_timed_task (std::chrono::steady_clock::now () + this_l->response_time_cutoff, [round_l, this_w, endpoint]() { if (auto this_l = this_w.lock ()) { - nano::lock_guard guard (this_l->mutex); + nano::lock_guard guard (this_l->mutex); auto it = this_l->recent_or_initial_request_telemetry_data.find (endpoint); if (it != this_l->recent_or_initial_request_telemetry_data.cend () && it->undergoing_request && round_l == it->round) { @@ -412,7 +412,7 @@ void nano::telemetry::flush_callbacks_async (nano::endpoint const & endpoint_a, workers.push_task ([endpoint_a, error_a, this_w = std::weak_ptr (shared_from_this ())]() { if (auto this_l = this_w.lock ()) { - nano::unique_lock lk (this_l->mutex); + nano::unique_lock lk (this_l->mutex); while (!this_l->callbacks[endpoint_a].empty ()) { lk.unlock (); @@ -429,7 +429,7 @@ void nano::telemetry::invoke_callbacks (nano::endpoint const & endpoint_a, bool telemetry_data_response response_data{ nano::telemetry_data (), endpoint_a, error_a }; { // Copy data so that it can be used outside of holding the lock - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); callbacks_l = callbacks[endpoint_a]; auto it = recent_or_initial_request_telemetry_data.find (endpoint_a); @@ -449,7 +449,7 @@ void nano::telemetry::invoke_callbacks (nano::endpoint const & endpoint_a, bool size_t nano::telemetry::telemetry_data_size () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return recent_or_initial_request_telemetry_data.size (); } @@ -471,7 +471,7 @@ std::unique_ptr nano::collect_container_info (te auto composite = std::make_unique (name); size_t callbacks_count; { - nano::lock_guard guard (telemetry.mutex); + nano::lock_guard guard (telemetry.mutex); std::unordered_map>> callbacks; callbacks_count = std::accumulate (callbacks.begin (), callbacks.end (), static_cast (0), [](auto total, auto const & callback_a) { return total += callback_a.second.size (); diff --git a/nano/node/telemetry.hpp b/nano/node/telemetry.hpp index 937929b6a7..c90809f36a 100644 --- a/nano/node/telemetry.hpp +++ b/nano/node/telemetry.hpp @@ -113,7 +113,7 @@ class telemetry : public std::enable_shared_from_this std::atomic stopped{ false }; - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::telemetry) }; // clang-format off // This holds the last telemetry data received from peers or can be a placeholder awaiting the first response (check with awaiting_first_response ()) boost::multi_index_container lk (channel_mutex); + nano::lock_guard lk (channel_mutex); // Close socket. Exception: socket is used by bootstrap_server if (auto socket_l = socket.lock ()) { @@ -101,7 +101,7 @@ std::string nano::transport::channel_tcp::to_string () const void nano::transport::channel_tcp::set_endpoint () { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); debug_assert (endpoint == nano::tcp_endpoint (boost::asio::ip::address_v6::any (), 0)); // Not initialized endpoint value // Calculate TCP socket endpoint if (auto socket_l = socket.lock ()) @@ -123,7 +123,7 @@ bool nano::transport::tcp_channels::insert (std::shared_ptr lock (mutex); + nano::unique_lock lock (mutex); auto existing (channels.get ().find (endpoint)); if (existing == channels.get ().end ()) { @@ -148,19 +148,19 @@ bool nano::transport::tcp_channels::insert (std::shared_ptr lock (mutex); + nano::lock_guard lock (mutex); channels.get ().erase (endpoint_a); } size_t nano::transport::tcp_channels::size () const { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return channels.size (); } std::shared_ptr nano::transport::tcp_channels::find_channel (nano::tcp_endpoint const & endpoint_a) const { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); std::shared_ptr result; auto existing (channels.get ().find (endpoint_a)); if (existing != channels.get ().end ()) @@ -174,7 +174,7 @@ std::unordered_set> nano::transport::t { std::unordered_set> result; result.reserve (count_a); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); // Stop trying to fill result with random samples after this many attempts auto random_cutoff (count_a * 2); auto peers_size (channels.size ()); @@ -218,7 +218,7 @@ bool nano::transport::tcp_channels::store_all (bool clear_peers) // we collect endpoints to be saved and then relase the lock. std::vector endpoints; { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); endpoints.reserve (channels.size ()); std::transform (channels.begin (), channels.end (), std::back_inserter (endpoints), [](const auto & channel) { return nano::transport::map_tcp_to_endpoint (channel.endpoint ()); }); @@ -245,7 +245,7 @@ bool nano::transport::tcp_channels::store_all (bool clear_peers) std::shared_ptr nano::transport::tcp_channels::find_node_id (nano::account const & node_id_a) { std::shared_ptr result; - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto existing (channels.get ().find (node_id_a)); if (existing != channels.get ().end ()) { @@ -257,7 +257,7 @@ std::shared_ptr nano::transport::tcp_channels::fin nano::tcp_endpoint nano::transport::tcp_channels::bootstrap_peer (uint8_t connection_protocol_version_min) { nano::tcp_endpoint result (boost::asio::ip::address_v6::any (), 0); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); for (auto i (channels.get ().begin ()), n (channels.get ().end ()); i != n;) { if (i->channel->get_network_version () >= connection_protocol_version_min) @@ -347,7 +347,7 @@ void nano::transport::tcp_channels::start () void nano::transport::tcp_channels::stop () { stopped = true; - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); // Close all TCP sockets for (auto i (channels.begin ()), j (channels.end ()); i != j; ++i) { @@ -370,7 +370,7 @@ bool nano::transport::tcp_channels::max_ip_connections (nano::tcp_endpoint const bool result (false); if (!node.flags.disable_max_peers_per_ip) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); result = channels.get ().count (endpoint_a.address ()) >= node.network_params.node.max_peers_per_ip; if (!result) { @@ -389,7 +389,7 @@ bool nano::transport::tcp_channels::reachout (nano::endpoint const & endpoint_a) { // Don't keepalive to nodes that already sent us something error |= find_channel (tcp_endpoint) != nullptr; - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto inserted (attempts.emplace (tcp_endpoint)); error |= !inserted.second; } @@ -402,7 +402,7 @@ std::unique_ptr nano::transport::tcp_channels::c size_t attemps_count; size_t node_id_handshake_sockets_count; { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); channels_count = channels.size (); attemps_count = attempts.size (); node_id_handshake_sockets_count = node_id_handshake_sockets.size (); @@ -418,7 +418,7 @@ std::unique_ptr nano::transport::tcp_channels::c void nano::transport::tcp_channels::purge (std::chrono::steady_clock::time_point const & cutoff_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto disconnect_cutoff (channels.get ().lower_bound (cutoff_a)); channels.get ().erase (channels.get ().begin (), disconnect_cutoff); // Remove keepalive attempt tracking for attempts older than cutoff @@ -440,7 +440,7 @@ void nano::transport::tcp_channels::ongoing_keepalive () { nano::keepalive message; node.network.random_fill (message.peers); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); // Wake up channels std::vector> send_list; auto keepalive_sent_cutoff (channels.get ().lower_bound (std::chrono::steady_clock::now () - node.network_params.node.period)); @@ -481,7 +481,7 @@ void nano::transport::tcp_channels::ongoing_keepalive () void nano::transport::tcp_channels::list_below_version (std::vector> & channels_a, uint8_t cutoff_version_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); // clang-format off nano::transform_if (channels.get ().begin (), channels.get ().end (), std::back_inserter (channels_a), [cutoff_version_a](auto & channel_a) { return channel_a.channel->get_network_version () < cutoff_version_a; }, @@ -491,7 +491,7 @@ void nano::transport::tcp_channels::list_below_version (std::vector> & deque_a, uint8_t minimum_version_a, bool include_temporary_channels_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); // clang-format off nano::transform_if (channels.get ().begin (), channels.get ().end (), std::back_inserter (deque_a), [include_temporary_channels_a, minimum_version_a](auto & channel_a) { return channel_a.channel->get_network_version () >= minimum_version_a && (include_temporary_channels_a || !channel_a.channel->temporary); }, @@ -501,7 +501,7 @@ void nano::transport::tcp_channels::list (std::deque const & channel_a, std::function const &)> modify_callback_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto existing (channels.get ().find (channel_a->get_tcp_endpoint ())); if (existing != channels.get ().end ()) { @@ -513,7 +513,7 @@ void nano::transport::tcp_channels::modify (std::shared_ptr lock (mutex); + nano::lock_guard lock (mutex); auto existing (channels.get ().find (endpoint_a)); if (existing != channels.get ().end ()) { @@ -525,13 +525,13 @@ void nano::transport::tcp_channels::update (nano::tcp_endpoint const & endpoint_ bool nano::transport::tcp_channels::node_id_handhake_sockets_empty () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return node_id_handshake_sockets.empty (); } void nano::transport::tcp_channels::push_node_id_handshake_socket (std::shared_ptr const & socket_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); node_id_handshake_sockets.push_back (socket_a); } @@ -540,7 +540,7 @@ void nano::transport::tcp_channels::remove_node_id_handshake_socket (std::shared std::weak_ptr node_w (node.shared ()); if (auto node_l = node_w.lock ()) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); node_id_handshake_sockets.erase (std::remove (node_id_handshake_sockets.begin (), node_id_handshake_sockets.end (), socket_a), node_id_handshake_sockets.end ()); } } @@ -720,7 +720,7 @@ void nano::transport::tcp_channels::start_tcp_receive_node_id (std::shared_ptr lock (node_l->network.tcp_channels.mutex); + nano::lock_guard lock (node_l->network.tcp_channels.mutex); node_l->network.tcp_channels.attempts.get ().erase (nano::transport::map_endpoint_to_tcp (endpoint_a)); } } @@ -746,7 +746,7 @@ void nano::transport::tcp_channels::start_tcp_receive_node_id (std::shared_ptr const &)> const & callback_a) { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); attempts.get ().erase (nano::transport::map_endpoint_to_tcp (endpoint_a)); } if (callback_a && !node.flags.disable_udp) diff --git a/nano/node/transport/tcp.hpp b/nano/node/transport/tcp.hpp index 894902dbc6..02318db65d 100644 --- a/nano/node/transport/tcp.hpp +++ b/nano/node/transport/tcp.hpp @@ -60,7 +60,7 @@ namespace transport nano::tcp_endpoint get_tcp_endpoint () const override { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); return endpoint; } @@ -189,7 +189,7 @@ namespace transport { } }; - mutable std::mutex mutex; + mutable nano::mutex mutex; // clang-format off boost::multi_index_container lk (channel_mutex); + nano::lock_guard lk (channel_mutex); return last_bootstrap_attempt; } void set_last_bootstrap_attempt (std::chrono::steady_clock::time_point const time_a) { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); last_bootstrap_attempt = time_a; } std::chrono::steady_clock::time_point get_last_packet_received () const { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); return last_packet_received; } void set_last_packet_received (std::chrono::steady_clock::time_point const time_a) { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); last_packet_received = time_a; } std::chrono::steady_clock::time_point get_last_packet_sent () const { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); return last_packet_sent; } void set_last_packet_sent (std::chrono::steady_clock::time_point const time_a) { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); last_packet_sent = time_a; } boost::optional get_node_id_optional () const { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); return node_id; } nano::account get_node_id () const { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); if (node_id.is_initialized ()) { return node_id.get (); @@ -106,7 +106,7 @@ namespace transport void set_node_id (nano::account node_id_a) { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); node_id = node_id_a; } @@ -120,7 +120,7 @@ namespace transport network_version = network_version_a; } - mutable std::mutex channel_mutex; + mutable nano::mutex channel_mutex; private: std::chrono::steady_clock::time_point last_bootstrap_attempt{ std::chrono::steady_clock::time_point () }; diff --git a/nano/node/transport/udp.cpp b/nano/node/transport/udp.cpp index 560ab9cb01..c2aa0ff6f9 100644 --- a/nano/node/transport/udp.cpp +++ b/nano/node/transport/udp.cpp @@ -101,7 +101,7 @@ std::shared_ptr nano::transport::udp_channels::ins std::shared_ptr result; if (!node.network.not_a_peer (endpoint_a, node.config.allow_local_peers) && (node.network_params.network.is_dev_network () || !max_ip_connections (endpoint_a))) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto existing (channels.get ().find (endpoint_a)); if (existing != channels.get ().end ()) { @@ -121,19 +121,19 @@ std::shared_ptr nano::transport::udp_channels::ins void nano::transport::udp_channels::erase (nano::endpoint const & endpoint_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); channels.get ().erase (endpoint_a); } size_t nano::transport::udp_channels::size () const { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return channels.size (); } std::shared_ptr nano::transport::udp_channels::channel (nano::endpoint const & endpoint_a) const { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); std::shared_ptr result; auto existing (channels.get ().find (endpoint_a)); if (existing != channels.get ().end ()) @@ -147,7 +147,7 @@ std::unordered_set> nano::transport::u { std::unordered_set> result; result.reserve (count_a); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); // Stop trying to fill result with random samples after this many attempts auto random_cutoff (count_a * 2); auto peers_size (channels.size ()); @@ -190,7 +190,7 @@ bool nano::transport::udp_channels::store_all (bool clear_peers) // we collect endpoints to be saved and then relase the lock. std::vector endpoints; { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); endpoints.reserve (channels.size ()); std::transform (channels.begin (), channels.end (), std::back_inserter (endpoints), [](const auto & channel) { return channel.endpoint (); }); @@ -217,7 +217,7 @@ bool nano::transport::udp_channels::store_all (bool clear_peers) std::shared_ptr nano::transport::udp_channels::find_node_id (nano::account const & node_id_a) { std::shared_ptr result; - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto existing (channels.get ().find (node_id_a)); if (existing != channels.get ().end ()) { @@ -228,13 +228,13 @@ std::shared_ptr nano::transport::udp_channels::fin void nano::transport::udp_channels::clean_node_id (nano::account const & node_id_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); channels.get ().erase (node_id_a); } void nano::transport::udp_channels::clean_node_id (nano::endpoint const & endpoint_a, nano::account const & node_id_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto existing (channels.get ().equal_range (node_id_a)); for (auto & record : boost::make_iterator_range (existing)) { @@ -250,7 +250,7 @@ void nano::transport::udp_channels::clean_node_id (nano::endpoint const & endpoi nano::tcp_endpoint nano::transport::udp_channels::bootstrap_peer (uint8_t connection_protocol_version_min) { nano::tcp_endpoint result (boost::asio::ip::address_v6::any (), 0); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); for (auto i (channels.get ().begin ()), n (channels.get ().end ()); i != n;) { if (i->channel->get_network_version () >= connection_protocol_version_min) @@ -326,7 +326,7 @@ void nano::transport::udp_channels::stop () // Stop and invalidate local endpoint if (!stopped.exchange (true)) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); local_endpoint = nano::endpoint (boost::asio::ip::address_v6::loopback (), 0); // On devnet, close directly to avoid address-reuse issues. On livenet, close @@ -355,7 +355,7 @@ void nano::transport::udp_channels::close_socket () nano::endpoint nano::transport::udp_channels::get_local_endpoint () const { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); return local_endpoint; } @@ -630,7 +630,7 @@ bool nano::transport::udp_channels::max_ip_connections (nano::endpoint const & e bool result (false); if (!node.flags.disable_max_peers_per_ip) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); result = channels.get ().count (endpoint_a.address ()) >= node.network_params.node.max_peers_per_ip; } return result; @@ -645,7 +645,7 @@ bool nano::transport::udp_channels::reachout (nano::endpoint const & endpoint_a) auto endpoint_l (nano::transport::map_endpoint_to_v6 (endpoint_a)); // Don't keepalive to nodes that already sent us something error |= channel (endpoint_l) != nullptr; - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto inserted (attempts.emplace (endpoint_l)); error |= !inserted.second; } @@ -657,7 +657,7 @@ std::unique_ptr nano::transport::udp_channels::c size_t channels_count; size_t attemps_count; { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); channels_count = channels.size (); attemps_count = attempts.size (); } @@ -671,7 +671,7 @@ std::unique_ptr nano::transport::udp_channels::c void nano::transport::udp_channels::purge (std::chrono::steady_clock::time_point const & cutoff_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto disconnect_cutoff (channels.get ().lower_bound (cutoff_a)); channels.get ().erase (channels.get ().begin (), disconnect_cutoff); // Remove keepalive attempt tracking for attempts older than cutoff @@ -684,7 +684,7 @@ void nano::transport::udp_channels::ongoing_keepalive () nano::keepalive message; node.network.random_fill (message.peers); std::vector> send_list; - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto keepalive_cutoff (channels.get ().lower_bound (std::chrono::steady_clock::now () - node.network_params.node.period)); for (auto i (channels.get ().begin ()); i != keepalive_cutoff; ++i) { @@ -706,7 +706,7 @@ void nano::transport::udp_channels::ongoing_keepalive () void nano::transport::udp_channels::list_below_version (std::vector> & channels_a, uint8_t cutoff_version_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); // clang-format off nano::transform_if (channels.get ().begin (), channels.get ().end (), std::back_inserter (channels_a), [cutoff_version_a](auto & channel_a) { return channel_a.channel->get_network_version () < cutoff_version_a; }, @@ -716,7 +716,7 @@ void nano::transport::udp_channels::list_below_version (std::vector> & deque_a, uint8_t minimum_version_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); // clang-format off nano::transform_if (channels.get ().begin (), channels.get ().end (), std::back_inserter (deque_a), [minimum_version_a](auto & channel_a) { return channel_a.channel->get_network_version () >= minimum_version_a; }, @@ -726,7 +726,7 @@ void nano::transport::udp_channels::list (std::deque const & channel_a, std::function const &)> modify_callback_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto existing (channels.get ().find (channel_a->endpoint)); if (existing != channels.get ().end ()) { diff --git a/nano/node/transport/udp.hpp b/nano/node/transport/udp.hpp index dfa09b1d43..538744ad71 100644 --- a/nano/node/transport/udp.hpp +++ b/nano/node/transport/udp.hpp @@ -38,13 +38,13 @@ namespace transport nano::endpoint get_endpoint () const override { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); return endpoint; } nano::tcp_endpoint get_tcp_endpoint () const override { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); return nano::transport::map_endpoint_to_tcp (endpoint); } @@ -55,13 +55,13 @@ namespace transport std::chrono::steady_clock::time_point get_last_telemetry_req () { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); return last_telemetry_req; } void set_last_telemetry_req (std::chrono::steady_clock::time_point const time_a) { - nano::lock_guard lk (channel_mutex); + nano::lock_guard lk (channel_mutex); last_telemetry_req = time_a; } @@ -174,7 +174,7 @@ namespace transport { } }; - mutable std::mutex mutex; + mutable nano::mutex mutex; // clang-format off boost::multi_index_container< channel_udp_wrapper, diff --git a/nano/node/vote_processor.cpp b/nano/node/vote_processor.cpp index 99bbe2f251..49af33fd2d 100644 --- a/nano/node/vote_processor.cpp +++ b/nano/node/vote_processor.cpp @@ -35,7 +35,7 @@ thread ([this]() { process_loop (); }) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); condition.wait (lock, [& started = started] { return started; }); } @@ -44,7 +44,7 @@ void nano::vote_processor::process_loop () nano::timer elapsed; bool log_this_iteration; - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); started = true; lock.unlock (); @@ -94,7 +94,7 @@ bool nano::vote_processor::vote (std::shared_ptr const & vote_a, std { debug_assert (channel_a != nullptr); bool process (false); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (!stopped) { // Level 0 (< 0.1%) @@ -205,7 +205,7 @@ nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr void nano::vote_processor::stop () { { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); stopped = true; } condition.notify_all (); @@ -217,7 +217,7 @@ void nano::vote_processor::stop () void nano::vote_processor::flush () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (is_active || !votes.empty ()) { condition.wait (lock); @@ -226,7 +226,7 @@ void nano::vote_processor::flush () void nano::vote_processor::flush_active () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); while (is_active) { condition.wait (lock); @@ -235,13 +235,13 @@ void nano::vote_processor::flush_active () size_t nano::vote_processor::size () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return votes.size (); } bool nano::vote_processor::empty () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return votes.empty (); } @@ -252,7 +252,7 @@ bool nano::vote_processor::half_full () void nano::vote_processor::calculate_weights () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (!stopped) { representatives_1.clear (); @@ -288,7 +288,7 @@ std::unique_ptr nano::collect_container_info (vo size_t representatives_3_count; { - nano::lock_guard guard (vote_processor.mutex); + nano::lock_guard guard (vote_processor.mutex); votes_count = vote_processor.votes.size (); representatives_1_count = vote_processor.representatives_1.size (); representatives_2_count = vote_processor.representatives_2.size (); diff --git a/nano/node/vote_processor.hpp b/nano/node/vote_processor.hpp index 3bb1ed24bb..84341d33fc 100644 --- a/nano/node/vote_processor.hpp +++ b/nano/node/vote_processor.hpp @@ -69,7 +69,7 @@ class vote_processor final std::unordered_set representatives_2; std::unordered_set representatives_3; nano::condition_variable condition; - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::vote_processor) }; bool started; bool stopped; bool is_active; diff --git a/nano/node/voting.cpp b/nano/node/voting.cpp index 910cc9b3fc..a737eb1680 100644 --- a/nano/node/voting.cpp +++ b/nano/node/voting.cpp @@ -34,7 +34,7 @@ void nano::vote_spacing::flag (nano::root const & root_a, nano::block_hash const auto existing = recent.get ().find (root_a); if (existing != recent.end ()) { - recent.get ().modify (existing, [now] (entry & entry) { + recent.get ().modify (existing, [now](entry & entry) { entry.time = now; }); } @@ -65,7 +65,7 @@ bool nano::local_vote_history::consistency_check (nano::root const & root_a) con void nano::local_vote_history::add (nano::root const & root_a, nano::block_hash const & hash_a, std::shared_ptr const & vote_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); clean (); auto & history_by_root (history.get ()); // Erase any vote that is not for this hash, or duplicate by account @@ -89,7 +89,7 @@ void nano::local_vote_history::add (nano::root const & root_a, nano::block_hash void nano::local_vote_history::erase (nano::root const & root_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto & history_by_root (history.get ()); auto range (history_by_root.equal_range (root_a)); history_by_root.erase (range.first, range.second); @@ -97,7 +97,7 @@ void nano::local_vote_history::erase (nano::root const & root_a) std::vector> nano::local_vote_history::votes (nano::root const & root_a) const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); std::vector> result; auto range (history.get ().equal_range (root_a)); std::transform (range.first, range.second, std::back_inserter (result), [](auto const & entry) { return entry.vote; }); @@ -106,7 +106,7 @@ std::vector> nano::local_vote_history::votes (nano:: std::vector> nano::local_vote_history::votes (nano::root const & root_a, nano::block_hash const & hash_a) const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); std::vector> result; auto range (history.get ().equal_range (root_a)); // clang-format off @@ -119,7 +119,7 @@ std::vector> nano::local_vote_history::votes (nano:: bool nano::local_vote_history::exists (nano::root const & root_a) const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return history.get ().find (root_a) != history.get ().end (); } @@ -135,7 +135,7 @@ void nano::local_vote_history::clean () size_t nano::local_vote_history::size () const { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return history.size (); } @@ -160,7 +160,7 @@ network (network_a), stats (stats_a), thread ([this]() { run (); }) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); condition.wait (lock, [& started = started] { return started; }); } @@ -180,7 +180,7 @@ void nano::vote_generator::add (nano::root const & root_a, nano::block_hash cons auto block (ledger.store.block_get (transaction, hash_a)); if (block != nullptr && ledger.dependents_confirmed (transaction, *block)) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); candidates.emplace_back (root_a, hash_a); if (candidates.size () >= nano::network::confirm_ack_hashes_max) { @@ -193,7 +193,7 @@ void nano::vote_generator::add (nano::root const & root_a, nano::block_hash cons void nano::vote_generator::stop () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); stopped = true; lock.unlock (); @@ -219,7 +219,7 @@ size_t nano::vote_generator::generate (std::vector> nano::transform_if (blocks_a.begin (), blocks_a.end (), std::back_inserter (candidates), dependents_confirmed, as_candidate); } auto const result = candidates.size (); - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); requests.emplace_back (std::move (candidates), channel_a); while (requests.size () > max_requests) { @@ -236,7 +236,7 @@ void nano::vote_generator::set_reply_action (std::function & lock_a) +void nano::vote_generator::broadcast (nano::unique_lock & lock_a) { debug_assert (lock_a.owns_lock ()); std::unordered_set> cached_sent; @@ -280,7 +280,7 @@ void nano::vote_generator::broadcast (nano::unique_lock & lock_a) } } -void nano::vote_generator::reply (nano::unique_lock & lock_a, request_t && request_a) +void nano::vote_generator::reply (nano::unique_lock & lock_a, request_t && request_a) { lock_a.unlock (); std::unordered_set> cached_sent; @@ -360,7 +360,7 @@ void nano::vote_generator::broadcast_action (std::shared_ptr const & void nano::vote_generator::run () { nano::thread_role::set (nano::thread_role::name::voting); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); started = true; lock.unlock (); condition.notify_all (); @@ -417,7 +417,7 @@ std::unique_ptr nano::collect_container_info (na size_t candidates_count = 0; size_t requests_count = 0; { - nano::lock_guard guard (vote_generator.mutex); + nano::lock_guard guard (vote_generator.mutex); candidates_count = vote_generator.candidates.size (); requests_count = vote_generator.requests.size (); } diff --git a/nano/node/voting.hpp b/nano/node/voting.hpp index d220daa486..074b26ab69 100644 --- a/nano/node/voting.hpp +++ b/nano/node/voting.hpp @@ -103,7 +103,7 @@ class local_vote_history final std::vector> votes (nano::root const & root_a) const; // Only used in Debug bool consistency_check (nano::root const &) const; - mutable std::mutex mutex; + mutable nano::mutex mutex; friend std::unique_ptr collect_container_info (local_vote_history & history, std::string const & name); friend class local_vote_history_basic_Test; @@ -128,8 +128,8 @@ class vote_generator final private: void run (); - void broadcast (nano::unique_lock &); - void reply (nano::unique_lock &, request_t &&); + void broadcast (nano::unique_lock &); + void reply (nano::unique_lock &, request_t &&); void vote (std::vector const &, std::vector const &, std::function const &)> const &); void broadcast_action (std::shared_ptr const &) const; std::function const &, std::shared_ptr &)> reply_action; // must be set only during initialization by using set_reply_action @@ -141,7 +141,7 @@ class vote_generator final nano::vote_spacing spacing; nano::network & network; nano::stat & stats; - mutable std::mutex mutex; + mutable nano::mutex mutex; nano::condition_variable condition; static size_t constexpr max_requests{ 2048 }; std::deque requests; diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 484488c714..d4e6d14dd0 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -214,7 +214,7 @@ nano::fan::fan (nano::uint256_union const & key, size_t count_a) void nano::fan::value (nano::raw_key & prv_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); value_get (prv_a); } @@ -230,7 +230,7 @@ void nano::fan::value_get (nano::raw_key & prv_a) void nano::fan::value_set (nano::raw_key const & value_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); nano::raw_key value_l; value_get (value_l); *(values[0]) ^= value_l.data; @@ -648,7 +648,7 @@ void nano::wallet_store::version_put (nano::transaction const & transaction_a, u void nano::kdf::phs (nano::raw_key & result_a, std::string const & password_a, nano::uint256_union const & salt_a) { static nano::network_params network_params; - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto success (argon2_hash (1, network_params.kdf_work, 1, password_a.data (), password_a.size (), salt_a.bytes.data (), salt_a.bytes.size (), result_a.data.bytes.data (), result_a.data.bytes.size (), NULL, 0, Argon2_d, 0x10)); debug_assert (success == 0); (void)success; @@ -722,7 +722,7 @@ nano::public_key nano::wallet::deterministic_insert (nano::transaction const & t auto half_principal_weight (wallets.node.minimum_principal_weight () / 2); if (wallets.check_rep (key, half_principal_weight)) { - nano::lock_guard lock (representatives_mutex); + nano::lock_guard lock (representatives_mutex); representatives.insert (key); } } @@ -769,7 +769,7 @@ nano::public_key nano::wallet::insert_adhoc (nano::raw_key const & key_a, bool g transaction.commit (); if (wallets.check_rep (key, half_principal_weight)) { - nano::lock_guard lock (representatives_mutex); + nano::lock_guard lock (representatives_mutex); representatives.insert (key); } } @@ -1331,7 +1331,7 @@ nano::work_watcher::~work_watcher () void nano::work_watcher::stop () { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); watched.clear (); stopped = true; } @@ -1342,7 +1342,7 @@ void nano::work_watcher::add (std::shared_ptr const & block_a) if (!stopped && block_l != nullptr) { auto root_l (block_l->qualified_root ()); - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); watched[root_l] = block_l; lock.unlock (); watching (root_l, block_l); @@ -1351,7 +1351,7 @@ void nano::work_watcher::add (std::shared_ptr const & block_a) void nano::work_watcher::update (nano::qualified_root const & root_a, std::shared_ptr const & block_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); watched[root_a] = block_a; } @@ -1401,7 +1401,7 @@ void nano::work_watcher::watching (nano::qualified_root const & root_a, std::sha void nano::work_watcher::remove (nano::block const & block_a) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); auto existing (watched.find (block_a.qualified_root ())); if (existing != watched.end ()) { @@ -1413,26 +1413,26 @@ void nano::work_watcher::remove (nano::block const & block_a) bool nano::work_watcher::is_watched (nano::qualified_root const & root_a) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); auto exists (watched.find (root_a)); return exists != watched.end (); } auto nano::work_watcher::list_watched () -> decltype (watched) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return watched; } size_t nano::work_watcher::size () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return watched.size (); } void nano::wallets::do_wallet_actions () { - nano::unique_lock action_lock (action_mutex); + nano::unique_lock action_lock (action_mutex); while (!stopped) { if (!actions.empty ()) @@ -1468,7 +1468,7 @@ thread ([this]() { do_wallet_actions (); }) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); if (!error_a) { auto transaction (tx_begin_write ()); @@ -1537,7 +1537,7 @@ nano::wallets::~wallets () std::shared_ptr nano::wallets::open (nano::wallet_id const & id_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); std::shared_ptr result; auto existing (items.find (id_a)); if (existing != items.end ()) @@ -1549,7 +1549,7 @@ std::shared_ptr nano::wallets::open (nano::wallet_id const & id_a) std::shared_ptr nano::wallets::create (nano::wallet_id const & id_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); debug_assert (items.find (id_a) == items.end ()); std::shared_ptr result; bool error; @@ -1577,7 +1577,7 @@ bool nano::wallets::search_pending (nano::wallet_id const & wallet_a) void nano::wallets::search_pending_all () { - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); auto wallets_l = get_wallets (); auto wallet_transaction (tx_begin_read ()); lk.unlock (); @@ -1589,10 +1589,10 @@ void nano::wallets::search_pending_all () void nano::wallets::destroy (nano::wallet_id const & id_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto transaction (tx_begin_write ()); // action_mutex should be after transactions to prevent deadlocks in deterministic_insert () & insert_adhoc () - nano::lock_guard action_lock (action_mutex); + nano::lock_guard action_lock (action_mutex); auto existing (items.find (id_a)); debug_assert (existing != items.end ()); auto wallet (existing->second); @@ -1602,7 +1602,7 @@ void nano::wallets::destroy (nano::wallet_id const & id_a) void nano::wallets::reload () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto transaction (tx_begin_write ()); std::unordered_set stored_items; std::string beginning (nano::uint256_union (0).to_string ()); @@ -1646,7 +1646,7 @@ void nano::wallets::reload () void nano::wallets::queue_wallet_action (nano::uint128_t const & amount_a, std::shared_ptr const & wallet_a, std::function action_a) { { - nano::lock_guard action_lock (action_mutex); + nano::lock_guard action_lock (action_mutex); actions.emplace (amount_a, std::make_pair (wallet_a, action_a)); } condition.notify_all (); @@ -1659,14 +1659,14 @@ void nano::wallets::foreach_representative (std::function> action_accounts_l; { auto transaction_l (tx_begin_read ()); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); for (auto i (items.begin ()), n (items.end ()); i != n; ++i) { auto & wallet (*i->second); nano::lock_guard store_lock (wallet.store.mutex); decltype (wallet.representatives) representatives_l; { - nano::lock_guard representatives_lock (wallet.representatives_mutex); + nano::lock_guard representatives_lock (wallet.representatives_mutex); representatives_l = wallet.representatives; } for (auto const & account : representatives_l) @@ -1706,7 +1706,7 @@ void nano::wallets::foreach_representative (std::function lock (mutex); + nano::lock_guard lock (mutex); auto result (false); for (auto i (items.begin ()), n (items.end ()); !result && i != n; ++i) { @@ -1718,7 +1718,7 @@ bool nano::wallets::exists (nano::transaction const & transaction_a, nano::accou void nano::wallets::stop () { { - nano::lock_guard action_lock (action_mutex); + nano::lock_guard action_lock (action_mutex); stopped = true; actions.clear (); } @@ -1749,7 +1749,7 @@ void nano::wallets::clear_send_ids (nano::transaction const & transaction_a) nano::wallet_representatives nano::wallets::reps () const { - nano::lock_guard counts_guard (reps_cache_mutex); + nano::lock_guard counts_guard (reps_cache_mutex); return representatives; } @@ -1759,10 +1759,10 @@ bool nano::wallets::check_rep (nano::account const & account_a, nano::uint128_t auto weight (node.ledger.weight (account_a)); if (weight >= node.config.vote_minimum.number ()) { - nano::unique_lock lock; + nano::unique_lock lock; if (acquire_lock_a) { - lock = nano::unique_lock (reps_cache_mutex); + lock = nano::unique_lock (reps_cache_mutex); } result = true; representatives.accounts.insert (account_a); @@ -1777,8 +1777,8 @@ bool nano::wallets::check_rep (nano::account const & account_a, nano::uint128_t void nano::wallets::compute_reps () { - nano::lock_guard guard (mutex); - nano::lock_guard counts_guard (reps_cache_mutex); + nano::lock_guard guard (mutex); + nano::lock_guard counts_guard (reps_cache_mutex); representatives.clear (); auto half_principal_weight (node.minimum_principal_weight () / 2); auto transaction (tx_begin_read ()); @@ -1794,7 +1794,7 @@ void nano::wallets::compute_reps () representatives_l.insert (account); } } - nano::lock_guard representatives_guard (wallet.representatives_mutex); + nano::lock_guard representatives_guard (wallet.representatives_mutex); wallet.representatives.swap (representatives_l); } } @@ -1950,7 +1950,7 @@ std::unique_ptr nano::collect_container_info (wa size_t items_count; size_t actions_count; { - nano::lock_guard guard (wallets.mutex); + nano::lock_guard guard (wallets.mutex); items_count = wallets.items.size (); actions_count = wallets.actions.size (); } diff --git a/nano/node/wallet.hpp b/nano/node/wallet.hpp index deaf357d8a..248f1f0f05 100644 --- a/nano/node/wallet.hpp +++ b/nano/node/wallet.hpp @@ -28,14 +28,14 @@ class fan final std::vector> values; private: - std::mutex mutex; + nano::mutex mutex; void value_get (nano::raw_key &); }; class kdf final { public: void phs (nano::raw_key &, std::string const &, nano::uint256_union const &); - std::mutex mutex; + nano::mutex mutex; }; enum class key_type { @@ -157,7 +157,7 @@ class wallet final : public std::enable_shared_from_this std::function lock_observer; nano::wallet_store store; nano::wallets & wallets; - std::mutex representatives_mutex; + nano::mutex representatives_mutex; std::unordered_set representatives; }; @@ -178,7 +178,7 @@ class work_watcher final : public std::enable_shared_from_this stopped; }; @@ -238,8 +238,8 @@ class wallets final std::unordered_map> items; std::multimap, std::function>, std::greater> actions; nano::locked> delayed_work; - std::mutex mutex; - std::mutex action_mutex; + nano::mutex mutex; + nano::mutex action_mutex; nano::condition_variable condition; nano::kdf kdf; MDB_dbi handle; @@ -258,7 +258,7 @@ class wallets final nano::read_transaction tx_begin_read (); private: - mutable std::mutex reps_cache_mutex; + mutable nano::mutex reps_cache_mutex; nano::wallet_representatives representatives; }; diff --git a/nano/node/websocket.cpp b/nano/node/websocket.cpp index f7cf33f251..7ed84812b6 100644 --- a/nano/node/websocket.cpp +++ b/nano/node/websocket.cpp @@ -241,7 +241,7 @@ ws_listener (listener_a), ws (std::move (socket_a)), strand (ws.get_executor ()) nano::websocket::session::~session () { { - nano::unique_lock lk (subscriptions_mutex); + nano::unique_lock lk (subscriptions_mutex); for (auto & subscription : subscriptions) { ws_listener.decrease_subscriber_count (subscription.first); @@ -282,7 +282,7 @@ void nano::websocket::session::close () void nano::websocket::session::write (nano::websocket::message message_a) { - nano::unique_lock lk (subscriptions_mutex); + nano::unique_lock lk (subscriptions_mutex); auto subscription (subscriptions.find (message_a.topic)); if (message_a.topic == nano::websocket::topic::ack || (subscription != subscriptions.end () && !subscription->second->should_filter (message_a))) { @@ -468,7 +468,7 @@ void nano::websocket::session::handle_message (boost::property_tree::ptree const if (action == "subscribe" && topic_l != nano::websocket::topic::invalid) { auto options_text_l (message_a.get_child_optional ("options")); - nano::lock_guard lk (subscriptions_mutex); + nano::lock_guard lk (subscriptions_mutex); std::unique_ptr options_l{ nullptr }; if (options_text_l && topic_l == nano::websocket::topic::confirmation) { @@ -498,7 +498,7 @@ void nano::websocket::session::handle_message (boost::property_tree::ptree const } else if (action == "update") { - nano::lock_guard lk (subscriptions_mutex); + nano::lock_guard lk (subscriptions_mutex); auto existing (subscriptions.find (topic_l)); if (existing != subscriptions.end ()) { @@ -511,7 +511,7 @@ void nano::websocket::session::handle_message (boost::property_tree::ptree const } else if (action == "unsubscribe" && topic_l != nano::websocket::topic::invalid) { - nano::lock_guard lk (subscriptions_mutex); + nano::lock_guard lk (subscriptions_mutex); if (subscriptions.erase (topic_l)) { ws_listener.get_logger ().always_log ("Websocket: removed subscription to topic: ", from_topic (topic_l)); @@ -536,7 +536,7 @@ void nano::websocket::listener::stop () stopped = true; acceptor.close (); - nano::lock_guard lk (sessions_mutex); + nano::lock_guard lk (sessions_mutex); for (auto & weak_session : sessions) { auto session_ptr (weak_session.lock ()); @@ -616,7 +616,7 @@ void nano::websocket::listener::broadcast_confirmation (std::shared_ptr lk (sessions_mutex); + nano::lock_guard lk (sessions_mutex); boost::optional msg_with_block; boost::optional msg_without_block; for (auto & weak_session : sessions) @@ -656,7 +656,7 @@ void nano::websocket::listener::broadcast_confirmation (std::shared_ptr lk (sessions_mutex); + nano::lock_guard lk (sessions_mutex); for (auto & weak_session : sessions) { auto session_ptr (weak_session.lock ()); diff --git a/nano/node/websocket.hpp b/nano/node/websocket.hpp index ab78a6522f..cb48a6cd71 100644 --- a/nano/node/websocket.hpp +++ b/nano/node/websocket.hpp @@ -267,7 +267,7 @@ namespace websocket }; /** Map of subscriptions -> options registered by this session. */ std::unordered_map, topic_hash> subscriptions; - std::mutex subscriptions_mutex; + nano::mutex subscriptions_mutex; /** Handle incoming message */ void handle_message (boost::property_tree::ptree const & message_a); @@ -334,7 +334,7 @@ namespace websocket nano::wallets & wallets; boost::asio::ip::tcp::acceptor acceptor; socket_type socket; - std::mutex sessions_mutex; + nano::mutex sessions_mutex; std::vector> sessions; std::array, number_topics> topic_subscriber_count; std::atomic stopped{ false }; diff --git a/nano/node/write_database_queue.cpp b/nano/node/write_database_queue.cpp index 8d8e96ba49..118c2493f6 100644 --- a/nano/node/write_database_queue.cpp +++ b/nano/node/write_database_queue.cpp @@ -55,7 +55,7 @@ guard_finish_callback ([use_noops_a, &queue = queue, &mutex = mutex, &cv = cv]() if (!use_noops_a) { { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); queue.pop_front (); } cv.notify_all (); @@ -72,7 +72,7 @@ nano::write_guard nano::write_database_queue::wait (nano::writer writer) return write_guard ([] {}); } - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); // Add writer to the end of the queue if it's not already waiting auto exists = std::find (queue.cbegin (), queue.cend (), writer) != queue.cend (); if (!exists) @@ -91,7 +91,7 @@ nano::write_guard nano::write_database_queue::wait (nano::writer writer) bool nano::write_database_queue::contains (nano::writer writer) { debug_assert (!use_noops && nano::network_constants ().is_dev_network ()); - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return std::find (queue.cbegin (), queue.cend (), writer) != queue.cend (); } @@ -104,7 +104,7 @@ bool nano::write_database_queue::process (nano::writer writer) auto result = false; { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); // Add writer to the end of the queue if it's not already waiting auto exists = std::find (queue.cbegin (), queue.cend (), writer) != queue.cend (); if (!exists) diff --git a/nano/node/write_database_queue.hpp b/nano/node/write_database_queue.hpp index bdd20250d9..acc300188c 100644 --- a/nano/node/write_database_queue.hpp +++ b/nano/node/write_database_queue.hpp @@ -53,7 +53,7 @@ class write_database_queue final private: std::deque queue; - std::mutex mutex; + nano::mutex mutex; nano::condition_variable cv; std::function guard_finish_callback; bool use_noops; diff --git a/nano/rpc/rpc_request_processor.cpp b/nano/rpc/rpc_request_processor.cpp index f8b81eba91..621aa212e6 100644 --- a/nano/rpc/rpc_request_processor.cpp +++ b/nano/rpc/rpc_request_processor.cpp @@ -13,7 +13,7 @@ thread ([this]() { this->run (); }) { - nano::lock_guard lk (this->request_mutex); + nano::lock_guard lk (this->request_mutex); this->connections.reserve (rpc_config.rpc_process.num_ipc_connections); for (auto i = 0u; i < rpc_config.rpc_process.num_ipc_connections; ++i) { @@ -21,7 +21,7 @@ thread ([this]() { auto connection = this->connections.back (); connection->client.async_connect (ipc_address, ipc_port, [connection, &connections_mutex = this->connections_mutex](nano::error err) { // Even if there is an error this needs to be set so that another attempt can be made to connect with the ipc connection - nano::lock_guard lk (connections_mutex); + nano::lock_guard lk (connections_mutex); connection->is_available = true; }); } @@ -35,7 +35,7 @@ nano::rpc_request_processor::~rpc_request_processor () void nano::rpc_request_processor::stop () { { - nano::lock_guard lock (request_mutex); + nano::lock_guard lock (request_mutex); stopped = true; } condition.notify_one (); @@ -48,7 +48,7 @@ void nano::rpc_request_processor::stop () void nano::rpc_request_processor::add (std::shared_ptr const & request) { { - nano::lock_guard lk (request_mutex); + nano::lock_guard lk (request_mutex); requests.push_back (request); } condition.notify_one (); @@ -80,7 +80,7 @@ void nano::rpc_request_processor::read_payload (std::shared_ptr lk (connections_mutex); + nano::lock_guard lk (connections_mutex); connection.is_available = true; // Allow people to use it now } @@ -124,13 +124,13 @@ void nano::rpc_request_processor::try_reconnect_and_execute_request (std::shared void nano::rpc_request_processor::run () { // This should be a conditioned wait - nano::unique_lock lk (request_mutex); + nano::unique_lock lk (request_mutex); while (!stopped) { if (!requests.empty ()) { lk.unlock (); - nano::unique_lock conditions_lk (connections_mutex); + nano::unique_lock conditions_lk (connections_mutex); // Find the first free ipc_client auto it = std::find_if (connections.begin (), connections.end (), [](auto connection) -> bool { return connection->is_available; diff --git a/nano/rpc/rpc_request_processor.hpp b/nano/rpc/rpc_request_processor.hpp index 46ae632d59..dae034cc80 100644 --- a/nano/rpc/rpc_request_processor.hpp +++ b/nano/rpc/rpc_request_processor.hpp @@ -59,8 +59,8 @@ class rpc_request_processor void make_available (nano::ipc_connection & connection); std::vector> connections; - std::mutex request_mutex; - std::mutex connections_mutex; + nano::mutex request_mutex; + nano::mutex connections_mutex; bool stopped{ false }; std::deque> requests; nano::condition_variable condition; diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 623243ea6d..fd6078be3a 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -1868,7 +1868,7 @@ TEST (rpc, process_block_with_work_watcher) double updated_multiplier; while (!updated) { - nano::unique_lock lock (node1.active.mutex); + nano::unique_lock lock (node1.active.mutex); //fill multipliers_cb and update active difficulty; for (auto i (0); i < node1.active.multipliers_cb.size (); i++) { @@ -2008,7 +2008,7 @@ TEST (rpc, process_block_async_work_watcher) double updated_multiplier; while (!updated) { - nano::unique_lock lock (node1.active.mutex); + nano::unique_lock lock (node1.active.mutex); // Fill multipliers_cb and update active difficulty for (auto i (0); i < node1.active.multipliers_cb.size (); i++) { @@ -2294,11 +2294,11 @@ TEST (rpc, process_difficulty_update_flood) // Ensure the difficulty update occurs in both nodes ASSERT_NO_ERROR (system.poll_until_true (5s, [&node, &node_passive, &send, expected_multiplier] { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); auto const existing (node.active.roots.find (send.qualified_root ())); EXPECT_NE (existing, node.active.roots.end ()); - nano::lock_guard guard_passive (node_passive.active.mutex); + nano::lock_guard guard_passive (node_passive.active.mutex); auto const existing_passive (node_passive.active.roots.find (send.qualified_root ())); EXPECT_NE (existing_passive, node_passive.active.roots.end ()); @@ -7131,7 +7131,7 @@ TEST (rpc, active_difficulty) rpc.start (); boost::property_tree::ptree request; request.put ("action", "active_difficulty"); - nano::unique_lock lock (node->active.mutex); + nano::unique_lock lock (node->active.mutex); node->active.multipliers_cb.push_front (1.5); node->active.multipliers_cb.push_front (4.2); // Also pushes 1.0 to the front of multipliers_cb diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index b1ef86c91e..aca28d94c7 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -749,7 +749,7 @@ std::shared_ptr nano::vote_uniquer::unique (std::shared_ptrblocks.front () = uniquer.unique (boost::get> (result->blocks.front ())); } nano::block_hash key (vote_a->full_hash ()); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto & existing (votes[key]); if (auto block_l = existing.lock ()) { @@ -788,7 +788,7 @@ std::shared_ptr nano::vote_uniquer::unique (std::shared_ptr lock (mutex); + nano::lock_guard lock (mutex); return votes.size (); } diff --git a/nano/secure/common.hpp b/nano/secure/common.hpp index ab7b3d91b9..a3c2b736b1 100644 --- a/nano/secure/common.hpp +++ b/nano/secure/common.hpp @@ -287,7 +287,7 @@ class vote_uniquer final private: nano::block_uniquer & uniquer; - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::vote_uniquer) }; std::unordered_map, value_type::second_type> votes; static unsigned constexpr cleanup_count = 2; }; diff --git a/nano/secure/network_filter.cpp b/nano/secure/network_filter.cpp index f9e6b0374a..0bc98ae503 100644 --- a/nano/secure/network_filter.cpp +++ b/nano/secure/network_filter.cpp @@ -15,7 +15,7 @@ bool nano::network_filter::apply (uint8_t const * bytes_a, size_t count_a, nano: // Get hash before locking auto digest (hash (bytes_a, count_a)); - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto & element (get_element (digest)); bool existed (element == digest); if (!existed) @@ -32,7 +32,7 @@ bool nano::network_filter::apply (uint8_t const * bytes_a, size_t count_a, nano: void nano::network_filter::clear (nano::uint128_t const & digest_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); auto & element (get_element (digest_a)); if (element == digest_a) { @@ -42,7 +42,7 @@ void nano::network_filter::clear (nano::uint128_t const & digest_a) void nano::network_filter::clear (std::vector const & digests_a) { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); for (auto const & digest : digests_a) { auto & element (get_element (digest)); @@ -66,7 +66,7 @@ void nano::network_filter::clear (OBJECT const & object_a) void nano::network_filter::clear () { - nano::lock_guard lock (mutex); + nano::lock_guard lock (mutex); items.assign (items.size (), nano::uint128_t{ 0 }); } diff --git a/nano/secure/network_filter.hpp b/nano/secure/network_filter.hpp index 0e42a2cce7..4374d5d2f0 100644 --- a/nano/secure/network_filter.hpp +++ b/nano/secure/network_filter.hpp @@ -80,6 +80,6 @@ class network_filter final std::vector items; CryptoPP::SecByteBlock key{ siphash_t::KEYLENGTH }; - std::mutex mutex; + nano::mutex mutex{ mutex_identifier (mutexes::network_filter) }; }; } diff --git a/nano/slow_test/node.cpp b/nano/slow_test/node.cpp index aec39be5a6..be9248c800 100644 --- a/nano/slow_test/node.cpp +++ b/nano/slow_test/node.cpp @@ -225,7 +225,7 @@ TEST (node, fork_storm) } else { - nano::unique_lock lock (node_a->active.mutex); + nano::unique_lock lock (node_a->active.mutex); auto election = node_a->active.roots.begin ()->election; lock.unlock (); if (election->votes ().size () == 1) @@ -1458,7 +1458,7 @@ TEST (telemetry, many_nodes) // This is the node which will request metrics from all other nodes auto node_client = system.nodes.front (); - std::mutex mutex; + nano::mutex mutex; std::vector telemetry_datas; auto peers = node_client->network.list (num_nodes - 1); ASSERT_EQ (peers.size (), num_nodes - 1); @@ -1466,13 +1466,13 @@ TEST (telemetry, many_nodes) { node_client->telemetry->get_metrics_single_peer_async (peer, [&telemetry_datas, &mutex](nano::telemetry_data_response const & response_a) { ASSERT_FALSE (response_a.error); - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); telemetry_datas.push_back (response_a.telemetry_data); }); } system.deadline_set (20s); - nano::unique_lock lk (mutex); + nano::unique_lock lk (mutex); while (telemetry_datas.size () != num_nodes - 1) { lk.unlock (); @@ -1719,7 +1719,7 @@ TEST (node, mass_block_new) node.block_processor.flush (); // Clear all active { - nano::lock_guard guard (node.active.mutex); + nano::lock_guard guard (node.active.mutex); node.active.roots.clear (); node.active.blocks.clear (); } diff --git a/nano/test_common/testutil.hpp b/nano/test_common/testutil.hpp index fea9a81686..f0729d6476 100644 --- a/nano/test_common/testutil.hpp +++ b/nano/test_common/testutil.hpp @@ -69,25 +69,25 @@ class stringstream_mt_sink : public boost::iostreams::sink stringstream_mt_sink () = default; stringstream_mt_sink (stringstream_mt_sink const & sink) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); ss << sink.ss.str (); } std::streamsize write (const char * string_to_write, std::streamsize size) { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); ss << std::string (string_to_write, size); return size; } std::string str () { - nano::lock_guard guard (mutex); + nano::lock_guard guard (mutex); return ss.str (); } private: - mutable std::mutex mutex; + mutable nano::mutex mutex; std::stringstream ss; }; @@ -150,7 +150,7 @@ namespace util protected: nano::condition_variable cv; - std::mutex mutex; + nano::mutex mutex; }; /** @@ -183,7 +183,7 @@ namespace util error = count < required_count; if (error) { - nano::unique_lock lock (mutex); + nano::unique_lock lock (mutex); cv.wait_for (lock, std::chrono::milliseconds (1)); } }