Use std::thread and std:mutex instead of uv_thread_t and uv_mutex_t.

This commit is contained in:
XMRig 2019-07-30 21:25:27 +07:00
parent 1c7ca3a0a7
commit 962f0cdd8e
4 changed files with 47 additions and 50 deletions

View file

@ -26,7 +26,7 @@
#define XMRIG_THREAD_H #define XMRIG_THREAD_H
#include <uv.h> #include <thread>
#include "backend/common/interfaces/IWorker.h" #include "backend/common/interfaces/IWorker.h"
@ -43,21 +43,21 @@ class Thread
{ {
public: public:
inline Thread(IBackend *backend, size_t index, const T &config) : m_index(index), m_config(config), m_backend(backend) {} 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 const T &config() const { return m_config; }
inline IBackend *backend() const { return m_backend; } inline IBackend *backend() const { return m_backend; }
inline IWorker *worker() const { return m_worker; } inline IWorker *worker() const { return m_worker; }
inline size_t index() const { return m_index; } inline size_t index() const { return m_index; }
inline void setWorker(IWorker *worker) { m_worker = worker; } 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: private:
const size_t m_index = 0; const size_t m_index = 0;
const T m_config; const T m_config;
IBackend *m_backend; IBackend *m_backend;
IWorker *m_worker = nullptr; IWorker *m_worker = nullptr;
uv_thread_t m_thread; std::thread m_thread;
}; };

View file

@ -59,6 +59,10 @@ public:
inline MinerPrivate(Controller *controller) : controller(controller) inline MinerPrivate(Controller *controller) : controller(controller)
{ {
uv_rwlock_init(&rwlock); uv_rwlock_init(&rwlock);
# ifdef XMRIG_ALGO_RANDOMX
Rx::init();
# endif
} }
@ -71,6 +75,10 @@ public:
for (IBackend *backend : backends) { for (IBackend *backend : backends) {
delete backend; delete backend;
} }
# ifdef XMRIG_ALGO_RANDOMX
Rx::destroy();
# endif
} }

View file

