uv_mutex replaced to std::mutex.

This commit is contained in:
XMRig 2019-08-09 01:00:21 +07:00
parent ce370bf721
commit 647cbef43c
3 changed files with 39 additions and 59 deletions

View file

@ -23,7 +23,7 @@
*/ */
#include <uv.h> #include <mutex>
#include "backend/common/Hashrate.h" #include "backend/common/Hashrate.h"
@ -83,13 +83,6 @@ public:
inline CpuBackendPrivate(Controller *controller) : inline CpuBackendPrivate(Controller *controller) :
controller(controller) controller(controller)
{ {
uv_mutex_init(&mutex);
}
inline ~CpuBackendPrivate()
{
uv_mutex_destroy(&mutex);
} }
@ -119,9 +112,9 @@ public:
Algorithm algo; Algorithm algo;
Controller *controller; Controller *controller;
LaunchStatus status; LaunchStatus status;
std::mutex mutex;
std::vector<CpuLaunchData> threads; std::vector<CpuLaunchData> threads;
String profileName; String profileName;
uv_mutex_t mutex;
Workers<CpuLaunchData> workers; Workers<CpuLaunchData> workers;
}; };
@ -233,7 +226,7 @@ void xmrig::CpuBackend::setJob(const Job &job)
void xmrig::CpuBackend::start(IWorker *worker) void xmrig::CpuBackend::start(IWorker *worker)
{ {
uv_mutex_lock(&d_ptr->mutex); d_ptr->mutex.lock();
const auto pages = worker->memory()->hugePages(); const auto pages = worker->memory()->hugePages();
@ -254,7 +247,7 @@ void xmrig::CpuBackend::start(IWorker *worker)
); );
} }
uv_mutex_unlock(&d_ptr->mutex); d_ptr->mutex.unlock();
worker->start(); worker->start();
} }
@ -299,10 +292,10 @@ rapidjson::Value xmrig::CpuBackend::toJSON(rapidjson::Document &doc) const
out.AddMember("asm", false, allocator); out.AddMember("asm", false, allocator);
# endif # endif
uv_mutex_lock(&d_ptr->mutex); d_ptr->mutex.lock();
uint64_t pages[2] = { d_ptr->status.hugePages, d_ptr->status.pages }; uint64_t pages[2] = { d_ptr->status.hugePages, d_ptr->status.pages };
const size_t ways = d_ptr->status.ways; const size_t ways = d_ptr->status.ways;
uv_mutex_unlock(&d_ptr->mutex); d_ptr->mutex.unlock();
# ifdef XMRIG_ALGO_RANDOMX # ifdef XMRIG_ALGO_RANDOMX
if (d_ptr->algo.family() == Algorithm::RANDOM_X) { if (d_ptr->algo.family() == Algorithm::RANDOM_X) {

View file

@ -23,7 +23,7 @@
*/ */
#include <uv.h> #include <mutex>
#include "crypto/common/Nonce.h" #include "crypto/common/Nonce.h"
@ -37,7 +37,7 @@ std::atomic<uint64_t> Nonce::m_sequence[Nonce::MAX];
uint32_t Nonce::m_nonces[2] = { 0, 0 }; uint32_t Nonce::m_nonces[2] = { 0, 0 };
static uv_mutex_t mutex; static std::mutex mutex;
static Nonce nonce; static Nonce nonce;
@ -51,8 +51,6 @@ xmrig::Nonce::Nonce()
for (int i = 0; i < MAX; ++i) { for (int i = 0; i < MAX; ++i) {
m_sequence[i] = 1; m_sequence[i] = 1;
} }
uv_mutex_init(&mutex);
} }
@ -60,7 +58,7 @@ uint32_t xmrig::Nonce::next(uint8_t index, uint32_t nonce, uint32_t reserveCount
{ {
uint32_t next; uint32_t next;
uv_mutex_lock(&mutex); std::lock_guard<std::mutex> lock(mutex);
if (nicehash) { if (nicehash) {
next = (nonce & 0xFF000000) | m_nonces[index]; next = (nonce & 0xFF000000) | m_nonces[index];
@ -71,20 +69,16 @@ uint32_t xmrig::Nonce::next(uint8_t index, uint32_t nonce, uint32_t reserveCount
m_nonces[index] += reserveCount; m_nonces[index] += reserveCount;
uv_mutex_unlock(&mutex);
return next; return next;
} }
void xmrig::Nonce::reset(uint8_t index) void xmrig::Nonce::reset(uint8_t index)
{ {
uv_mutex_lock(&mutex); std::lock_guard<std::mutex> lock(mutex);
m_nonces[index] = 0; m_nonces[index] = 0;
touch(); touch();
uv_mutex_unlock(&mutex);
} }

View file

@ -25,6 +25,7 @@
#include <assert.h> #include <assert.h>
#include <list> #include <list>
#include <mutex>
#include <uv.h> #include <uv.h>
@ -40,75 +41,65 @@ namespace xmrig {
class JobResultsPrivate class JobResultsPrivate
{ {
public: public:
inline JobResultsPrivate() inline JobResultsPrivate(IJobResultListener *listener) :
listener(listener)
{ {
uv_mutex_init(&m_mutex); async = new uv_async_t;
async->data = this;
m_async = new uv_async_t; uv_async_init(uv_default_loop(), async, JobResultsPrivate::onResult);
m_async->data = this;
uv_async_init(uv_default_loop(), m_async, JobResultsPrivate::onResult);
} }
inline ~JobResultsPrivate() inline ~JobResultsPrivate()
{ {
Handle::close(m_async); Handle::close(async);
uv_mutex_destroy(&m_mutex);
}
void setListener(IJobResultListener *listener)
{
m_listener = listener;
} }
void submit(const JobResult &result) void submit(const JobResult &result)
{ {
uv_mutex_lock(&m_mutex); mutex.lock();
m_queue.push_back(result); queue.push_back(result);
uv_mutex_unlock(&m_mutex); mutex.unlock();
uv_async_send(m_async); uv_async_send(async);
} }
private: private:
static void onResult(uv_async_t *handle) static void onResult(uv_async_t *handle) { static_cast<JobResultsPrivate*>(handle->data)->submit(); }
{
static_cast<JobResultsPrivate*>(handle->data)->submit();
}
inline void submit() inline void submit()
{ {
std::list<JobResult> results; std::list<JobResult> results;
uv_mutex_lock(&m_mutex); mutex.lock();
while (!m_queue.empty()) {
results.push_back(std::move(m_queue.front())); while (!queue.empty()) {
m_queue.pop_front(); results.push_back(std::move(queue.front()));
queue.pop_front();
} }
uv_mutex_unlock(&m_mutex);
mutex.unlock();
for (auto result : results) { for (auto result : results) {
m_listener->onJobResult(result); listener->onJobResult(result);
} }
results.clear(); results.clear();
} }
IJobResultListener *m_listener = nullptr; IJobResultListener *listener;
std::list<JobResult> m_queue; std::list<JobResult> queue;
uv_async_t *m_async; std::mutex mutex;
uv_mutex_t m_mutex; uv_async_t *async;
}; };
static JobResultsPrivate *handler = new JobResultsPrivate(); static JobResultsPrivate *handler = nullptr;
} // namespace xmrig } // namespace xmrig
@ -117,14 +108,16 @@ static JobResultsPrivate *handler = new JobResultsPrivate();
void xmrig::JobResults::setListener(IJobResultListener *listener) void xmrig::JobResults::setListener(IJobResultListener *listener)
{ {
assert(handler != nullptr && listener != nullptr); assert(handler == nullptr);
handler->setListener(listener); handler = new JobResultsPrivate(listener);
} }
void xmrig::JobResults::stop() void xmrig::JobResults::stop()
{ {
assert(handler != nullptr);
delete handler; delete handler;
handler = nullptr; handler = nullptr;