Skip to content

Commit fa69c3e

Browse files
author
MarcoFalke
committed
util: Explain why the path is cached
1 parent 3077f11 commit fa69c3e

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/util/system.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2018 The Bitcoin Core developers
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -705,19 +705,16 @@ fs::path GetDefaultDataDir()
705705
static fs::path g_blocks_path_cache_net_specific;
706706
static fs::path pathCached;
707707
static fs::path pathCachedNetSpecific;
708-
static CCriticalSection csPathCached;
708+
static RecursiveMutex csPathCached;
709709

710710
const fs::path &GetBlocksDir()
711711
{
712-
713712
LOCK(csPathCached);
714-
715713
fs::path &path = g_blocks_path_cache_net_specific;
716714

717-
// This can be called during exceptions by LogPrintf(), so we cache the
718-
// value so we don't have to do memory allocations after that.
719-
if (!path.empty())
720-
return path;
715+
// Cache the path to avoid calling fs::create_directories on every call of
716+
// this function
717+
if (!path.empty()) return path;
721718

722719
if (gArgs.IsArgSet("-blocksdir")) {
723720
path = fs::system_complete(gArgs.GetArg("-blocksdir", ""));
@@ -737,15 +734,12 @@ const fs::path &GetBlocksDir()
737734

738735
const fs::path &GetDataDir(bool fNetSpecific)
739736
{
740-
741737
LOCK(csPathCached);
742-
743738
fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
744739

745-
// This can be called during exceptions by LogPrintf(), so we cache the
746-
// value so we don't have to do memory allocations after that.
747-
if (!path.empty())
748-
return path;
740+
// Cache the path to avoid calling fs::create_directories on every call of
741+
// this function
742+
if (!path.empty()) return path;
749743

750744
if (gArgs.IsArgSet("-datadir")) {
751745
path = fs::system_complete(gArgs.GetArg("-datadir", ""));

src/util/system.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2018 The Bitcoin Core developers
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -20,18 +20,16 @@
2020
#include <fs.h>
2121
#include <logging.h>
2222
#include <sync.h>
23-
#include <util/threadnames.h>
2423
#include <tinyformat.h>
2524
#include <util/memory.h>
25+
#include <util/threadnames.h>
2626
#include <util/time.h>
2727

28-
#include <atomic>
2928
#include <exception>
3029
#include <map>
3130
#include <set>
3231
#include <stdint.h>
3332
#include <string>
34-
#include <unordered_set>
3533
#include <utility>
3634
#include <vector>
3735

@@ -85,6 +83,7 @@ fs::path GetDefaultDataDir();
8583
// The blocks directory is always net specific.
8684
const fs::path &GetBlocksDir();
8785
const fs::path &GetDataDir(bool fNetSpecific = true);
86+
/** Tests only */
8887
void ClearDatadirCache();
8988
fs::path GetConfigFile(const std::string& confPath);
9089
#ifdef WIN32

0 commit comments

Comments
 (0)