@ -26,8 +26,8 @@
#include <map> #include <map>
#include <mutex>
#include <thread> #include <thread>
#include <uv.h>
#ifdef XMRIG_FEATURE_HWLOC #ifdef XMRIG_FEATURE_HWLOC
@ -50,7 +50,11 @@
namespace xmrig { namespace xmrig {
class RxPrivate;
static const char *tag = BLUE_BG(WHITE_BOLD_S " rx ") " "; static const char *tag = BLUE_BG(WHITE_BOLD_S " rx ") " ";
static RxPrivate *d_ptr = nullptr;
class RxPrivate class RxPrivate
@ -58,7 +62,6 @@ class RxPrivate
public: public:
inline RxPrivate() inline RxPrivate()
{ {
uv_mutex_init(&mutex);
} }
@ -69,21 +72,15 @@ public:
} }
datasets.clear(); datasets.clear();
uv_mutex_destroy(&mutex);
} }
inline void lock() { uv_mutex_lock(&mutex); } static void allocate(uint32_t nodeId)
inline void unlock() { uv_mutex_unlock(&mutex); }
static void allocate(RxPrivate *self, uint32_t nodeId)
{ {
const uint64_t ts = Chrono::steadyMSecs(); const uint64_t ts = Chrono::steadyMSecs();
# ifdef XMRIG_FEATURE_HWLOC # ifdef XMRIG_FEATURE_HWLOC
if (self->numa) { if (d_ptr->numa) {
hwloc_topology_t topology; hwloc_topology_t topology;
hwloc_topology_init(&topology); hwloc_topology_init(&topology);
hwloc_topology_load(topology); hwloc_topology_load(topology);
@ -113,8 +110,8 @@ public:
RxCache::size() / 1024 / 1024 RxCache::size() / 1024 / 1024
); );
RxDataset *dataset = new RxDataset(self->hugePages); RxDataset *dataset = new RxDataset(d_ptr->hugePages);
self->datasets[nodeId] = dataset; d_ptr->datasets[nodeId] = dataset;
if (dataset->get() != nullptr) { if (dataset->get() != nullptr) {
const auto hugePages = dataset->hugePages(); const auto hugePages = dataset->hugePages();
@ -140,40 +137,33 @@ public:
bool hugePages = true; bool hugePages = true;
bool numa = true; bool numa = true;
std::map<uint32_t, RxDataset *> datasets; std::map<uint32_t, RxDataset *> datasets;
uv_mutex_t mutex; std::mutex mutex;
}; };
static RxPrivate *d_ptr = new RxPrivate();
} // namespace xmrig } // namespace xmrig
bool xmrig::Rx::isReady(const Job &job, uint32_t nodeId) bool xmrig::Rx::isReady(const Job &job, uint32_t nodeId)
{ {
d_ptr->lock(); std::lock_guard<std::mutex> lock(d_ptr->mutex);
const bool rc = isReady(job.seedHash(), job.algorithm(), d_ptr->numa ? nodeId : 0);
d_ptr->unlock();
return rc; return isReady(job.seedHash(), job.algorithm(), d_ptr->numa ? nodeId : 0);
} }
xmrig::RxDataset *xmrig::Rx::dataset(uint32_t nodeId) xmrig::RxDataset *xmrig::Rx::dataset(uint32_t nodeId)
{ {
d_ptr->lock(); std::lock_guard<std::mutex> lock(d_ptr->mutex);
RxDataset *dataset = d_ptr->datasets[d_ptr->numa ? nodeId : 0];
d_ptr->unlock();
return dataset; return d_ptr->datasets[d_ptr->numa ? nodeId : 0];
} }
std::pair<size_t, size_t> xmrig::Rx::hugePages() std::pair<size_t, size_t> xmrig::Rx::hugePages()
{ {
std::pair<size_t, size_t> pages(0, 0); std::pair<size_t, size_t> pages(0, 0);
d_ptr->lock(); std::lock_guard<std::mutex> lock(d_ptr->mutex);
for (auto const &item : d_ptr->datasets) { for (auto const &item : d_ptr->datasets) {
if (!item.second) { if (!item.second) {
@ -185,19 +175,31 @@ std::pair<size_t, size_t> xmrig::Rx::hugePages()
pages.second += p.second; pages.second += p.second;
} }
d_ptr->unlock();
return pages; 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) void xmrig::Rx::init(const Job &job, int initThreads, bool hugePages, bool numa)
{ {
if (job.algorithm().family() != Algorithm::RANDOM_X) { if (job.algorithm().family() != Algorithm::RANDOM_X) {
return; return;
} }
d_ptr->lock(); std::lock_guard<std::mutex> lock(d_ptr->mutex);
size_t ready = 0; 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()) { if (!d_ptr->datasets.empty() && ready == d_ptr->datasets.size()) {
d_ptr->unlock();
return; 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); std::thread thread(initDataset, 0, job.seedHash(), job.algorithm(), threads);
thread.detach(); 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) void xmrig::Rx::initDataset(uint32_t nodeId, const uint8_t *seed, const Algorithm &algorithm, uint32_t threads)
{ {
d_ptr->lock(); std::lock_guard<std::mutex> lock(d_ptr->mutex);
RxDataset *dataset = d_ptr->datasets[nodeId]; RxDataset *dataset = d_ptr->datasets[nodeId];
if (!dataset) { if (!dataset) {
# ifdef XMRIG_FEATURE_HWLOC # ifdef XMRIG_FEATURE_HWLOC
if (d_ptr->numa) { if (d_ptr->numa) {
std::thread thread(RxPrivate::allocate, d_ptr, nodeId); std::thread thread(RxPrivate::allocate, nodeId);
thread.join(); thread.join();
} else } else
# endif # endif
{ {
RxPrivate::allocate(d_ptr, nodeId); RxPrivate::allocate(nodeId);
} }
dataset = d_ptr->datasets[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); LOG_INFO("%s" CYAN_BOLD("#%u") GREEN(" init done") BLACK_BOLD(" (%" PRIu64 " ms)"), tag, nodeId, Chrono::steadyMSecs() - ts);
} }
d_ptr->unlock();
} }

View file

@ -47,8 +47,9 @@ public:
static bool isReady(const Job &job, uint32_t nodeId); static bool isReady(const Job &job, uint32_t nodeId);
static RxDataset *dataset(uint32_t nodeId); static RxDataset *dataset(uint32_t nodeId);
static std::pair<size_t, size_t> hugePages(); static std::pair<size_t, size_t> hugePages();
static void destroy();
static void init();
static void init(const Job &job, int initThreads, bool hugePages, bool numa); static void init(const Job &job, int initThreads, bool hugePages, bool numa);
static void stop();
private: private:
static bool isReady(const uint8_t *seed, const Algorithm &algorithm, uint32_t nodeId); static bool isReady(const uint8_t *seed, const Algorithm &algorithm, uint32_t nodeId);