Run internal http server in main loop to avoid requirement to thread synchronization.

This commit is contained in:
XMRig 2018-02-20 23:22:34 +07:00
parent 71759e3752
commit a0d4e4ed3f
4 changed files with 33 additions and 17 deletions

View file

@ -4,7 +4,7 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2016-2018 XMRig <support@xmrig.com>
* *
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -29,12 +29,10 @@
ApiState *Api::m_state = nullptr; ApiState *Api::m_state = nullptr;
uv_mutex_t Api::m_mutex;
bool Api::start() bool Api::start()
{ {
uv_mutex_init(&m_mutex);
m_state = new ApiState(); m_state = new ApiState();
return true; return true;
@ -53,11 +51,7 @@ char *Api::get(const char *url, int *status)
return nullptr; return nullptr;
} }
uv_mutex_lock(&m_mutex); return m_state->get(url, status);
char *buf = m_state->get(url, status);
uv_mutex_unlock(&m_mutex);
return buf;
} }
@ -67,9 +61,7 @@ void Api::tick(const Hashrate *hashrate)
return; return;
} }
uv_mutex_lock(&m_mutex);
m_state->tick(hashrate); m_state->tick(hashrate);
uv_mutex_unlock(&m_mutex);
} }
@ -79,7 +71,5 @@ void Api::tick(const NetworkState &network)
return; return;
} }
uv_mutex_lock(&m_mutex);
m_state->tick(network); m_state->tick(network);
uv_mutex_unlock(&m_mutex);
} }

View file

@ -4,7 +4,7 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2016-2018 XMRig <support@xmrig.com>
* *
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -45,7 +45,6 @@ public:
private: private:
static ApiState *m_state; static ApiState *m_state;
static uv_mutex_t m_mutex;
}; };
#endif /* __API_H__ */ #endif /* __API_H__ */

View file

@ -4,7 +4,7 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2016-2018 XMRig <support@xmrig.com>
* *
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -36,6 +36,18 @@ Httpd::Httpd(int port, const char *accessToken) :
m_port(port), m_port(port),
m_daemon(nullptr) m_daemon(nullptr)
{ {
uv_idle_init(uv_default_loop(), &m_idle);
m_idle.data = this;
}
Httpd::~Httpd()
{
uv_idle_stop(&m_idle);
if (m_daemon) {
MHD_stop_daemon(m_daemon);
}
} }
@ -45,12 +57,18 @@ bool Httpd::start()
return false; return false;
} }
m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); unsigned int flags = 0;
if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) {
flags |= MHD_USE_EPOLL_LINUX_ONLY;
}
m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END);
if (!m_daemon) { if (!m_daemon) {
LOG_ERR("HTTP Daemon failed to start."); LOG_ERR("HTTP Daemon failed to start.");
return false; return false;
} }
uv_idle_start(&m_idle, Httpd::onIdle);
return true; return true;
} }
@ -114,3 +132,9 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url
MHD_Response *rsp = MHD_create_response_from_buffer(strlen(buf), (void*) buf, MHD_RESPMEM_MUST_FREE); MHD_Response *rsp = MHD_create_response_from_buffer(strlen(buf), (void*) buf, MHD_RESPMEM_MUST_FREE);
return done(connection, status, rsp); return done(connection, status, rsp);
} }
void Httpd::onIdle(uv_idle_t *handle)
{
MHD_run(static_cast<Httpd*>(handle->data)->m_daemon);
}

View file

@ -4,7 +4,7 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2016-2018 XMRig <support@xmrig.com>
* *
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -37,6 +37,7 @@ class Httpd
{ {
public: public:
Httpd(int port, const char *accessToken); Httpd(int port, const char *accessToken);
~Httpd();
bool start(); bool start();
private: private:
@ -44,10 +45,12 @@ private:
static int done(MHD_Connection *connection, int status, MHD_Response *rsp); static int done(MHD_Connection *connection, int status, MHD_Response *rsp);
static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls);
static void onIdle(uv_idle_t *handle);
const char *m_accessToken; const char *m_accessToken;
const int m_port; const int m_port;
MHD_Daemon *m_daemon; MHD_Daemon *m_daemon;
uv_idle_t m_idle;
}; };
#endif /* __HTTPD_H__ */ #endif /* __HTTPD_H__ */