Improve bootstrap attempt locking (#2286)

This commit is contained in:
Wesley Shillingford 2019-09-05 12:44:41 +01:00 committed by GitHub
commit f7cef4cca5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 9 additions and 6 deletions

View file

@ -15,8 +15,8 @@ if [[ $(grep -rl --exclude="*asio.hpp" "asio::async_write" ./nano) ]]; then
fi
# prevent unsolicited use of std::lock_guard & std::unique_lock outside of allowed areas
if [[ $(grep -rl --exclude={"*random_pool.cpp","*random_pool.hpp","*locks.hpp"} "std::unique_lock\|std::lock_guard" ./nano) ]]; then
echo "using std::unique_lock or std::lock_guard is not permitted (except in nano/lib/locks.hpp and non-nano dependent libraries). Use the nano::* versions instead"
if [[ $(grep -rl --exclude={"*random_pool.cpp","*random_pool.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"
exit 1
fi

View file

@ -7,7 +7,10 @@ namespace
template <typename Mutex>
void output (const char * str, std::chrono::milliseconds time, Mutex & mutex)
{
static std::mutex cout_mutex;
auto stacktrace = nano::generate_stacktrace ();
// Guard standard out to keep the output from being interleaved
std::lock_guard<std::mutex> guard (cout_mutex);
std::cout << std::addressof (mutex) << " Mutex " << str << " for: " << time.count () << "ms\n"
<< stacktrace << std::endl;
}

View file

@ -85,7 +85,7 @@ nano::bootstrap_attempt::~bootstrap_attempt ()
bool nano::bootstrap_attempt::should_log ()
{
nano::lock_guard<std::mutex> lock (mutex);
nano::lock_guard<std::mutex> guard (next_log_mutex);
auto result (false);
auto now (std::chrono::steady_clock::now ());
if (next_log < now)

View file

@ -80,6 +80,7 @@ public:
void wallet_run ();
void wallet_start (std::deque<nano::account> &);
bool wallet_finished ();
std::mutex next_log_mutex;
std::chrono::steady_clock::time_point next_log;
std::deque<std::weak_ptr<nano::bootstrap_client>> clients;
std::weak_ptr<nano::bootstrap_client> connection_frontier_request;
@ -97,7 +98,7 @@ public:
std::atomic<bool> stopped;
nano::bootstrap_mode mode;
std::mutex mutex;
std::condition_variable condition;
nano::condition_variable condition;
// Lazy bootstrap
std::unordered_set<nano::block_hash> lazy_blocks;
std::unordered_map<nano::block_hash, std::pair<nano::block_hash, nano::uint128_t>> lazy_state_unknown;
@ -176,7 +177,7 @@ private:
std::shared_ptr<nano::bootstrap_attempt> attempt;
std::atomic<bool> stopped;
std::mutex mutex;
std::condition_variable condition;
nano::condition_variable condition;
std::mutex observers_mutex;
std::vector<std::function<void(bool)>> observers;
boost::thread thread;

View file

@ -19,7 +19,6 @@ pull (pull_a),
pull_blocks (0),
unexpected_count (0)
{
nano::lock_guard<std::mutex> mutex (connection->attempt->mutex);
connection->attempt->condition.notify_all ();
}