diff --git a/nano/core_test/distributed_work.cpp b/nano/core_test/distributed_work.cpp index 3642b7b2..9f39acf9 100644 --- a/nano/core_test/distributed_work.cpp +++ b/nano/core_test/distributed_work.cpp @@ -83,12 +83,12 @@ TEST (distributed_work, no_peers_multi) { node->distributed_work.make (hash, callback, nano::difficulty::from_multiplier (10, node->network_params.network.publish_threshold)); } - // 1 root, and _total_ requests for that root are expected + // 1 root, and _total_ requests for that root are expected, but some may have already finished ASSERT_EQ (1, node->distributed_work.work.size ()); { auto requests (node->distributed_work.work.begin ()); ASSERT_EQ (hash, requests->first); - ASSERT_EQ (total, requests->second.size ()); + ASSERT_GE (requests->second.size (), total - 4); } system.deadline_set (5s); while (count < total) diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index eec09a5e..7528bda7 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -1130,11 +1130,11 @@ TEST (wallet, work_watcher_update) //fill multipliers_cb and update active difficulty; for (auto i (0); i < node.active.multipliers_cb.size (); i++) { - node.active.multipliers_cb.push_back (multiplier * (2 + i / 100.)); + node.active.multipliers_cb.push_back (multiplier * (1.5 + i / 100.)); } node.active.update_active_difficulty (lock); } - system.deadline_set (10s); + system.deadline_set (20s); while (updated_difficulty1 == difficulty1 || updated_difficulty2 == difficulty2) { { diff --git a/nano/lib/utility.cpp b/nano/lib/utility.cpp index 439bd116..daa5b18e 100644 --- a/nano/lib/utility.cpp +++ b/nano/lib/utility.cpp @@ -250,7 +250,6 @@ void nano::worker::run () // So that we reduce locking for anything being pushed as that will // most likely be on an io-thread std::this_thread::yield (); - lk.lock (); } else { diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index 49483b17..5daca47e 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -20,10 +20,7 @@ thread ([this]() { }) { std::unique_lock lock (mutex); - while (!started) - { - condition.wait (lock); - } + condition.wait (lock, [& started = started] { return started; }); } nano::active_transactions::~active_transactions () @@ -340,7 +337,8 @@ void nano::active_transactions::request_loop () break; } const auto extra_delay (std::min (roots.size (), max_broadcast_queue) * node.network.broadcast_interval_ms * 2); - condition.wait_for (lock, std::chrono::milliseconds (node.network_params.network.request_interval_ms + extra_delay)); + const auto wakeup (std::chrono::steady_clock::now () + std::chrono::milliseconds (node.network_params.network.request_interval_ms + extra_delay)); + condition.wait_until (lock, wakeup, [&wakeup] { return std::chrono::steady_clock::now () >= wakeup; }); } } @@ -504,10 +502,7 @@ void nano::active_transactions::prioritize_frontiers_for_confirmation (nano::tra void nano::active_transactions::stop () { std::unique_lock lock (mutex); - while (!started) - { - condition.wait (lock); - } + condition.wait (lock, [& started = started] { return started; }); stopped = true; lock.unlock (); condition.notify_all (); diff --git a/nano/node/bootstrap.cpp b/nano/node/bootstrap.cpp index 7b6f7bcd..2c756bec 100644 --- a/nano/node/bootstrap.cpp +++ b/nano/node/bootstrap.cpp @@ -982,10 +982,9 @@ void nano::bootstrap_attempt::run () std::shared_ptr nano::bootstrap_attempt::connection (std::unique_lock & lock_a) { - while (!stopped && idle.empty ()) - { - condition.wait (lock_a); - } + // clang-format off + condition.wait (lock_a, [& stopped = stopped, &idle = idle] { return stopped || !idle.empty (); }); + // clang-format on std::shared_ptr result; if (!idle.empty ()) { @@ -1687,10 +1686,10 @@ void nano::bootstrap_initiator::bootstrap (nano::endpoint const & endpoint_a, bo std::unique_lock lock (mutex); if (!stopped) { - while (attempt != nullptr) + if (attempt != nullptr) { attempt->stop (); - condition.wait (lock); + condition.wait (lock, [attempt = attempt] { return attempt == nullptr; }); } node.stats.inc (nano::stat::type::bootstrap, nano::stat::detail::initiate, nano::stat::dir::out); attempt = std::make_shared (node.shared ()); @@ -1705,10 +1704,10 @@ void nano::bootstrap_initiator::bootstrap_lazy (nano::block_hash const & hash_a, std::unique_lock lock (mutex); if (force) { - while (attempt != nullptr) + if (attempt != nullptr) { attempt->stop (); - condition.wait (lock); + condition.wait (lock, [attempt = attempt] { return attempt == nullptr; }); } } node.stats.inc (nano::stat::type::bootstrap, nano::stat::detail::initiate_lazy, nano::stat::dir::out); diff --git a/nano/node/network.cpp b/nano/node/network.cpp index fdde25e8..fb13d8c1 100644 --- a/nano/node/network.cpp +++ b/nano/node/network.cpp @@ -842,10 +842,12 @@ stopped (false) nano::message_buffer * nano::message_buffer_manager::allocate () { std::unique_lock lock (mutex); - while (!stopped && free.empty () && full.empty ()) + if (!stopped && free.empty () && full.empty ()) { stats.inc (nano::stat::type::udp, nano::stat::detail::blocking, nano::stat::dir::in); - condition.wait (lock); + // clang-format off + condition.wait (lock, [& stopped = stopped, &free = free, &full = full] { return stopped || !free.empty () || !full.empty (); }); + // clang-format on } nano::message_buffer * result (nullptr); if (!free.empty ()) diff --git a/nano/node/vote_processor.cpp b/nano/node/vote_processor.cpp index d80cdf94..0ed5ef0b 100644 --- a/nano/node/vote_processor.cpp +++ b/nano/node/vote_processor.cpp @@ -13,10 +13,7 @@ thread ([this]() { }) { std::unique_lock lock (mutex); - while (!started) - { - condition.wait (lock); - } + condition.wait (lock, [& started = started] { return started; }); } void nano::vote_processor::process_loop () diff --git a/nano/node/voting.cpp b/nano/node/voting.cpp index 025dd688..83f8ea72 100644 --- a/nano/node/voting.cpp +++ b/nano/node/voting.cpp @@ -8,10 +8,7 @@ node (node_a), thread ([this]() { run (); }) { std::unique_lock lock (mutex); - while (!started) - { - condition.wait (lock); - } + condition.wait (lock, [& started = started] { return started; }); } void nano::vote_generator::add (nano::block_hash const & hash_a)