diff options
22 files changed, 136 insertions, 116 deletions
diff --git a/filedistribution/src/vespa/filedistribution/model/zkfacade.cpp b/filedistribution/src/vespa/filedistribution/model/zkfacade.cpp index e07f0684584..de410289ec0 100644 --- a/filedistribution/src/vespa/filedistribution/model/zkfacade.cpp +++ b/filedistribution/src/vespa/filedistribution/model/zkfacade.cpp @@ -4,7 +4,7 @@ #include <vespa/vespalib/net/socket_address.h> #include <vespa/filedistribution/common/logfwd.h> #include <vespa/defaults.h> -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> #include <vespa/vespalib/text/stringtokenizer.h> #include <zookeeper/zookeeper.h> #include <sstream> diff --git a/messagebus/src/vespa/messagebus/messagebus.cpp b/messagebus/src/vespa/messagebus/messagebus.cpp index fa646b55221..fd1ad2908c7 100644 --- a/messagebus/src/vespa/messagebus/messagebus.cpp +++ b/messagebus/src/vespa/messagebus/messagebus.cpp @@ -8,6 +8,7 @@ #include "protocolrepository.h" #include <vespa/messagebus/network/inetwork.h> #include <vespa/vespalib/util/exceptions.h> +#include <vespa/vespalib/util/gate.h> #include <vespa/log/log.h> LOG_SETUP(".messagebus"); diff --git a/messagebus/src/vespa/messagebus/messenger.cpp b/messagebus/src/vespa/messagebus/messenger.cpp index 4b612b66c31..63df7f2f482 100644 --- a/messagebus/src/vespa/messagebus/messenger.cpp +++ b/messagebus/src/vespa/messagebus/messenger.cpp @@ -2,6 +2,7 @@ #include "messenger.h" #include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> #include <vespa/log/log.h> LOG_SETUP(".messenger"); diff --git a/searchcore/src/tests/proton/attribute/exclusive_attribute_read_accessor/exclusive_attribute_read_accessor_test.cpp b/searchcore/src/tests/proton/attribute/exclusive_attribute_read_accessor/exclusive_attribute_read_accessor_test.cpp index b8a3cceb153..6d69d9b225b 100644 --- a/searchcore/src/tests/proton/attribute/exclusive_attribute_read_accessor/exclusive_attribute_read_accessor_test.cpp +++ b/searchcore/src/tests/proton/attribute/exclusive_attribute_read_accessor/exclusive_attribute_read_accessor_test.cpp @@ -5,7 +5,7 @@ #include <vespa/searchcommon/attribute/config.h> #include <vespa/searchlib/attribute/attributefactory.h> #include <vespa/searchlib/common/sequencedtaskexecutor.h> -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> using namespace proton; using namespace search; diff --git a/searchcore/src/tests/proton/documentdb/job_tracked_maintenance_job/job_tracked_maintenance_job_test.cpp b/searchcore/src/tests/proton/documentdb/job_tracked_maintenance_job/job_tracked_maintenance_job_test.cpp index 0a4b83350ba..c1626e94809 100644 --- a/searchcore/src/tests/proton/documentdb/job_tracked_maintenance_job/job_tracked_maintenance_job_test.cpp +++ b/searchcore/src/tests/proton/documentdb/job_tracked_maintenance_job/job_tracked_maintenance_job_test.cpp @@ -7,7 +7,7 @@ LOG_SETUP("job_tracked_maintenance_test"); #include <vespa/searchcore/proton/test/simple_job_tracker.h> #include <vespa/vespalib/testkit/testapp.h> #include <vespa/vespalib/util/closuretask.h> -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> #include <vespa/vespalib/util/threadstackexecutor.h> using namespace proton; diff --git a/searchcore/src/tests/proton/documentdb/maintenancecontroller/maintenancecontroller_test.cpp b/searchcore/src/tests/proton/documentdb/maintenancecontroller/maintenancecontroller_test.cpp index 0f69684373c..2de13545beb 100644 --- a/searchcore/src/tests/proton/documentdb/maintenancecontroller/maintenancecontroller_test.cpp +++ b/searchcore/src/tests/proton/documentdb/maintenancecontroller/maintenancecontroller_test.cpp @@ -32,7 +32,7 @@ #include <vespa/vespalib/data/slime/slime.h> #include <vespa/vespalib/testkit/testapp.h> #include <vespa/vespalib/util/closuretask.h> -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> #include <vespa/vespalib/util/threadstackexecutor.h> #include <vespa/log/log.h> diff --git a/searchcore/src/tests/proton/metrics/job_tracked_flush/job_tracked_flush_test.cpp b/searchcore/src/tests/proton/metrics/job_tracked_flush/job_tracked_flush_test.cpp index a0dd3ee6214..4ed01f8caf7 100644 --- a/searchcore/src/tests/proton/metrics/job_tracked_flush/job_tracked_flush_test.cpp +++ b/searchcore/src/tests/proton/metrics/job_tracked_flush/job_tracked_flush_test.cpp @@ -9,7 +9,7 @@ LOG_SETUP("job_tracked_flush_test"); #include <vespa/vespalib/testkit/testapp.h> #include <vespa/vespalib/util/closuretask.h> #include <vespa/vespalib/util/threadstackexecutor.h> -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> using namespace proton; using namespace searchcorespi; @@ -17,7 +17,6 @@ using search::SerialNum; using test::SimpleJobTracker; using vespalib::makeTask; using vespalib::makeClosure; -using vespalib::CountDownLatch; using vespalib::Gate; using vespalib::ThreadStackExecutor; diff --git a/searchcore/src/vespa/searchcore/proton/attribute/exclusive_attribute_read_accessor.cpp b/searchcore/src/vespa/searchcore/proton/attribute/exclusive_attribute_read_accessor.cpp index c2c26e52a21..d9a0ff3d8dd 100644 --- a/searchcore/src/vespa/searchcore/proton/attribute/exclusive_attribute_read_accessor.cpp +++ b/searchcore/src/vespa/searchcore/proton/attribute/exclusive_attribute_read_accessor.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 "exclusive_attribute_read_accessor.h" +#include <vespa/vespalib/util/gate.h> #include <vespa/searchlib/attribute/attributevector.h> #include <vespa/searchlib/common/isequencedtaskexecutor.h> diff --git a/searchcore/src/vespa/searchcore/proton/persistenceengine/transport_latch.h b/searchcore/src/vespa/searchcore/proton/persistenceengine/transport_latch.h index 27b9ffa9b88..2fb55b14fdf 100644 --- a/searchcore/src/vespa/searchcore/proton/persistenceengine/transport_latch.h +++ b/searchcore/src/vespa/searchcore/proton/persistenceengine/transport_latch.h @@ -4,7 +4,7 @@ #include <vespa/persistence/spi/result.h> #include <vespa/searchcore/proton/common/feedtoken.h> #include <vespa/vespalib/util/sequence.h> -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/count_down_latch.h> #include <mutex> namespace proton { diff --git a/searchcore/src/vespa/searchcore/proton/server/lid_space_compaction_job.cpp b/searchcore/src/vespa/searchcore/proton/server/lid_space_compaction_job.cpp index 2ae8d826ebc..fad00fa00e6 100644 --- a/searchcore/src/vespa/searchcore/proton/server/lid_space_compaction_job.cpp +++ b/searchcore/src/vespa/searchcore/proton/server/lid_space_compaction_job.cpp @@ -6,7 +6,7 @@ #include "lid_space_compaction_job.h" #include <vespa/searchcore/proton/common/eventlogger.h> #include <vespa/searchlib/common/gatecallback.h> -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> #include <cassert> #include <vespa/log/log.h> diff --git a/searchcore/src/vespa/searchcore/proton/server/packetwrapper.h b/searchcore/src/vespa/searchcore/proton/server/packetwrapper.h index 118f9601abd..6224b3b693a 100644 --- a/searchcore/src/vespa/searchcore/proton/server/packetwrapper.h +++ b/searchcore/src/vespa/searchcore/proton/server/packetwrapper.h @@ -4,7 +4,7 @@ #include "tls_replay_progress.h" #include <vespa/searchlib/transactionlog/common.h> -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> namespace proton { /** diff --git a/searchcore/src/vespa/searchcore/proton/test/simple_job_tracker.h b/searchcore/src/vespa/searchcore/proton/test/simple_job_tracker.h index a80eb7b670e..648ca0aa8ad 100644 --- a/searchcore/src/vespa/searchcore/proton/test/simple_job_tracker.h +++ b/searchcore/src/vespa/searchcore/proton/test/simple_job_tracker.h @@ -2,7 +2,7 @@ #pragma once #include <vespa/searchcore/proton/metrics/i_job_tracker.h> -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/count_down_latch.h> namespace proton { namespace test { diff --git a/searchlib/src/vespa/searchlib/common/gatecallback.cpp b/searchlib/src/vespa/searchlib/common/gatecallback.cpp index a853909be71..29346d7ad9c 100644 --- a/searchlib/src/vespa/searchlib/common/gatecallback.cpp +++ b/searchlib/src/vespa/searchlib/common/gatecallback.cpp @@ -1,7 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "gatecallback.h" -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> namespace search { diff --git a/vbench/src/vbench/core/dispatcher.h b/vbench/src/vbench/core/dispatcher.h index 6336df17965..212cf04a06e 100644 --- a/vbench/src/vbench/core/dispatcher.h +++ b/vbench/src/vbench/core/dispatcher.h @@ -6,6 +6,7 @@ #include "provider.h" #include "closeable.h" #include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> #include <vector> namespace vbench { diff --git a/vespalib/src/vespa/vespalib/testkit/test_hook.h b/vespalib/src/vespa/vespalib/testkit/test_hook.h index 336e965b0b1..28419c0b31b 100644 --- a/vespalib/src/vespa/vespalib/testkit/test_hook.h +++ b/vespalib/src/vespa/vespalib/testkit/test_hook.h @@ -2,7 +2,7 @@ #pragma once -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/count_down_latch.h> #include <vespa/vespalib/util/barrier.h> #include <string> #include <vector> diff --git a/vespalib/src/vespa/vespalib/testkit/time_bomb.h b/vespalib/src/vespa/vespalib/testkit/time_bomb.h index 5b39e27db79..8412e4b8661 100644 --- a/vespalib/src/vespa/vespalib/testkit/time_bomb.h +++ b/vespalib/src/vespa/vespalib/testkit/time_bomb.h @@ -2,7 +2,7 @@ #pragma once -#include <vespa/vespalib/util/sync.h> +#include <vespa/vespalib/util/gate.h> #include <thread> namespace vespalib { diff --git a/vespalib/src/vespa/vespalib/util/count_down_latch.h b/vespalib/src/vespa/vespalib/util/count_down_latch.h new file mode 100644 index 00000000000..66ef1e44cee --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/count_down_latch.h @@ -0,0 +1,95 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <mutex> +#include <condition_variable> +#include <chrono> + +namespace vespalib { + +/** + * A countdown latch helps one or more threads wait for the completion + * of a number of operations performed by other threads. Specifically, + * any thread invoking the await method will block until the countDown + * method has been invoked an appropriate number of times. The + * countdown latch is created with a count. Each invocation of + * countDown will reduce the current count. When the count reaches 0, + * the threads blocked in await will be unblocked. When the count is + * 0, additional invocations of await will not block and additional + * invocations of countDown will have no effect. + **/ +class CountDownLatch +{ +private: + std::mutex _lock; + std::condition_variable _cond; + uint32_t _count; + + CountDownLatch(const CountDownLatch &rhs) = delete; + CountDownLatch(CountDownLatch &&rhs) = delete; + CountDownLatch &operator=(const CountDownLatch &rhs) = delete; + CountDownLatch &operator=(CountDownLatch &&rhs) = delete; + +public: + /** + * Create a countdown latch with the given initial count. + * + * @param cnt initial count + **/ + CountDownLatch(uint32_t cnt) : _lock(), _cond(), _count(cnt) {} + + /** + * Count down this latch. When the count reaches 0, all threads + * blocked in the await method will be unblocked. + **/ + void countDown() { + std::lock_guard<std::mutex> guard(_lock); + if (_count != 0) { + --_count; + if (_count == 0) { + _cond.notify_all(); + } + } + } + + /** + * Wait for this latch to count down to 0. This method will block + * until the countDown method has been invoked enough times to + * reduce the count to 0. + **/ + void await() { + std::unique_lock<std::mutex> guard(_lock); + _cond.wait(guard, [this]() { return (_count == 0); }); + } + + /** + * Wait for this latch to count down to 0. This method will block + * until the countDown method has been invoked enough times to + * reduce the count to 0 or the given amount of time has elapsed. + * + * @param maxwait the maximum number of milliseconds to wait + * @return true if the counter reached 0, false if we timed out + **/ + bool await(int maxwait) { + auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(maxwait); + std::unique_lock<std::mutex> guard(_lock); + return _cond.wait_until(guard, deadline, [this]() { return (_count == 0); }); + } + + /** + * Obtain the current count for this latch. This method is mostly + * useful for debugging and testing. + * + * @return current count + **/ + uint32_t getCount() const { return _count; } + + /** + * Empty. Needs to be virtual to reduce compiler warnings. + **/ + virtual ~CountDownLatch() = default; +}; + +} // namespace vespalib + diff --git a/vespalib/src/vespa/vespalib/util/gate.h b/vespalib/src/vespa/vespalib/util/gate.h new file mode 100644 index 00000000000..7d913a7a039 --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/gate.h @@ -0,0 +1,22 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "count_down_latch.h" + +namespace vespalib { + +/** + * A gate is a countdown latch with an initial count of 1, indicating + * that we are only waiting for a single operation to complete. + **/ +class Gate : public CountDownLatch +{ +public: + /** + * Sets the initial count to 1. + **/ + Gate() : CountDownLatch(1) {} +}; + +} // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/util/simple_thread_bundle.h b/vespalib/src/vespa/vespalib/util/simple_thread_bundle.h index 8fb7e1d04fc..4fa73c1112f 100644 --- a/vespalib/src/vespa/vespalib/util/simple_thread_bundle.h +++ b/vespalib/src/vespa/vespalib/util/simple_thread_bundle.h @@ -3,6 +3,7 @@ #pragma once #include "sync.h" +#include "count_down_latch.h" #include "thread.h" #include "runnable.h" #include "thread_bundle.h" diff --git a/vespalib/src/vespa/vespalib/util/sync.h b/vespalib/src/vespa/vespalib/util/sync.h index c3d8ea86aec..e3f3d44122c 100644 --- a/vespalib/src/vespa/vespalib/util/sync.h +++ b/vespalib/src/vespa/vespalib/util/sync.h @@ -527,108 +527,5 @@ public: } }; - -/** - * A countdown latch helps one or more threads wait for the completion - * of a number of operations performed by other threads. Specifically, - * any thread invoking the await method will block until the countDown - * method has been invoked an appropriate number of times. The - * countdown latch is created with a count. Each invocation of - * countDown will reduce the current count. When the count reaches 0, - * the threads blocked in await will be unblocked. When the count is - * 0, additional invocations of await will not block and additional - * invocations of countDown will have no effect. - **/ -class CountDownLatch -{ -private: - Monitor _monitor; - uint32_t _count; - - CountDownLatch(const CountDownLatch &rhs) = delete; - CountDownLatch &operator=(const CountDownLatch &rhs) = delete; - -public: - /** - * Create a countdown latch with the given initial count. - * - * @param cnt initial count - **/ - CountDownLatch(uint32_t cnt) : _monitor(), _count(cnt) {} - - /** - * Count down this latch. When the count reaches 0, all threads - * blocked in the await method will be unblocked. - **/ - void countDown() { - MonitorGuard guard(_monitor); - if (_count == 0) { - return; - } - --_count; - if (_count == 0) { - guard.broadcast(); - } - } - - /** - * Wait for this latch to count down to 0. This method will block - * until the countDown method has been invoked enough times to - * reduce the count to 0. - **/ - void await() { - MonitorGuard guard(_monitor); - while (_count != 0) { - guard.wait(); - } - } - - /** - * Wait for this latch to count down to 0. This method will block - * until the countDown method has been invoked enough times to - * reduce the count to 0 or the given amount of time has elapsed. - * - * @param maxwait the maximum number of milliseconds to wait - * @return true if the counter reached 0, false if we timed out - **/ - bool await(int maxwait) { - MonitorGuard guard(_monitor); - TimedWaiter waiter(guard, maxwait); - while (_count != 0 && waiter.hasTime()) { - waiter.wait(); - } - return (_count == 0); - } - - /** - * Obtain the current count for this latch. This method is mostly - * useful for debugging and testing. - * - * @return current count - **/ - uint32_t getCount() const { - return _count; - } - - /** - * Empty. Needs to be virtual to reduce compiler warnings. - **/ - virtual ~CountDownLatch() = default; -}; - - -/** - * A gate is a countdown latch with an initial count of 1, indicating - * that we are only waiting for a single operation to complete. - **/ -class Gate : public CountDownLatch -{ -public: - /** - * Sets the initial count to 1. - **/ - Gate() : CountDownLatch(1) {} -}; - } // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/util/thread.h b/vespalib/src/vespa/vespalib/util/thread.h index 426057be85a..d24a7e6f174 100644 --- a/vespalib/src/vespa/vespalib/util/thread.h +++ b/vespalib/src/vespa/vespalib/util/thread.h @@ -3,6 +3,7 @@ #pragma once #include "sync.h" +#include "gate.h" #include "runnable.h" #include "active.h" #include <vespa/fastos/thread.h> diff --git a/vespalib/src/vespa/vespalib/util/threadstackexecutorbase.h b/vespalib/src/vespa/vespalib/util/threadstackexecutorbase.h index 5048a9c0436..1d8545781f5 100644 --- a/vespalib/src/vespa/vespalib/util/threadstackexecutorbase.h +++ b/vespalib/src/vespa/vespalib/util/threadstackexecutorbase.h @@ -6,6 +6,7 @@ #include "eventbarrier.hpp" #include "arrayqueue.hpp" #include "sync.h" +#include "gate.h" #include "runnable.h" #include <memory> #include <vector> |