Skip to content

Commit 374fd6f

Browse files
author
MarcoFalke
committed
Merge #19189: refactor: Replace RecursiveMutex with Mutex in timedata.cpp
cc5c0d2 refactor: Fix formatting of timedata.cpp (Hennadii Stepanov) c2410ce refactor: Replace RecursiveMutex with Mutex in timedata.cpp (Hennadii Stepanov) Pull request description: Only `GetTimeOffset()` and `AddTimeData()` functions lock this mutex. They do not call itself recursively, and do not call each other either directly or indirectly. Therefore, the `g_timeoffset_mutex` could be a non-recursive mutex. Related to #19180. ACKs for top commit: MarcoFalke: ACK cc5c0d2 , checked the second commit with --word-diff-regex=. --ignore-all-space -U0 🦉 vasild: ACK cc5c0d2 verified that recursion is not happening Tree-SHA512: 38f6df689374d4a1a0e9aedb3ed5e885d8285c4da6b75f9bc84ae036936a159ef8276462db33b4f4dd5c71c6312fa9b45380f7a5726959665bc71dc39031be88
2 parents 8496dbe + cc5c0d2 commit 374fd6f

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/timedata.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
#include <util/translation.h>
1616
#include <warnings.h>
1717

18-
19-
static RecursiveMutex cs_nTimeOffset;
20-
static int64_t nTimeOffset GUARDED_BY(cs_nTimeOffset) = 0;
18+
static Mutex g_timeoffset_mutex;
19+
static int64_t nTimeOffset GUARDED_BY(g_timeoffset_mutex) = 0;
2120

2221
/**
2322
* "Never go to sea with two chronometers; take one or three."
@@ -28,7 +27,7 @@ static int64_t nTimeOffset GUARDED_BY(cs_nTimeOffset) = 0;
2827
*/
2928
int64_t GetTimeOffset()
3029
{
31-
LOCK(cs_nTimeOffset);
30+
LOCK(g_timeoffset_mutex);
3231
return nTimeOffset;
3332
}
3433

@@ -46,7 +45,7 @@ static int64_t abs64(int64_t n)
4645

4746
void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
4847
{
49-
LOCK(cs_nTimeOffset);
48+
LOCK(g_timeoffset_mutex);
5049
// Ignore duplicates
5150
static std::set<CNetAddr> setKnown;
5251
if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
@@ -57,7 +56,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
5756
// Add data
5857
static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
5958
vTimeOffsets.input(nOffsetSample);
60-
LogPrint(BCLog::NET,"added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
59+
LogPrint(BCLog::NET, "added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample / 60);
6160

6261
// There is a known issue here (see issue #4521):
6362
//
@@ -76,30 +75,24 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
7675
// So we should hold off on fixing this and clean it up as part of
7776
// a timing cleanup that strengthens it in a number of other ways.
7877
//
79-
if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
80-
{
78+
if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) {
8179
int64_t nMedian = vTimeOffsets.median();
8280
std::vector<int64_t> vSorted = vTimeOffsets.sorted();
8381
// Only let other nodes change our time by so much
84-
if (abs64(nMedian) <= std::max<int64_t>(0, gArgs.GetArg("-maxtimeadjustment", DEFAULT_MAX_TIME_ADJUSTMENT)))
85-
{
82+
if (abs64(nMedian) <= std::max<int64_t>(0, gArgs.GetArg("-maxtimeadjustment", DEFAULT_MAX_TIME_ADJUSTMENT))) {
8683
nTimeOffset = nMedian;
87-
}
88-
else
89-
{
84+
} else {
9085
nTimeOffset = 0;
9186

9287
static bool fDone;
93-
if (!fDone)
94-
{
88+
if (!fDone) {
9589
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
9690
bool fMatch = false;
97-
for (const int64_t nOffset : vSorted)
98-
if (nOffset != 0 && abs64(nOffset) < 5 * 60)
99-
fMatch = true;
91+
for (const int64_t nOffset : vSorted) {
92+
if (nOffset != 0 && abs64(nOffset) < 5 * 60) fMatch = true;
93+
}
10094

101-
if (!fMatch)
102-
{
95+
if (!fMatch) {
10396
fDone = true;
10497
bilingual_str strMessage = strprintf(_("Please check that your computer's date and time are correct! If your clock is wrong, %s will not work properly."), PACKAGE_NAME);
10598
SetMiscWarning(strMessage.translated);
@@ -113,8 +106,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
113106
LogPrint(BCLog::NET, "%+d ", n); /* Continued */
114107
}
115108
LogPrint(BCLog::NET, "| "); /* Continued */
116-
117-
LogPrint(BCLog::NET, "nTimeOffset = %+d (%+d minutes)\n", nTimeOffset, nTimeOffset/60);
109+
LogPrint(BCLog::NET, "nTimeOffset = %+d (%+d minutes)\n", nTimeOffset, nTimeOffset / 60);
118110
}
119111
}
120112
}

0 commit comments

Comments
 (0)