From 962f0cdd8ee5fe8ba287b47d4b491fca55776828 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 30 Jul 2019 21:25:27 +0700 Subject: [PATCH] Use std::thread and std:mutex instead of uv_thread_t and uv_mutex_t. --- src/backend/common/Thread.h | 8 ++-- src/core/Miner.cpp | 8 ++++ src/crypto/rx/Rx.cpp | 78 ++++++++++++++++--------------------- src/crypto/rx/Rx.h | 3 +- 4 files changed, 47 insertions(+), 50 deletions(-) diff --git a/src/backend/common/Thread.h b/src/backend/common/Thread.h index 36367ece4..b71659151 100644 --- a/src/backend/common/Thread.h +++ b/src/backend/common/Thread.h @@ -26,7 +26,7 @@ #define XMRIG_THREAD_H -#include +#include #include "backend/common/interfaces/IWorker.h" @@ -43,21 +43,21 @@ class Thread { public: inline Thread(IBackend *backend, size_t index, const T &config) : m_index(index), m_config(config), m_backend(backend) {} - inline ~Thread() { uv_thread_join(&m_thread); delete m_worker; } + inline ~Thread() { m_thread.join(); delete m_worker; } inline const T &config() const { return m_config; } inline IBackend *backend() const { return m_backend; } inline IWorker *worker() const { return m_worker; } inline size_t index() const { return m_index; } inline void setWorker(IWorker *worker) { m_worker = worker; } - inline void start(void (*callback) (void *)) { uv_thread_create(&m_thread, callback, this); } + inline void start(void (*callback) (void *)) { m_thread = std::thread(callback, this); } private: const size_t m_index = 0; const T m_config; IBackend *m_backend; IWorker *m_worker = nullptr; - uv_thread_t m_thread; + std::thread m_thread; }; diff --git a/src/core/Miner.cpp b/src/core/Miner.cpp index 64bd54c8d..1bca9fc82 100644 --- a/src/core/Miner.cpp +++ b/src/core/Miner.cpp @@ -59,6 +59,10 @@ public: inline MinerPrivate(Controller *controller) : controller(controller) { uv_rwlock_init(&rwlock); + +# ifdef XMRIG_ALGO_RANDOMX + Rx::init(); +# endif } @@ -71,6 +75,10 @@ public: for (IBackend *backend : backends) { delete backend; } + +# ifdef XMRIG_ALGO_RANDOMX + Rx::destroy(); +# endif } diff --git a/src/crypto/rx/Rx.cpp b/src/crypto/rx/Rx.cpp index 5fc950067..676eca2c3 100644 --- a/src/crypto/rx/Rx.cpp +++ b/src/crypto/rx/Rx.cpp @@ -26,8 +26,8 @@ #include +#include #include -#include #ifdef XMRIG_FEATURE_HWLOC @@ -50,7 +50,11 @@ namespace xmrig { +class RxPrivate; + + static const char *tag = BLUE_BG(WHITE_BOLD_S " rx ") " "; +static RxPrivate *d_ptr = nullptr; class RxPrivate @@ -58,7 +62,6 @@ class RxPrivate public: inline RxPrivate() { - uv_mutex_init(&mutex); } @@ -69,21 +72,15 @@ public: } datasets.clear(); - - uv_mutex_destroy(&mutex); } - inline void lock() { uv_mutex_lock(&mutex); } - inline void unlock() { uv_mutex_unlock(&mutex); } - - - static void allocate(RxPrivate *self, uint32_t nodeId) + static void allocate(uint32_t nodeId) { const uint64_t ts = Chrono::steadyMSecs(); # ifdef XMRIG_FEATURE_HWLOC - if (self->numa) { + if (d_ptr->numa) { hwloc_topology_t topology; hwloc_topology_init(&topology); hwloc_topology_load(topology); @@ -113,8 +110,8 @@ public: RxCache::size() / 1024 / 1024 ); - RxDataset *dataset = new RxDataset(self->hugePages); - self->datasets[nodeId] = dataset; + RxDataset *dataset = new RxDataset(d_ptr->hugePages); + d_ptr->datasets[nodeId] = dataset; if (dataset->get() != nullptr) { const auto hugePages = dataset->hugePages(); @@ -140,40 +137,33 @@ public: bool hugePages = true; bool numa = true; std::map datasets; - uv_mutex_t mutex; + std::mutex mutex; }; -static RxPrivate *d_ptr = new RxPrivate(); - - } // namespace xmrig bool xmrig::Rx::isReady(const Job &job, uint32_t nodeId) { - d_ptr->lock(); - const bool rc = isReady(job.seedHash(), job.algorithm(), d_ptr->numa ? nodeId : 0); - d_ptr->unlock(); + std::lock_guard lock(d_ptr->mutex); - return rc; + return isReady(job.seedHash(), job.algorithm(), d_ptr->numa ? nodeId : 0); } xmrig::RxDataset *xmrig::Rx::dataset(uint32_t nodeId) { - d_ptr->lock(); - RxDataset *dataset = d_ptr->datasets[d_ptr->numa ? nodeId : 0]; - d_ptr->unlock(); + std::lock_guard lock(d_ptr->mutex); - return dataset; + return d_ptr->datasets[d_ptr->numa ? nodeId : 0]; } std::pair xmrig::Rx::hugePages() { std::pair pages(0, 0); - d_ptr->lock(); + std::lock_guard lock(d_ptr->mutex); for (auto const &item : d_ptr->datasets) { if (!item.second) { @@ -185,19 +175,31 @@ std::pair xmrig::Rx::hugePages() pages.second += p.second; } - d_ptr->unlock(); - return pages; } +void xmrig::Rx::destroy() +{ + delete d_ptr; + + d_ptr = nullptr; +} + + +void xmrig::Rx::init() +{ + d_ptr = new RxPrivate(); +} + + void xmrig::Rx::init(const Job &job, int initThreads, bool hugePages, bool numa) { if (job.algorithm().family() != Algorithm::RANDOM_X) { return; } - d_ptr->lock(); + std::lock_guard lock(d_ptr->mutex); size_t ready = 0; @@ -208,8 +210,6 @@ void xmrig::Rx::init(const Job &job, int initThreads, bool hugePages, bool numa) } if (!d_ptr->datasets.empty() && ready == d_ptr->datasets.size()) { - d_ptr->unlock(); - return; } @@ -231,16 +231,6 @@ void xmrig::Rx::init(const Job &job, int initThreads, bool hugePages, bool numa) std::thread thread(initDataset, 0, job.seedHash(), job.algorithm(), threads); thread.detach(); } - - d_ptr->unlock(); -} - - -void xmrig::Rx::stop() -{ - delete d_ptr; - - d_ptr = nullptr; } @@ -252,19 +242,19 @@ bool xmrig::Rx::isReady(const uint8_t *seed, const Algorithm &algorithm, uint32_ void xmrig::Rx::initDataset(uint32_t nodeId, const uint8_t *seed, const Algorithm &algorithm, uint32_t threads) { - d_ptr->lock(); + std::lock_guard lock(d_ptr->mutex); RxDataset *dataset = d_ptr->datasets[nodeId]; if (!dataset) { # ifdef XMRIG_FEATURE_HWLOC if (d_ptr->numa) { - std::thread thread(RxPrivate::allocate, d_ptr, nodeId); + std::thread thread(RxPrivate::allocate, nodeId); thread.join(); } else # endif { - RxPrivate::allocate(d_ptr, nodeId); + RxPrivate::allocate(nodeId); } dataset = d_ptr->datasets[nodeId]; @@ -295,6 +285,4 @@ void xmrig::Rx::initDataset(uint32_t nodeId, const uint8_t *seed, const Algorith LOG_INFO("%s" CYAN_BOLD("#%u") GREEN(" init done") BLACK_BOLD(" (%" PRIu64 " ms)"), tag, nodeId, Chrono::steadyMSecs() - ts); } - - d_ptr->unlock(); } diff --git a/src/crypto/rx/Rx.h b/src/crypto/rx/Rx.h index 43e4be1ca..c484c3b4f 100644 --- a/src/crypto/rx/Rx.h +++ b/src/crypto/rx/Rx.h @@ -47,8 +47,9 @@ public: static bool isReady(const Job &job, uint32_t nodeId); static RxDataset *dataset(uint32_t nodeId); static std::pair hugePages(); + static void destroy(); + static void init(); static void init(const Job &job, int initThreads, bool hugePages, bool numa); - static void stop(); private: static bool isReady(const uint8_t *seed, const Algorithm &algorithm, uint32_t nodeId);