diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-10-14 19:08:55 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2020-10-14 19:08:55 +0000 |
commit | b46c34b9d81cd60c8b01bdc47835262bd96731dc (patch) | |
tree | 9875936d2373cd07966e606351ebad4bf71ea44e /vespalib | |
parent | 02644cdef3183563d0b89cafe31078012ccd1f84 (diff) |
Use c++11 primitives for synchronization
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/util/thread.cpp | 17 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/thread.h | 20 |
2 files changed, 21 insertions, 16 deletions
diff --git a/vespalib/src/vespa/vespalib/util/thread.cpp b/vespalib/src/vespa/vespalib/util/thread.cpp index c595a76b8fe..aa7b8ed3bb3 100644 --- a/vespalib/src/vespa/vespalib/util/thread.cpp +++ b/vespalib/src/vespa/vespalib/util/thread.cpp @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "thread.h" +#include "time.h" #include <thread> #include <cassert> @@ -8,6 +9,11 @@ namespace vespalib { __thread Thread *Thread::_currentThread = nullptr; +Thread::Proxy::Proxy(Thread &parent, Runnable &target) + : thread(parent), runnable(target), + start(), started(), cancel(false) +{ } + void Thread::Proxy::Run(FastOS_ThreadInterface *, void *) { @@ -27,7 +33,8 @@ Thread::Proxy::~Proxy() = default; Thread::Thread(Runnable &runnable) : _proxy(*this, runnable), _pool(STACK_SIZE, 1), - _monitor(), + _lock(), + _cond(), _stopped(false), _woken(false) { @@ -52,9 +59,9 @@ Thread::start() Thread & Thread::stop() { - vespalib::MonitorGuard guard(_monitor); + std::unique_lock guard(_lock); _stopped = true; - guard.broadcast(); + _cond.notify_all(); return *this; } @@ -67,9 +74,9 @@ Thread::join() bool Thread::slumber(double s) { - vespalib::MonitorGuard guard(_monitor); + std::unique_lock guard(_lock); if (!_stopped || _woken) { - if (guard.wait((int)(s * 1000.0))) { + if (_cond.wait_for(guard, from_s(s)) == std::cv_status::no_timeout) { _woken = _stopped; } } else { diff --git a/vespalib/src/vespa/vespalib/util/thread.h b/vespalib/src/vespa/vespalib/util/thread.h index d24a7e6f174..71b81645efa 100644 --- a/vespalib/src/vespa/vespalib/util/thread.h +++ b/vespalib/src/vespa/vespalib/util/thread.h @@ -2,7 +2,6 @@ #pragma once -#include "sync.h" #include "gate.h" #include "runnable.h" #include "active.h" @@ -26,23 +25,22 @@ private: vespalib::Gate started; bool cancel; - Proxy(Thread &parent, Runnable &target) - : thread(parent), runnable(target), - start(), started(), cancel(false) { } - ~Proxy(); + Proxy(Thread &parent, Runnable &target); + ~Proxy() override; void Run(FastOS_ThreadInterface *thisThread, void *arguments) override; }; - Proxy _proxy; - FastOS_ThreadPool _pool; - vespalib::Monitor _monitor; - bool _stopped; - bool _woken; + Proxy _proxy; + FastOS_ThreadPool _pool; + std::mutex _lock; + std::condition_variable _cond; + bool _stopped; + bool _woken; public: Thread(Runnable &runnable); - ~Thread(); + ~Thread() override; void start() override; Thread &stop() override; void join() override; |