From 7c6e4298544b99f5e1f10647b6251bf0c750c0ed Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 10 Jul 2017 21:42:28 +0300 Subject: [PATCH] Improved pause/resume mechanism. --- src/net/Job.cpp | 6 ++++++ src/net/Job.h | 2 ++ src/workers/DoubleWorker.cpp | 3 +++ src/workers/SingleWorker.cpp | 3 +++ src/workers/Workers.cpp | 23 +++++++++++++++++++++++ src/workers/Workers.h | 6 +++++- 6 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 0ec788a43..0199c0059 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -164,3 +164,9 @@ void Job::toHex(const unsigned char* in, unsigned int len, char* out) out[i * 2 + 1] = hf_bin2hex(in[i] & 0x0F); } } + + +bool Job::operator==(const Job &other) const +{ + return memcmp(m_id, other.m_id, sizeof(m_id)) == 0; +} diff --git a/src/net/Job.h b/src/net/Job.h index d8a9e203e..5b88cf450 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -55,6 +55,8 @@ public: static inline uint64_t toDiff(uint64_t target) { return 0xFFFFFFFFFFFFFFFFULL / target; } static void toHex(const unsigned char* in, unsigned int len, char* out); + bool operator==(const Job &other) const; + private: bool m_nicehash; int m_poolId; diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 1ce29760d..b3b606ca4 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -114,6 +114,9 @@ void DoubleWorker::consumeJob() { Job job = Workers::job(); m_sequence = Workers::sequence(); + if (m_state->job == job) { + return; + } save(job); diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 10931fb90..764293b27 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -85,6 +85,9 @@ void SingleWorker::consumeJob() { Job job = Workers::job(); m_sequence = Workers::sequence(); + if (m_job == job) { + return; + } save(job); diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 1ee6262ef..18e38eddf 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -34,6 +34,8 @@ #include "workers/Workers.h" +bool Workers::m_active = false; +bool Workers::m_enabled = true; Hashrate *Workers::m_hashrate = nullptr; IJobResultListener *Workers::m_listener = nullptr; Job Workers::m_job; @@ -58,12 +60,33 @@ Job Workers::job() } +void Workers::setEnabled(bool enabled) +{ + if (m_enabled == enabled) { + return; + } + + m_enabled = enabled; + if (!m_active) { + return; + } + + m_paused = enabled ? 0 : 1; + m_sequence++; +} + + void Workers::setJob(const Job &job) { uv_rwlock_wrlock(&m_rwlock); m_job = job; uv_rwlock_wrunlock(&m_rwlock); + m_active = true; + if (!m_enabled) { + return; + } + m_sequence++; m_paused = 0; } diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 8bc07c1d4..582d56e7f 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -43,14 +43,16 @@ class Workers { public: static Job job(); + static void setEnabled(bool enabled); static void setJob(const Job &job); static void start(int64_t affinity); static void submit(const JobResult &result); + static inline bool isEnabled() { return m_enabled; } static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; } static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; } static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } - static inline void pause() { m_paused = 1; m_sequence++; } + static inline void pause() { m_active = false; m_paused = 1; m_sequence++; } static inline void setListener(IJobResultListener *listener) { m_listener = listener; } private: @@ -58,6 +60,8 @@ private: static void onResult(uv_async_t *handle); static void onTick(uv_timer_t *handle); + static bool m_active; + static bool m_enabled; static Hashrate *m_hashrate; static IJobResultListener *m_listener; static Job m_job;