aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2018-04-26 11:44:47 +0100
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2018-06-26 22:15:22 +0100
commit2771a18e85bd91dd881f89f6c14d26cba35a1844 (patch)
treeb8f710493bf453812f696b5392b17e6e72663d6f /tests/unit_tests
parentwallet2: remove unneeded divisions (diff)
downloadmonero-2771a18e85bd91dd881f89f6c14d26cba35a1844.tar.xz
threadpool: allow leaf functions to run concurrently
Decrease the number of worker threads by one to account for the fact the calling thread acts as a worker thread now
Diffstat (limited to 'tests/unit_tests')
-rw-r--r--tests/unit_tests/threadpool.cpp57
1 files changed, 51 insertions, 6 deletions
diff --git a/tests/unit_tests/threadpool.cpp b/tests/unit_tests/threadpool.cpp
index 34be1417a..1307cd738 100644
--- a/tests/unit_tests/threadpool.cpp
+++ b/tests/unit_tests/threadpool.cpp
@@ -35,7 +35,7 @@ TEST(threadpool, wait_nothing)
{
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
tools::threadpool::waiter waiter;
- waiter.wait();
+ waiter.wait(tpool.get());
}
TEST(threadpool, wait_waits)
@@ -45,7 +45,7 @@ TEST(threadpool, wait_waits)
std::atomic<bool> b(false);
tpool->submit(&waiter, [&b](){ epee::misc_utils::sleep_no_w(1000); b = true; });
ASSERT_FALSE(b);
- waiter.wait();
+ waiter.wait(tpool.get());
ASSERT_TRUE(b);
}
@@ -59,7 +59,7 @@ TEST(threadpool, one_thread)
{
tpool->submit(&waiter, [&counter](){++counter;});
}
- waiter.wait();
+ waiter.wait(tpool.get());
ASSERT_EQ(counter, 4096);
}
@@ -73,7 +73,7 @@ TEST(threadpool, many_threads)
{
tpool->submit(&waiter, [&counter](){++counter;});
}
- waiter.wait();
+ waiter.wait(tpool.get());
ASSERT_EQ(counter, 4096);
}
@@ -85,7 +85,7 @@ static uint64_t fibonacci(std::shared_ptr<tools::threadpool> tpool, uint64_t n)
tools::threadpool::waiter waiter;
tpool->submit(&waiter, [&tpool, &f1, n](){ f1 = fibonacci(tpool, n-1); });
tpool->submit(&waiter, [&tpool, &f2, n](){ f2 = fibonacci(tpool, n-2); });
- waiter.wait();
+ waiter.wait(tpool.get());
return f1 + f2;
}
@@ -95,7 +95,52 @@ TEST(threadpool, reentrency)
tools::threadpool::waiter waiter;
uint64_t f = fibonacci(tpool, 13);
- waiter.wait();
+ waiter.wait(tpool.get());
ASSERT_EQ(f, 233);
}
+TEST(threadpool, reentrancy)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
+ tools::threadpool::waiter waiter;
+
+ uint64_t f = fibonacci(tpool, 13);
+ waiter.wait(tpool.get());
+ ASSERT_EQ(f, 233);
+}
+
+TEST(threadpool, leaf_throws)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
+ tools::threadpool::waiter waiter;
+
+ bool thrown = false, executed = false;
+ tpool->submit(&waiter, [&](){
+ try { tpool->submit(&waiter, [&](){ executed = true; }); }
+ catch(const std::exception &e) { thrown = true; }
+ }, true);
+ waiter.wait(tpool.get());
+ ASSERT_TRUE(thrown);
+ ASSERT_FALSE(executed);
+}
+
+TEST(threadpool, leaf_reentrancy)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
+ tools::threadpool::waiter waiter;
+
+ std::atomic<int> counter(0);
+ for (int i = 0; i < 1000; ++i)
+ {
+ tpool->submit(&waiter, [&](){
+ tools::threadpool::waiter waiter;
+ for (int j = 0; j < 500; ++j)
+ {
+ tpool->submit(&waiter, [&](){ ++counter; }, true);
+ }
+ waiter.wait(tpool.get());
+ });
+ }
+ waiter.wait(tpool.get());
+ ASSERT_EQ(counter, 500000);
+}