Skip to content

Commit

Permalink
Fix hinted_scheduler stop
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 26, 2023
1 parent d8c790d commit ee3c0a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
28 changes: 14 additions & 14 deletions nano/node/hinted_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,34 @@ nano::hinted_scheduler::hinted_scheduler (config const & config_a, nano::node &
inactive_vote_cache{ inactive_vote_cache_a },
active{ active_a },
online_reps{ online_reps_a },
stats{ stats_a },
stopped{ false }
stats{ stats_a }
{
}

nano::hinted_scheduler::~hinted_scheduler ()
{
stop ();
if (thread.joinable ()) // Ensure thread was started
{
thread.join ();
}
// Thread must be stopped before destruction
debug_assert (!thread.joinable ());
}

void nano::hinted_scheduler::start ()
{
debug_assert (!thread.joinable ());
thread = std::thread{
[this] () { run (); }
};

thread = std::thread{ [this] () {
nano::thread_role::set (nano::thread_role::name::election_hinting);
run ();
} };
}

void nano::hinted_scheduler::stop ()
{
nano::unique_lock<nano::mutex> lock{ mutex };
stopped = true;
{
nano::lock_guard<nano::mutex> lock{ mutex };
stopped = true;
}
notify ();
nano::join_or_pass (thread);
}

void nano::hinted_scheduler::notify ()
Expand Down Expand Up @@ -91,7 +92,6 @@ bool nano::hinted_scheduler::run_one (nano::uint128_t const & minimum_tally)

void nano::hinted_scheduler::run ()
{
nano::thread_role::set (nano::thread_role::name::election_hinting);
nano::unique_lock<nano::mutex> lock{ mutex };
while (!stopped)
{
Expand Down Expand Up @@ -124,6 +124,6 @@ void nano::hinted_scheduler::run ()

nano::uint128_t nano::hinted_scheduler::tally_threshold () const
{
const auto min_tally = (online_reps.trended () / 100) * node.config.election_hint_weight_percent;
auto min_tally = (online_reps.trended () / 100) * node.config.election_hint_weight_percent;
return min_tally;
}
5 changes: 3 additions & 2 deletions nano/node/hinted_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ class hinted_scheduler final
};

public:
explicit hinted_scheduler (config const &, nano::node &, nano::vote_cache &, nano::active_transactions &, nano::online_reps &, nano::stats &);
hinted_scheduler (config const &, nano::node &, nano::vote_cache &, nano::active_transactions &, nano::online_reps &, nano::stats &);
~hinted_scheduler ();

void start ();
void stop ();

/*
* Notify about changes in AEC vacancy
*/
Expand All @@ -54,7 +55,7 @@ class hinted_scheduler final
private:
config const config_m;

bool stopped;
bool stopped{ false };
nano::condition_variable condition;
mutable nano::mutex mutex;
std::thread thread;
Expand Down

0 comments on commit ee3c0a4

Please sign in to comment.