From 0ce1558f5499ba45cff903551fd536825720b8f2 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 27 Feb 2023 19:23:14 +0100 Subject: [PATCH] Fixed data races --- src/p2pool.cpp | 3 +-- src/p2pool.h | 2 +- src/stratum_server.cpp | 7 +++++-- src/stratum_server.h | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/p2pool.cpp b/src/p2pool.cpp index 9ab9ddf..5c066b4 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -668,9 +668,8 @@ void p2pool::update_block_template() { MinerData data = miner_data(); - if (m_updateSeed) { + if (m_updateSeed.exchange(false)) { m_hasher->set_seed_async(data.seed_hash); - m_updateSeed = false; } m_blockTemplate->update(data, *m_mempool, &m_params->m_wallet); stratum_on_block(); diff --git a/src/p2pool.h b/src/p2pool.h index c8b1dda..7ba722f 100644 --- a/src/p2pool.h +++ b/src/p2pool.h @@ -119,7 +119,7 @@ private: SideChain* m_sideChain; RandomX_Hasher_Base* m_hasher; BlockTemplate* m_blockTemplate; - bool m_updateSeed; + std::atomic m_updateSeed; Mempool* m_mempool; mutable uv_rwlock_t m_mainchainLock; diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index 571db98..ead7068 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -1228,11 +1228,14 @@ void StratumServer::api_update_local_stats(uint64_t timestamp) } // Rate limit to no more than once in 60 seconds. - if (timestamp < m_apiLastUpdateTime + 60) { + uint64_t t = m_apiLastUpdateTime.load(); + if (timestamp < t + 60) { return; } - m_apiLastUpdateTime = timestamp; + if (!m_apiLastUpdateTime.compare_exchange_strong(t, timestamp)) { + return; + } uint64_t hashes_15m, hashes_1h, hashes_24h, total_hashes; int64_t dt_15m, dt_1h, dt_24h; diff --git a/src/stratum_server.h b/src/stratum_server.h index 7a74c50..c6e9a93 100644 --- a/src/stratum_server.h +++ b/src/stratum_server.h @@ -188,7 +188,7 @@ private: uint32_t m_totalFoundShares; uint32_t m_totalFailedShares; - uint64_t m_apiLastUpdateTime; + std::atomic m_apiLastUpdateTime; void update_hashrate_data(uint64_t hashes, uint64_t timestamp); void api_update_local_stats(uint64_t timestamp);