Change uv::Async to accept data parameters

This is a breaking change as it makes Async a template (e.g. Async<> must
be used instead of just Async).  When data parameters are provided, an
internal mutex and vector is used to hold the parameter packs until the loop
runs.
This commit is contained in:
Peter Johnson
2018-08-06 11:08:17 -07:00
parent 4a3e43d4a7
commit 1de1900dbb
3 changed files with 156 additions and 5 deletions

View File

@@ -51,7 +51,7 @@ TEST(UvAsync, Test) {
std::thread theThread;
auto loop = Loop::Create();
auto async = Async::Create(loop);
auto async = Async<>::Create(loop);
auto prepare = Prepare::Create(loop);
loop->error.connect([](Error) { FAIL(); });
@@ -101,5 +101,78 @@ TEST(UvAsync, Test) {
if (theThread.joinable()) theThread.join();
}
TEST(UvAsync, Data) {
int prepare_cb_called = 0;
int async_cb_called[2] = {0, 0};
int close_cb_called = 0;
std::thread theThread;
auto loop = Loop::Create();
auto async = Async<int, std::function<void(int)>>::Create(loop);
auto prepare = Prepare::Create(loop);
loop->error.connect([](Error) { FAIL(); });
prepare->error.connect([](Error) { FAIL(); });
prepare->prepare.connect([&] {
if (prepare_cb_called++) return;
theThread = std::thread([&] {
async->Send(0, [&](int v) {
ASSERT_EQ(v, 0);
++async_cb_called[0];
});
async->Send(1, [&](int v) {
ASSERT_EQ(v, 1);
++async_cb_called[1];
async->Close();
prepare->Close();
});
});
});
prepare->Start();
async->error.connect([](Error) { FAIL(); });
async->closed.connect([&] { close_cb_called++; });
async->wakeup.connect([&](int v, std::function<void(int)> f) { f(v); });
loop->Run();
ASSERT_EQ(async_cb_called[0], 1);
ASSERT_EQ(async_cb_called[1], 1);
ASSERT_EQ(close_cb_called, 1);
if (theThread.joinable()) theThread.join();
}
TEST(UvAsync, DataRef) {
int prepare_cb_called = 0;
int val = 0;
std::thread theThread;
auto loop = Loop::Create();
auto async = Async<int, int&>::Create(loop);
auto prepare = Prepare::Create(loop);
prepare->prepare.connect([&] {
if (prepare_cb_called++) return;
theThread = std::thread([&] { async->Send(1, val); });
});
prepare->Start();
async->wakeup.connect([&](int v, int& r) {
r = v;
async->Close();
prepare->Close();
});
loop->Run();
ASSERT_EQ(val, 1);
if (theThread.joinable()) theThread.join();
}
} // namespace uv
} // namespace wpi