From 98e408c667e3dcda3dc177594ffa4c4d0d99a33e Mon Sep 17 00:00:00 2001 From: HÃ¥vard Pettersen Date: Tue, 14 Feb 2023 10:15:12 +0000 Subject: stop using fastos thread more places - also stop using std::jthread - remove Active and Joinable interfaces - remove stop, stopped and slumber - remove currentThread - make start function static - override start for Runnable w/init or custom function - explicit stop/slumber where needed --- .../simple_thread_bundle_test.cpp | 1 + .../tests/singleexecutor/singleexecutor_test.cpp | 1 + vespalib/src/tests/thread/thread_test.cpp | 48 +++++++++------------- 3 files changed, 22 insertions(+), 28 deletions(-) (limited to 'vespalib/src/tests') diff --git a/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp b/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp index 59aeddfe8ca..e451f1e033d 100644 --- a/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp +++ b/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include diff --git a/vespalib/src/tests/singleexecutor/singleexecutor_test.cpp b/vespalib/src/tests/singleexecutor/singleexecutor_test.cpp index 56352ff3c0d..3b1d244eb13 100644 --- a/vespalib/src/tests/singleexecutor/singleexecutor_test.cpp +++ b/vespalib/src/tests/singleexecutor/singleexecutor_test.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include using namespace vespalib; diff --git a/vespalib/src/tests/thread/thread_test.cpp b/vespalib/src/tests/thread/thread_test.cpp index af1fb626462..f7e8753fd86 100644 --- a/vespalib/src/tests/thread/thread_test.cpp +++ b/vespalib/src/tests/thread/thread_test.cpp @@ -2,56 +2,48 @@ #include #include -#include using namespace vespalib; VESPA_THREAD_STACK_TAG(test_agent_thread); struct Agent : public Runnable { - bool started; - int loopCnt; - Agent() : started(false), loopCnt(0) {} + bool was_run; + Agent() : was_run(false) {} void run() override { - started = true; - Thread &thread = Thread::currentThread(); - while (thread.slumber(60.0)) { - ++loopCnt; - } + was_run = true; } }; -TEST("thread never started") { +void my_fun(bool *was_run) { + *was_run = true; +} + +TEST("run vespalib::Runnable with init function") { Agent agent; { - Thread thread(agent, test_agent_thread); + auto thread = Thread::start(agent, test_agent_thread); } - EXPECT_TRUE(!agent.started); - EXPECT_EQUAL(0, agent.loopCnt); + EXPECT_TRUE(agent.was_run); } -TEST("normal operation") { - Agent agent; +TEST("run custom function") { + bool was_run = false; { - Thread thread(agent, test_agent_thread); - thread.start(); - std::this_thread::sleep_for(20ms); - thread.stop().join(); + auto thread = Thread::start(my_fun, &was_run); } - EXPECT_TRUE(agent.started); - EXPECT_EQUAL(0, agent.loopCnt); + EXPECT_TRUE(was_run); } -TEST("stop before start") { - Agent agent; +TEST("join multiple times (including destructor)") { + bool was_run = false; { - Thread thread(agent, test_agent_thread); - thread.stop(); - thread.start(); + auto thread = Thread::start(my_fun, &was_run); + thread.join(); + thread.join(); thread.join(); } - EXPECT_TRUE(!agent.started); - EXPECT_EQUAL(0, agent.loopCnt); + EXPECT_TRUE(was_run); } TEST_MAIN() { TEST_RUN_ALL(); } -- cgit v1.2.3