New Async wrapper.
This commit is contained in:
parent
6860450147
commit
87b4d97798
7 changed files with 157 additions and 37 deletions
|
@ -19,9 +19,12 @@
|
|||
*/
|
||||
|
||||
#include "base/io/Async.h"
|
||||
#include "base/kernel/interfaces/IAsyncListener.h"
|
||||
#include "base/tools/Handle.h"
|
||||
|
||||
|
||||
#if defined(XMRIG_UV_PERFORMANCE_BUG)
|
||||
// since 2019.05.16, Version 1.29.0 (Stable) https://github.com/xmrig/xmrig/pull/1889
|
||||
#if (UV_VERSION_MAJOR >= 1) && (UV_VERSION_MINOR >= 29) && defined(__linux__)
|
||||
#include <sys/eventfd.h>
|
||||
#include <sys/poll.h>
|
||||
#include <unistd.h>
|
||||
|
@ -31,16 +34,28 @@
|
|||
namespace xmrig {
|
||||
|
||||
|
||||
struct uv_async_t: uv_poll_t
|
||||
{
|
||||
using uv_async_cb = void (*)(uv_async_t *);
|
||||
~uv_async_t();
|
||||
int m_fd = -1;
|
||||
uv_async_cb m_cb = nullptr;
|
||||
};
|
||||
|
||||
|
||||
using uv_async_cb = uv_async_t::uv_async_cb;
|
||||
|
||||
|
||||
uv_async_t::~uv_async_t()
|
||||
{
|
||||
close(m_fd);
|
||||
}
|
||||
|
||||
|
||||
static void on_schedule(uv_poll_t *handle, int status, int events)
|
||||
static void on_schedule(uv_poll_t *handle, int, int)
|
||||
{
|
||||
static uint64_t val;
|
||||
uv_async_t *async = reinterpret_cast<uv_async_t *>(handle);
|
||||
auto async = reinterpret_cast<uv_async_t *>(handle);
|
||||
for (;;) {
|
||||
int r = read(async->m_fd, &val, sizeof(val));
|
||||
|
||||
|
@ -64,7 +79,7 @@ static void on_schedule(uv_poll_t *handle, int status, int events)
|
|||
}
|
||||
|
||||
|
||||
int uv_async_init(uv_loop_t *loop, uv_async_t *async, uv_async_cb cb)
|
||||
static int uv_async_init(uv_loop_t *loop, uv_async_t *async, uv_async_cb cb)
|
||||
{
|
||||
int fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
|
||||
if (fd < 0) {
|
||||
|
@ -78,7 +93,7 @@ int uv_async_init(uv_loop_t *loop, uv_async_t *async, uv_async_cb cb)
|
|||
}
|
||||
|
||||
|
||||
int uv_async_send(uv_async_t *async)
|
||||
static int uv_async_send(uv_async_t *async)
|
||||
{
|
||||
static const uint64_t val = 1;
|
||||
int r;
|
||||
|
@ -96,3 +111,41 @@ int uv_async_send(uv_async_t *async)
|
|||
|
||||
} // namespace xmrig
|
||||
#endif
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class AsyncPrivate
|
||||
{
|
||||
public:
|
||||
IAsyncListener *listener = nullptr;
|
||||
uv_async_t *async = nullptr;
|
||||
};
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
xmrig::Async::Async(IAsyncListener *listener) : d_ptr(new AsyncPrivate())
|
||||
{
|
||||
d_ptr->listener = listener;
|
||||
d_ptr->async = new uv_async_t;
|
||||
d_ptr->async->data = this;
|
||||
|
||||
uv_async_init(uv_default_loop(), d_ptr->async, [](uv_async_t *handle) { static_cast<Async *>(handle->data)->d_ptr->listener->onAsync(); });
|
||||
}
|
||||
|
||||
|
||||
xmrig::Async::~Async()
|
||||
{
|
||||
Handle::close(d_ptr->async);
|
||||
|
||||
delete d_ptr;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Async::send()
|
||||
{
|
||||
uv_async_send(d_ptr->async);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue