diff options
author | Håvard Pettersen <havardpe@yahooinc.com> | 2022-09-12 12:31:16 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@yahooinc.com> | 2022-09-12 12:45:52 +0000 |
commit | 19a095712f39cc68477321ba705010f1235bd601 (patch) | |
tree | ee54aaaf7108ecf1c4f7a2db63e0bd1e6320fb9d /vespalib | |
parent | 33c97cd5a14070178a1499fb7c3abe2e00e663fa (diff) |
thread bundle now available when calculating the global filter
Diffstat (limited to 'vespalib')
3 files changed, 31 insertions, 0 deletions
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 5d69a18112a..debb34724f6 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 @@ -89,6 +89,17 @@ TEST_FF("require that having too many targets fails", SimpleThreadBundle(1), Sta f2.check(Box<size_t>().add(0).add(0)); } +TEST_F("require that ThreadBundle::trivial works the same as SimpleThreadBundle(1)", State(2)) { + ThreadBundle &bundle = ThreadBundle::trivial(); + EXPECT_EQUAL(bundle.size(), 1u); + bundle.run(f.getTargets(0)); + f.check({0,0}); + bundle.run(f.getTargets(1)); + f.check({1,0}); + EXPECT_EXCEPTION(bundle.run(f.getTargets(2)), IllegalArgumentException, ""); + f.check({1,0}); +} + TEST_FF("require that bundles with multiple internal threads work", SimpleThreadBundle(3), State(3)) { f1.run(f2.getTargets(3)); f2.check(Box<size_t>().add(1).add(1).add(1)); diff --git a/vespalib/src/vespa/vespalib/util/thread_bundle.cpp b/vespalib/src/vespa/vespalib/util/thread_bundle.cpp index 7068224229e..9f953abfce7 100644 --- a/vespalib/src/vespa/vespalib/util/thread_bundle.cpp +++ b/vespalib/src/vespa/vespalib/util/thread_bundle.cpp @@ -1,7 +1,24 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "thread_bundle.h" +#include "exceptions.h" namespace vespalib { +ThreadBundle & +ThreadBundle::trivial() { + struct TrivialThreadBundle : ThreadBundle { + size_t size() const override { return 1; } + void run(const std::vector<Runnable*> &targets) override { + if (targets.size() == 1) { + targets[0]->run(); + } else if (targets.size() > 1) { + throw IllegalArgumentException("too many targets"); + } + }; + }; + static TrivialThreadBundle trivial_thread_bundle; + return trivial_thread_bundle; +} + } // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/util/thread_bundle.h b/vespalib/src/vespa/vespalib/util/thread_bundle.h index 32b7d32b2a7..699fd8e27a0 100644 --- a/vespalib/src/vespa/vespalib/util/thread_bundle.h +++ b/vespalib/src/vespa/vespalib/util/thread_bundle.h @@ -33,6 +33,9 @@ struct ThreadBundle { * Empty virtual destructor to enable subclassing. **/ virtual ~ThreadBundle() {} + + // a thread bundle that can only run things in the current thread. + static ThreadBundle &trivial(); }; } // namespace vespalib |