Skip to content

Commit

Permalink
util: Explain why the path is cached
Browse files Browse the repository at this point in the history
Summary: This is a backport of Core [[bitcoin/bitcoin#16300 | PR16300]]

Test Plan:
  make check

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D5774
  • Loading branch information
MarcoFalke authored and deadalnix committed Apr 20, 2020
1 parent 49cd702 commit 1bbbeff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
14 changes: 6 additions & 8 deletions src/util/system.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Copyright (c) 2009-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down Expand Up @@ -803,15 +803,14 @@ fs::path GetDefaultDataDir() {
static fs::path g_blocks_path_cache_net_specific;
static fs::path pathCached;
static fs::path pathCachedNetSpecific;
static CCriticalSection csPathCached;
static RecursiveMutex csPathCached;

const fs::path &GetBlocksDir() {
LOCK(csPathCached);

fs::path &path = g_blocks_path_cache_net_specific;

// This can be called during exceptions by LogPrintf(), so we cache the
// value so we don't have to do memory allocations after that.
// Cache the path to avoid calling fs::create_directories on every call of
// this function
if (!path.empty()) {
return path;
}
Expand All @@ -834,11 +833,10 @@ const fs::path &GetBlocksDir() {

const fs::path &GetDataDir(bool fNetSpecific) {
LOCK(csPathCached);

fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;

// This can be called during exceptions by LogPrintf(), so we cache the
// value so we don't have to do memory allocations after that.
// Cache the path to avoid calling fs::create_directories on every call of
// this function
if (!path.empty()) {
return path;
}
Expand Down
5 changes: 2 additions & 3 deletions src/util/system.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Copyright (c) 2009-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand All @@ -26,13 +26,11 @@

#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted

#include <atomic>
#include <cstdint>
#include <exception>
#include <map>
#include <set>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -84,6 +82,7 @@ fs::path GetDefaultDataDir();
// The blocks directory is always net specific.
const fs::path &GetBlocksDir();
const fs::path &GetDataDir(bool fNetSpecific = true);
/** Tests only */
void ClearDatadirCache();
fs::path GetConfigFile(const std::string &confPath);
#ifdef WIN32
Expand Down

0 comments on commit 1bbbeff

Please sign in to comment.