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

View file

@ -23,7 +23,7 @@
*/
#include <uv.h>
#include <mutex>
#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 };
static uv_mutex_t mutex;
static std::mutex mutex;
static Nonce nonce;
@ -51,8 +51,6 @@ xmrig::Nonce::Nonce()
for (int i = 0; i < MAX; ++i) {
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;
uv_mutex_lock(&mutex);
std::lock_guard<std::mutex> lock(mutex);
if (nicehash) {
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;
uv_mutex_unlock(&mutex);
return next;
}
void xmrig::Nonce::reset(uint8_t index)
{
uv_mutex_lock(&mutex);
std::lock_guard<std::mutex> lock(mutex);
m_nonces[index] = 0;
touch();
uv_mutex_unlock(&mutex);
}

View file

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