From 09b68f3cdb1e7349f8689daaa6c8b2838e38fe0f Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 24 Nov 2020 17:59:40 +0700 Subject: [PATCH] Added BenchStatePrivate class. --- src/backend/common/benchmark/BenchState.cpp | 86 +++++++++++++------ src/backend/common/benchmark/BenchState.h | 11 +-- .../net/stratum/benchmark/BenchClient.cpp | 3 +- 3 files changed, 63 insertions(+), 37 deletions(-) diff --git a/src/backend/common/benchmark/BenchState.cpp b/src/backend/common/benchmark/BenchState.cpp index 4fd5df82..65fc1c42 100644 --- a/src/backend/common/benchmark/BenchState.cpp +++ b/src/backend/common/benchmark/BenchState.cpp @@ -33,17 +33,27 @@ namespace xmrig { -static bool done = false; -static std::mutex mutex; -static std::shared_ptr async; -static uint32_t remaining = 0; -static uint64_t doneTime = 0; -static uint64_t result = 0; -static uint64_t topDiff = 0; +class BenchStatePrivate +{ +public: + BenchStatePrivate(IBenchListener *listener, uint32_t size) : + listener(listener), + size(size) + {} -IBenchListener *BenchState::m_listener = nullptr; -uint32_t BenchState::m_size = 0; + IBenchListener *listener; + std::mutex mutex; + std::shared_ptr async; + uint32_t remaining = 0; + uint32_t size; + uint64_t doneTime = 0; + uint64_t result = 0; + uint64_t topDiff = 0; +}; + + +static BenchStatePrivate *d_ptr = nullptr; } // namespace xmrig @@ -52,7 +62,13 @@ uint32_t BenchState::m_size = 0; bool xmrig::BenchState::isDone() { - return xmrig::done; + return d_ptr == nullptr; +} + + +uint32_t xmrig::BenchState::size() +{ + return d_ptr ? d_ptr->size : 0U; } @@ -71,18 +87,18 @@ uint64_t xmrig::BenchState::referenceHash(const Algorithm &algo, uint32_t size, uint64_t xmrig::BenchState::start(size_t threads, const IBackend *backend) { - assert(m_listener != nullptr); + assert(d_ptr != nullptr); - remaining = static_cast(threads); + d_ptr->remaining = static_cast(threads); - async = std::make_shared([] { - m_listener->onBenchDone(result, topDiff, doneTime); - async.reset(); - xmrig::done = true; + d_ptr->async = std::make_shared([] { + d_ptr->listener->onBenchDone(d_ptr->result, d_ptr->topDiff, d_ptr->doneTime); + + destroy(); }); const uint64_t ts = Chrono::steadyMSecs(); - m_listener->onBenchReady(ts, remaining, backend); + d_ptr->listener->onBenchReady(ts, d_ptr->remaining, backend); return ts; } @@ -90,23 +106,39 @@ uint64_t xmrig::BenchState::start(size_t threads, const IBackend *backend) void xmrig::BenchState::destroy() { - xmrig::done = true; - async.reset(); + delete d_ptr; + d_ptr = nullptr; } void xmrig::BenchState::done(uint64_t data, uint64_t diff, uint64_t ts) { - assert(async && remaining > 0); + assert(d_ptr != nullptr && d_ptr->async && d_ptr->remaining > 0); - std::lock_guard lock(mutex); + std::lock_guard lock(d_ptr->mutex); - result ^= data; - doneTime = std::max(doneTime, ts); - topDiff = std::max(topDiff, diff); - --remaining; + d_ptr->result ^= data; + d_ptr->doneTime = std::max(d_ptr->doneTime, ts); + d_ptr->topDiff = std::max(d_ptr->topDiff, diff); + --d_ptr->remaining; - if (remaining == 0) { - async->send(); + if (d_ptr->remaining == 0) { + d_ptr->async->send(); } } + + +void xmrig::BenchState::init(IBenchListener *listener, uint32_t size) +{ + assert(d_ptr == nullptr); + + d_ptr = new BenchStatePrivate(listener, size); +} + + +void xmrig::BenchState::setSize(uint32_t size) +{ + assert(d_ptr != nullptr); + + d_ptr->size = size; +} diff --git a/src/backend/common/benchmark/BenchState.h b/src/backend/common/benchmark/BenchState.h index e451f4a7..ed7b3371 100644 --- a/src/backend/common/benchmark/BenchState.h +++ b/src/backend/common/benchmark/BenchState.h @@ -36,18 +36,13 @@ class BenchState { public: static bool isDone(); + static uint32_t size(); static uint64_t referenceHash(const Algorithm &algo, uint32_t size, uint32_t threads); static uint64_t start(size_t threads, const IBackend *backend); static void destroy(); static void done(uint64_t data, uint64_t diff, uint64_t ts); - - inline static uint32_t size() { return m_size; } - inline static void setListener(IBenchListener *listener) { m_listener = listener; } - inline static void setSize(uint32_t size) { m_size = size; } - -private: - static IBenchListener *m_listener; - static uint32_t m_size; + static void init(IBenchListener *listener, uint32_t size); + static void setSize(uint32_t size); }; diff --git a/src/base/net/stratum/benchmark/BenchClient.cpp b/src/base/net/stratum/benchmark/BenchClient.cpp index 71d6a183..f05359b0 100644 --- a/src/base/net/stratum/benchmark/BenchClient.cpp +++ b/src/base/net/stratum/benchmark/BenchClient.cpp @@ -51,8 +51,7 @@ xmrig::BenchClient::BenchClient(const std::shared_ptr &benchmark, I blob[Job::kMaxSeedSize * 2] = '\0'; m_job.setSeedHash(blob.data()); - BenchState::setListener(this); - BenchState::setSize(m_benchmark->size()); + BenchState::init(this, m_benchmark->size()); # ifdef XMRIG_FEATURE_HTTP if (m_benchmark->isSubmit()) {