aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@online.no>2024-06-07 23:14:34 +0200
committerTor Egge <Tor.Egge@online.no>2024-06-07 23:14:34 +0200
commit38a4da34ead02cde0ccdbb1d4fa88acce06f042f (patch)
tree6b7af6e01b6f710452741b7881ccfa9bf7e3e9b2 /vespalib/src
parent1478e54ecaffe8fffcbfdaf7fc853fb1578a36c5 (diff)
Move LimitedThreadBundleWrapper to vespalib.
Diffstat (limited to 'vespalib/src')
-rw-r--r--vespalib/src/vespa/vespalib/util/CMakeLists.txt1
-rw-r--r--vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.cpp31
-rw-r--r--vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.h24
3 files changed, 56 insertions, 0 deletions
diff --git a/vespalib/src/vespa/vespalib/util/CMakeLists.txt b/vespalib/src/vespa/vespalib/util/CMakeLists.txt
index 79000a89f5f..406ea68a08a 100644
--- a/vespalib/src/vespa/vespalib/util/CMakeLists.txt
+++ b/vespalib/src/vespa/vespalib/util/CMakeLists.txt
@@ -49,6 +49,7 @@ vespa_add_library(vespalib_vespalib_util OBJECT
jsonwriter.cpp
latch.cpp
left_right_heap.cpp
+ limited_thread_bundle_wrapper.cpp
lz4compressor.cpp
malloc_mmap_guard.cpp
md5.c
diff --git a/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.cpp b/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.cpp
new file mode 100644
index 00000000000..15a5bf101f0
--- /dev/null
+++ b/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.cpp
@@ -0,0 +1,31 @@
+// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "limited_thread_bundle_wrapper.h"
+#include "exceptions.h"
+
+namespace vespalib {
+
+LimitedThreadBundleWrapper::LimitedThreadBundleWrapper(ThreadBundle& thread_bundle, uint32_t max_threads)
+ : _thread_bundle(thread_bundle),
+ _max_threads(std::min(max_threads, static_cast<uint32_t>(thread_bundle.size())))
+{
+}
+
+LimitedThreadBundleWrapper::~LimitedThreadBundleWrapper() = default;
+
+size_t
+LimitedThreadBundleWrapper::size() const
+{
+ return _max_threads;
+}
+
+void
+LimitedThreadBundleWrapper::run(Runnable* const* targets, size_t cnt)
+{
+ if (cnt > size()) {
+ throw IllegalArgumentException("too many targets");
+ }
+ _thread_bundle.run(targets, cnt);
+}
+
+}
diff --git a/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.h b/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.h
new file mode 100644
index 00000000000..de3c58bf74d
--- /dev/null
+++ b/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.h
@@ -0,0 +1,24 @@
+// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#pragma once
+
+#include "thread_bundle.h"
+
+namespace vespalib {
+
+/*
+ * A ThreadBundle implementation that limits the number of available threads
+ * from the backing thread bundle.
+ */
+class LimitedThreadBundleWrapper : public ThreadBundle
+{
+ ThreadBundle& _thread_bundle;
+ const uint32_t _max_threads;
+public:
+ LimitedThreadBundleWrapper(ThreadBundle& thread_bundle, uint32_t max_threadss);
+ ~LimitedThreadBundleWrapper() override;
+ size_t size() const override;
+ void run(Runnable* const* targets, size_t cnt) override;
+};
+
+}