Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework nano::thread_pool #4762

Merged
merged 9 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nano/core_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ add_executable(
socket.cpp
system.cpp
telemetry.cpp
thread_pool.cpp
throttle.cpp
toml.cpp
timer.cpp
Expand Down
96 changes: 96 additions & 0 deletions nano/core_test/thread_pool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <nano/lib/thread_pool.hpp>
#include <nano/lib/timer.hpp>
#include <nano/test_common/testutil.hpp>

#include <gtest/gtest.h>

#include <future>

TEST (thread_pool, thread_pool)
{
std::atomic<bool> passed_sleep{ false };

auto func = [&passed_sleep] () {
std::this_thread::sleep_for (std::chrono::seconds (1));
passed_sleep = true;
};

nano::thread_pool workers (1u, nano::thread_role::name::unknown);
nano::test::start_stop_guard stop_guard{ workers };
workers.post (func);
ASSERT_FALSE (passed_sleep);

nano::timer<std::chrono::milliseconds> timer_l;
timer_l.start ();
while (!passed_sleep)
{
if (timer_l.since_start () > std::chrono::seconds (10))
{
break;
}
}
ASSERT_TRUE (passed_sleep);
}

TEST (thread_pool, one)
{
std::atomic<bool> done (false);
nano::mutex mutex;
nano::condition_variable condition;
nano::thread_pool workers (1u, nano::thread_role::name::unknown);
nano::test::start_stop_guard stop_guard{ workers };
workers.post ([&] () {
{
nano::lock_guard<nano::mutex> lock{ mutex };
done = true;
}
condition.notify_one ();
});
nano::unique_lock<nano::mutex> unique{ mutex };
condition.wait (unique, [&] () { return !!done; });
}

TEST (thread_pool, many)
{
std::atomic<int> count (0);
nano::mutex mutex;
nano::condition_variable condition;
nano::thread_pool workers (50u, nano::thread_role::name::unknown);
nano::test::start_stop_guard stop_guard{ workers };
for (auto i (0); i < 50; ++i)
{
workers.post ([&] () {
{
nano::lock_guard<nano::mutex> lock{ mutex };
count += 1;
}
condition.notify_one ();
});
}
nano::unique_lock<nano::mutex> unique{ mutex };
condition.wait (unique, [&] () { return count == 50; });
}

TEST (thread_pool, top_execution)
{
int value1 (0);
int value2 (0);
nano::mutex mutex;
std::promise<bool> promise;
nano::thread_pool workers (1u, nano::thread_role::name::unknown);
nano::test::start_stop_guard stop_guard{ workers };
workers.post ([&] () {
nano::lock_guard<nano::mutex> lock{ mutex };
value1 = 1;
value2 = 1;
});
workers.post_delayed (std::chrono::milliseconds (1), [&] () {
nano::lock_guard<nano::mutex> lock{ mutex };
value2 = 2;
promise.set_value (false);
});
promise.get_future ().get ();
nano::lock_guard<nano::mutex> lock{ mutex };
ASSERT_EQ (1, value1);
ASSERT_EQ (2, value2);
}
87 changes: 1 addition & 86 deletions nano/core_test/utility.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <nano/lib/optional_ptr.hpp>
#include <nano/lib/rate_limiting.hpp>
#include <nano/lib/thread_pool.hpp>
#include <nano/lib/relaxed_atomic.hpp>
#include <nano/lib/timer.hpp>
#include <nano/lib/utility.hpp>
#include <nano/secure/pending_info.hpp>
Expand Down Expand Up @@ -146,91 +146,6 @@ TEST (optional_ptr, basic)
ASSERT_EQ (opt->z, 3);
}

TEST (thread, thread_pool)
{
std::atomic<bool> passed_sleep{ false };

auto func = [&passed_sleep] () {
std::this_thread::sleep_for (std::chrono::seconds (1));
passed_sleep = true;
};

nano::thread_pool workers (1u, nano::thread_role::name::unknown);
workers.push_task (func);
ASSERT_FALSE (passed_sleep);

nano::timer<std::chrono::milliseconds> timer_l;
timer_l.start ();
while (!passed_sleep)
{
if (timer_l.since_start () > std::chrono::seconds (10))
{
break;
}
}
ASSERT_TRUE (passed_sleep);
}

TEST (thread_pool_alarm, one)
{
std::atomic<bool> done (false);
nano::mutex mutex;
nano::condition_variable condition;
nano::thread_pool workers (1u, nano::thread_role::name::unknown);
workers.add_timed_task (std::chrono::steady_clock::now (), [&] () {
{
nano::lock_guard<nano::mutex> lock{ mutex };
done = true;
}
condition.notify_one ();
});
nano::unique_lock<nano::mutex> unique{ mutex };
condition.wait (unique, [&] () { return !!done; });
}

TEST (thread_pool_alarm, many)
{
std::atomic<int> count (0);
nano::mutex mutex;
nano::condition_variable condition;
nano::thread_pool workers (50u, nano::thread_role::name::unknown);
for (auto i (0); i < 50; ++i)
{
workers.add_timed_task (std::chrono::steady_clock::now (), [&] () {
{
nano::lock_guard<nano::mutex> lock{ mutex };
count += 1;
}
condition.notify_one ();
});
}
nano::unique_lock<nano::mutex> unique{ mutex };
condition.wait (unique, [&] () { return count == 50; });
}

TEST (thread_pool_alarm, top_execution)
{
int value1 (0);
int value2 (0);
nano::mutex mutex;
std::promise<bool> promise;
nano::thread_pool workers (1u, nano::thread_role::name::unknown);
workers.add_timed_task (std::chrono::steady_clock::now (), [&] () {
nano::lock_guard<nano::mutex> lock{ mutex };
value1 = 1;
value2 = 1;
});
workers.add_timed_task (std::chrono::steady_clock::now () + std::chrono::milliseconds (1), [&] () {
nano::lock_guard<nano::mutex> lock{ mutex };
value2 = 2;
promise.set_value (false);
});
promise.get_future ().get ();
nano::lock_guard<nano::mutex> lock{ mutex };
ASSERT_EQ (1, value1);
ASSERT_EQ (2, value2);
}

TEST (filesystem, remove_all_files)
{
auto path = nano::unique_path ();
Expand Down
1 change: 0 additions & 1 deletion nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ add_library(
stats_sinks.hpp
stream.hpp
thread_pool.hpp
thread_pool.cpp
thread_roles.hpp
thread_roles.cpp
thread_runner.hpp
Expand Down
97 changes: 0 additions & 97 deletions nano/lib/thread_pool.cpp

This file was deleted.

Loading
Loading