diff --git a/CMakeLists.txt b/CMakeLists.txt index d252af5d..33d3ad70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,11 +35,12 @@ set(HEADERS src/core/config/ConfigTransform.h src/core/config/usage.h src/core/Controller.h - src/interfaces/IJobResultListener.h src/interfaces/IThread.h src/interfaces/IWorker.h src/Mem.h + src/net/interfaces/IJobResultListener.h src/net/JobResult.h + src/net/JobResults.h src/net/Network.h src/net/NetworkState.h src/net/strategies/DonateStrategy.h @@ -90,6 +91,7 @@ set(SOURCES src/core/config/ConfigTransform.cpp src/core/Controller.cpp src/Mem.cpp + src/net/JobResults.cpp src/net/Network.cpp src/net/NetworkState.cpp src/net/strategies/DonateStrategy.cpp diff --git a/src/crypto/common/Algorithm.cpp b/src/crypto/common/Algorithm.cpp index 7680ef02..ab63204f 100644 --- a/src/crypto/common/Algorithm.cpp +++ b/src/crypto/common/Algorithm.cpp @@ -137,9 +137,11 @@ size_t xmrig::Algorithm::memory() const return CnAlgo<>::memory(m_id); } +# ifdef XMRIG_ALGO_RANDOMX if (m_id == RX_WOW) { return 0x100000; } +# endif return 0x200000; } diff --git a/src/net/JobResults.cpp b/src/net/JobResults.cpp new file mode 100644 index 00000000..8c65b5e4 --- /dev/null +++ b/src/net/JobResults.cpp @@ -0,0 +1,139 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include +#include + + +#include "base/tools/Handle.h" +#include "net/interfaces/IJobResultListener.h" +#include "net/JobResult.h" +#include "net/JobResults.h" + + +namespace xmrig { + + +class JobResultsPrivate +{ +public: + inline JobResultsPrivate() + { + uv_mutex_init(&m_mutex); + + m_async = new uv_async_t; + m_async->data = this; + + uv_async_init(uv_default_loop(), m_async, JobResultsPrivate::onResult); + } + + + inline ~JobResultsPrivate() + { + Handle::close(m_async); + } + + + void setListener(IJobResultListener *listener) + { + m_listener = listener; + } + + + void submit(const JobResult &result) + { + uv_mutex_lock(&m_mutex); + m_queue.push_back(result); + uv_mutex_unlock(&m_mutex); + + uv_async_send(m_async); + } + + +private: + static void onResult(uv_async_t *handle) + { + static_cast(handle->data)->submit(); + } + + + inline void submit() + { + std::list results; + + uv_mutex_lock(&m_mutex); + while (!m_queue.empty()) { + results.push_back(std::move(m_queue.front())); + m_queue.pop_front(); + } + uv_mutex_unlock(&m_mutex); + + for (auto result : results) { + m_listener->onJobResult(result); + } + + results.clear(); + } + + + IJobResultListener *m_listener = nullptr; + std::list m_queue; + uv_async_t *m_async; + uv_mutex_t m_mutex; +}; + + +static JobResultsPrivate *handler = new JobResultsPrivate(); + + +} // namespace xmrig + + + +void xmrig::JobResults::setListener(IJobResultListener *listener) +{ + assert(handler != nullptr && listener != nullptr); + + handler->setListener(listener); +} + + +void xmrig::JobResults::stop() +{ + delete handler; + + handler = nullptr; +} + + +void xmrig::JobResults::submit(const JobResult &result) +{ + assert(handler != nullptr); + + if (handler) { + handler->submit(result); + } +} diff --git a/src/net/JobResults.h b/src/net/JobResults.h new file mode 100644 index 00000000..e7082acb --- /dev/null +++ b/src/net/JobResults.h @@ -0,0 +1,49 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_JOBRESULTS_H +#define XMRIG_JOBRESULTS_H + + +namespace xmrig { + + +class IJobResultListener; +class JobResult; + + +class JobResults +{ +public: + static void setListener(IJobResultListener *listener); + static void stop(); + static void submit(const JobResult &result); +}; + + +} // namespace xmrig + + +#endif /* XMRIG_JOBRESULTS_H */ diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 16669f52..c08facb9 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -40,6 +40,7 @@ #include "base/tools/Timer.h" #include "core/config/Config.h" #include "core/Controller.h" +#include "net/JobResults.h" #include "net/Network.h" #include "net/strategies/DonateStrategy.h" #include "rapidjson/document.h" @@ -57,7 +58,7 @@ xmrig::Network::Network(Controller *controller) : m_donate(nullptr), m_timer(nullptr) { - Workers::setListener(this); + JobResults::setListener(this); controller->addListener(this); # ifdef XMRIG_FEATURE_API @@ -77,6 +78,8 @@ xmrig::Network::Network(Controller *controller) : xmrig::Network::~Network() { + JobResults::stop(); + delete m_timer; if (m_donate) { diff --git a/src/interfaces/IJobResultListener.h b/src/net/interfaces/IJobResultListener.h similarity index 100% rename from src/interfaces/IJobResultListener.h rename to src/net/interfaces/IJobResultListener.h diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index dc1292a3..2565e7c4 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -28,6 +28,7 @@ #include "crypto/cn/CryptoNight_test.h" +#include "net/JobResults.h" #include "workers/CpuThreadLegacy.h" #include "workers/MultiWorker.h" #include "workers/Workers.h" @@ -170,7 +171,7 @@ void xmrig::MultiWorker::start() for (size_t i = 0; i < N; ++i) { if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { - Workers::submit(JobResult(m_state.job.poolId(), m_state.job.id(), m_state.job.clientId(), *nonce(i), m_hash + (i * 32), m_state.job.diff(), m_state.job.algorithm())); + JobResults::submit(JobResult(m_state.job.poolId(), m_state.job.id(), m_state.job.clientId(), *nonce(i), m_hash + (i * 32), m_state.job.diff(), m_state.job.algorithm())); } *nonce(i) += 1; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 28590d36..88d73a0b 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -32,7 +32,6 @@ #include "base/tools/Handle.h" #include "core/config/Config.h" #include "core/Controller.h" -#include "interfaces/IJobResultListener.h" #include "interfaces/IThread.h" #include "Mem.h" #include "rapidjson/document.h" @@ -45,15 +44,12 @@ bool Workers::m_active = false; bool Workers::m_enabled = true; Hashrate *Workers::m_hashrate = nullptr; -xmrig::IJobResultListener *Workers::m_listener = nullptr; xmrig::Job Workers::m_job; Workers::LaunchStatus Workers::m_status; std::atomic Workers::m_paused; std::atomic Workers::m_sequence; -std::list Workers::m_queue; std::vector Workers::m_workers; uint64_t Workers::m_ticks = 0; -uv_async_t *Workers::m_async = nullptr; uv_mutex_t Workers::m_mutex; uv_rwlock_t Workers::m_rwlock; uv_timer_t *Workers::m_timer = nullptr; @@ -199,9 +195,6 @@ void Workers::start(xmrig::Controller *controller) m_sequence = 1; m_paused = 1; - m_async = new uv_async_t; - uv_async_init(uv_default_loop(), m_async, Workers::onResult); - m_timer = new uv_timer_t; uv_timer_init(uv_default_loop(), m_timer); uv_timer_start(m_timer, Workers::onTick, 500, 500); @@ -221,7 +214,6 @@ void Workers::start(xmrig::Controller *controller) void Workers::stop() { xmrig::Handle::close(m_timer); - xmrig::Handle::close(m_async); m_hashrate->stop(); m_paused = 0; @@ -233,16 +225,6 @@ void Workers::stop() } -void Workers::submit(const xmrig::JobResult &result) -{ - uv_mutex_lock(&m_mutex); - m_queue.push_back(result); - uv_mutex_unlock(&m_mutex); - - uv_async_send(m_async); -} - - #ifdef XMRIG_FEATURE_API void Workers::threadsSummary(rapidjson::Document &doc) { @@ -306,25 +288,6 @@ void Workers::onReady(void *arg) } -void Workers::onResult(uv_async_t *) -{ - std::list results; - - uv_mutex_lock(&m_mutex); - while (!m_queue.empty()) { - results.push_back(std::move(m_queue.front())); - m_queue.pop_front(); - } - uv_mutex_unlock(&m_mutex); - - for (auto result : results) { - m_listener->onJobResult(result); - } - - results.clear(); -} - - void Workers::onTick(uv_timer_t *) { for (ThreadHandle *handle : m_workers) { diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 96191309..39e872b5 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -47,7 +47,6 @@ class ThreadHandle; namespace xmrig { class Controller; - class IJobResultListener; } @@ -62,7 +61,6 @@ public: static void setJob(const xmrig::Job &job, bool donate); static void start(xmrig::Controller *controller); static void stop(); - static void submit(const xmrig::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; } @@ -70,7 +68,6 @@ public: static inline Hashrate *hashrate() { return m_hashrate; } static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } static inline void pause() { m_active = false; m_paused = 1; m_sequence++; } - static inline void setListener(xmrig::IJobResultListener *listener) { m_listener = listener; } # ifdef XMRIG_FEATURE_API static void threadsSummary(rapidjson::Document &doc); @@ -83,7 +80,6 @@ public: private: static void onReady(void *arg); - static void onResult(uv_async_t *handle); static void onTick(uv_timer_t *handle); static void start(IWorker *worker); @@ -109,15 +105,12 @@ private: static bool m_active; static bool m_enabled; static Hashrate *m_hashrate; - static xmrig::IJobResultListener *m_listener; static xmrig::Job m_job; static LaunchStatus m_status; static std::atomic m_paused; static std::atomic m_sequence; - static std::list m_queue; static std::vector m_workers; static uint64_t m_ticks; - static uv_async_t *m_async; static uv_mutex_t m_mutex; static uv_rwlock_t m_rwlock; static uv_timer_t *m_timer;