From d3a165f287b6d3e010bf10d1816db972e320fad2 Mon Sep 17 00:00:00 2001 From: HÃ¥vard Pettersen Date: Mon, 13 Feb 2023 13:37:57 +0000 Subject: stop using fastos thread combine cancel with stop such that cancel means stopped before started and therefore never run. drop the started sync point since there is no state we need to pass from the thread to the starter of the thread. --- slobrok/src/tests/mirrorapi/mirrorapi.cpp | 1 + vespalib/src/tests/thread/thread_test.cpp | 2 +- vespalib/src/vespa/vespalib/util/thread.cpp | 47 ++++++++++++----------------- vespalib/src/vespa/vespalib/util/thread.h | 26 +++++----------- 4 files changed, 28 insertions(+), 48 deletions(-) diff --git a/slobrok/src/tests/mirrorapi/mirrorapi.cpp b/slobrok/src/tests/mirrorapi/mirrorapi.cpp index 58af7cd7f78..2ba54fdb9d0 100644 --- a/slobrok/src/tests/mirrorapi/mirrorapi.cpp +++ b/slobrok/src/tests/mirrorapi/mirrorapi.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include LOG_SETUP("mirrorapi_test"); diff --git a/vespalib/src/tests/thread/thread_test.cpp b/vespalib/src/tests/thread/thread_test.cpp index ee4f97c34cc..af1fb626462 100644 --- a/vespalib/src/tests/thread/thread_test.cpp +++ b/vespalib/src/tests/thread/thread_test.cpp @@ -50,7 +50,7 @@ TEST("stop before start") { thread.start(); thread.join(); } - EXPECT_TRUE(agent.started); + EXPECT_TRUE(!agent.started); EXPECT_EQUAL(0, agent.loopCnt); } diff --git a/vespalib/src/vespa/vespalib/util/thread.cpp b/vespalib/src/vespa/vespalib/util/thread.cpp index 82ba441420d..b6a491ee83d 100644 --- a/vespalib/src/vespa/vespalib/util/thread.cpp +++ b/vespalib/src/vespa/vespalib/util/thread.cpp @@ -9,51 +9,40 @@ namespace vespalib { __thread Thread *Thread::_currentThread = nullptr; -Thread::Proxy::Proxy(Thread &parent, Runnable &target, init_fun_t init_fun_in) - : thread(parent), runnable(target), init_fun(std::move(init_fun_in)), - start(), started(), cancel(false) -{ } - void -Thread::Proxy::Run(FastOS_ThreadInterface *, void *) +Thread::run() { assert(_currentThread == nullptr); - _currentThread = &thread; - start.await(); - if (!cancel) { - started.countDown(); - init_fun(runnable); + _currentThread = this; + _start.await(); + if (!stopped()) { + _init_fun(_runnable); } - assert(_currentThread == &thread); + assert(_currentThread == this); _currentThread = nullptr; } -Thread::Proxy::~Proxy() = default; - Thread::Thread(Runnable &runnable, init_fun_t init_fun_in) - : _proxy(*this, runnable, std::move(init_fun_in)), - _pool(1), - _lock(), - _cond(), - _stopped(false), - _woken(false) + : _runnable(runnable), + _init_fun(std::move(init_fun_in)), + _start(), + _lock(), + _cond(), + _stopped(false), + _woken(false), + _thread(&Thread::run, this) { - FastOS_ThreadInterface *thread = _pool.NewThread(&_proxy); - assert(thread != nullptr); - (void)thread; } Thread::~Thread() { - _proxy.cancel = true; - _proxy.start.countDown(); + stop().start(); } void Thread::start() { - _proxy.start.countDown(); - _proxy.started.await(); + _start.countDown(); } Thread & @@ -68,7 +57,9 @@ Thread::stop() void Thread::join() { - _pool.Close(); + if (_thread.joinable()) { + _thread.join(); + } } bool diff --git a/vespalib/src/vespa/vespalib/util/thread.h b/vespalib/src/vespa/vespalib/util/thread.h index 0c7693556c7..c03f1c6e65c 100644 --- a/vespalib/src/vespa/vespalib/util/thread.h +++ b/vespalib/src/vespa/vespalib/util/thread.h @@ -5,8 +5,8 @@ #include "gate.h" #include "runnable.h" #include "active.h" -#include #include +#include namespace vespalib { @@ -17,29 +17,18 @@ class Thread : public Active { private: using init_fun_t = Runnable::init_fun_t; - enum { STACK_SIZE = 256*1024 }; static __thread Thread *_currentThread; - struct Proxy : FastOS_Runnable { - Thread &thread; - Runnable &runnable; - init_fun_t init_fun; - vespalib::Gate start; - vespalib::Gate started; - bool cancel; - - Proxy(Thread &parent, Runnable &target, init_fun_t init_fun_in); - ~Proxy() override; - - void Run(FastOS_ThreadInterface *thisThread, void *arguments) override; - }; - - Proxy _proxy; - FastOS_ThreadPool _pool; + Runnable &_runnable; + init_fun_t _init_fun; + vespalib::Gate _start; std::mutex _lock; std::condition_variable _cond; std::atomic _stopped; bool _woken; + std::jthread _thread; + + void run(); public: Thread(Runnable &runnable, init_fun_t init_fun_in); @@ -56,4 +45,3 @@ public: }; } // namespace vespalib - -- cgit v1.2.3