aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@yahooinc.com>2023-02-13 13:37:57 +0000
committerHåvard Pettersen <havardpe@yahooinc.com>2023-02-13 15:05:13 +0000
commitd3a165f287b6d3e010bf10d1816db972e320fad2 (patch)
treedd8776ff877b27faa73189692131d6ae37fbb854
parent3da5e19509fcc10d4cdbcc49747cacc7cac2ae2e (diff)
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.
-rw-r--r--slobrok/src/tests/mirrorapi/mirrorapi.cpp1
-rw-r--r--vespalib/src/tests/thread/thread_test.cpp2
-rw-r--r--vespalib/src/vespa/vespalib/util/thread.cpp47
-rw-r--r--vespalib/src/vespa/vespalib/util/thread.h26
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 <vespa/fnet/frt/target.h>
#include <vespa/fnet/transport.h>
#include <thread>
+#include <vespa/fastos/thread.h>
#include <vespa/log/log.h>
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 <vespa/fastos/thread.h>
#include <atomic>
+#include <thread>
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<bool> _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
-