New Async wrapper.
This commit is contained in:
parent
6860450147
commit
87b4d97798
7 changed files with 157 additions and 37 deletions
|
@ -4,6 +4,7 @@ set(HEADERS_BASE
|
|||
src/base/crypto/Coin.h
|
||||
src/base/crypto/keccak.h
|
||||
src/base/crypto/sha3.h
|
||||
src/base/io/Async.h
|
||||
src/base/io/Console.h
|
||||
src/base/io/Env.h
|
||||
src/base/io/json/Json.h
|
||||
|
@ -21,6 +22,7 @@ set(HEADERS_BASE
|
|||
src/base/kernel/config/BaseTransform.h
|
||||
src/base/kernel/config/Title.h
|
||||
src/base/kernel/Entry.h
|
||||
src/base/kernel/interfaces/IAsyncListener.h
|
||||
src/base/kernel/interfaces/IBaseListener.h
|
||||
src/base/kernel/interfaces/IClient.h
|
||||
src/base/kernel/interfaces/IClientListener.h
|
||||
|
@ -73,6 +75,7 @@ set(SOURCES_BASE
|
|||
src/base/crypto/Coin.cpp
|
||||
src/base/crypto/keccak.cpp
|
||||
src/base/crypto/sha3.cpp
|
||||
src/base/io/Async.cpp
|
||||
src/base/io/Console.cpp
|
||||
src/base/io/Env.cpp
|
||||
src/base/io/json/Json.cpp
|
||||
|
@ -127,7 +130,6 @@ elseif (APPLE)
|
|||
)
|
||||
else()
|
||||
set(SOURCES_OS
|
||||
src/base/io/Async.cpp
|
||||
src/base/io/json/Json_unix.cpp
|
||||
src/base/kernel/Platform_unix.cpp
|
||||
)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
#define XMRIG_ASYNC_H
|
||||
|
||||
|
||||
#include <uv.h>
|
||||
#include "base/tools/Object.h"
|
||||
|
||||
|
||||
// since 2019.05.16, Version 1.29.0 (Stable)
|
||||
|
@ -49,4 +49,29 @@ extern int uv_async_send(uv_async_t *async);
|
|||
#endif
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class AsyncPrivate;
|
||||
class IAsyncListener;
|
||||
|
||||
|
||||
class Async
|
||||
{
|
||||
public:
|
||||
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Async)
|
||||
|
||||
Async(IAsyncListener *listener);
|
||||
~Async();
|
||||
|
||||
void send();
|
||||
|
||||
private:
|
||||
AsyncPrivate *d_ptr;
|
||||
};
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
#endif /* XMRIG_ASYNC_H */
|
||||
|
|
47
src/base/kernel/interfaces/IAsyncListener.h
Normal file
47
src/base/kernel/interfaces/IAsyncListener.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/* XMRig
|
||||
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_IASYNCLISTENER_H
|
||||
#define XMRIG_IASYNCLISTENER_H
|
||||
|
||||
|
||||
#include "base/tools/Object.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class Async;
|
||||
|
||||
|
||||
class IAsyncListener
|
||||
{
|
||||
public:
|
||||
XMRIG_DISABLE_COPY_MOVE(IAsyncListener)
|
||||
|
||||
IAsyncListener() = default;
|
||||
virtual ~IAsyncListener() = default;
|
||||
|
||||
virtual void onAsync() = 0;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif // XMRIG_IASYNCLISTENER_H
|
Loading…
Add table
Add a link
Reference in a new issue