diff --git a/tensorstore/internal/BUILD b/tensorstore/internal/BUILD index bf5e8250e..8d23b104d 100644 --- a/tensorstore/internal/BUILD +++ b/tensorstore/internal/BUILD @@ -146,12 +146,12 @@ tensorstore_cc_library( "concurrency_resource_provider.h", ], deps = [ - ":thread_pool", ":type_traits", "//tensorstore:context", "//tensorstore/internal/cache_key", "//tensorstore/internal/json_binding", "//tensorstore/internal/json_binding:bindable", + "//tensorstore/internal/thread:thread_pool", "//tensorstore/util:executor", "//tensorstore/util:result", "@com_github_nlohmann_json//:nlohmann_json", @@ -166,7 +166,7 @@ tensorstore_cc_library( hdrs = ["concurrent_testutil.h"], deps = [ ":multi_barrier", - ":thread", + "//tensorstore/internal/thread", "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/log:absl_log", ], @@ -1742,68 +1742,6 @@ tensorstore_cc_library( ], ) -tensorstore_cc_library( - name = "thread", - srcs = ["thread.cc"], - hdrs = ["thread.h"], - deps = [ - "@com_google_absl//absl/functional:any_invocable", - "@com_google_absl//absl/log:absl_check", - ], -) - -tensorstore_cc_test( - name = "thread_test", - srcs = ["thread_test.cc"], - deps = [ - ":thread", - "@com_google_googletest//:gtest_main", - ], -) - -tensorstore_cc_library( - name = "thread_pool", - srcs = ["thread_pool.cc"], - hdrs = ["thread_pool.h"], - deps = [ - ":attributes", - ":intrusive_ptr", - ":mutex", - ":no_destructor", - ":thread", - "//tensorstore/internal/metrics", - "//tensorstore/internal/tracing", - "//tensorstore/util:executor", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/functional:any_invocable", - "@com_google_absl//absl/log:absl_check", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/synchronization", - "@com_google_absl//absl/time", - ], -) - -tensorstore_cc_test( - name = "thread_pool_test", - size = "small", - deps = [ - ":thread_pool", - ":thread_pool_test_lib", - ], -) - -tensorstore_cc_library( - name = "thread_pool_test_lib", - testonly = True, - srcs = ["thread_pool_test.cc"], - deps = [ - "//tensorstore/util:executor", - "@com_google_absl//absl/synchronization", - "@com_google_absl//absl/time", - "@com_google_googletest//:gtest_main", - ], -) - tensorstore_cc_library( name = "type_traits", hdrs = ["type_traits.h"], @@ -1931,38 +1869,6 @@ tensorstore_cc_library( ], ) -tensorstore_cc_library( - name = "schedule_at", - srcs = ["schedule_at.cc"], - hdrs = ["schedule_at.h"], - deps = [ - ":intrusive_red_black_tree", - ":no_destructor", - ":tagged_ptr", - ":thread", - "//tensorstore/internal/metrics", - "//tensorstore/internal/tracing", - "//tensorstore/util:stop_token", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/functional:any_invocable", - "@com_google_absl//absl/synchronization", - "@com_google_absl//absl/time", - ], -) - -tensorstore_cc_test( - name = "schedule_at_test", - size = "small", - srcs = ["schedule_at_test.cc"], - deps = [ - ":schedule_at", - "//tensorstore/util:executor", - "//tensorstore/util:stop_token", - "@com_google_absl//absl/synchronization", - "@com_google_googletest//:gtest_main", - ], -) - tensorstore_cc_library( name = "multi_barrier", srcs = ["multi_barrier.cc"], @@ -1978,7 +1884,7 @@ tensorstore_cc_test( srcs = ["multi_barrier_test.cc"], deps = [ ":multi_barrier", - ":thread", + "//tensorstore/internal/thread", "@com_google_googletest//:gtest_main", ], ) diff --git a/tensorstore/internal/cache/BUILD b/tensorstore/internal/cache/BUILD index 1c9c4fd5d..40abcf4e4 100644 --- a/tensorstore/internal/cache/BUILD +++ b/tensorstore/internal/cache/BUILD @@ -1,5 +1,5 @@ -load("//bazel:tensorstore.bzl", "tensorstore_cc_binary", "tensorstore_cc_library", "tensorstore_cc_test") load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("//bazel:tensorstore.bzl", "tensorstore_cc_binary", "tensorstore_cc_library", "tensorstore_cc_test") package(default_visibility = ["//tensorstore:internal_packages"]) @@ -360,7 +360,7 @@ tensorstore_cc_binary( "//tensorstore/index_space:transformed_array", "//tensorstore/internal:element_copy_function", "//tensorstore/internal:intrusive_ptr", - "//tensorstore/internal:thread_pool", + "//tensorstore/internal/thread:thread_pool", "//tensorstore/kvstore:generation", "//tensorstore/util:executor", "//tensorstore/util:future", @@ -405,7 +405,7 @@ tensorstore_cc_test( "//tensorstore/internal:memory", "//tensorstore/internal:meta", "//tensorstore/internal:queue_testutil", - "//tensorstore/internal:thread_pool", + "//tensorstore/internal/thread:thread_pool", "//tensorstore/kvstore:generation", "//tensorstore/kvstore:mock_kvstore", "//tensorstore/kvstore:test_util", diff --git a/tensorstore/internal/cache/chunk_cache_benchmark_test.cc b/tensorstore/internal/cache/chunk_cache_benchmark_test.cc index eb3f6567b..8f8e55dbd 100644 --- a/tensorstore/internal/cache/chunk_cache_benchmark_test.cc +++ b/tensorstore/internal/cache/chunk_cache_benchmark_test.cc @@ -43,7 +43,7 @@ #include "tensorstore/internal/cache/chunk_cache.h" #include "tensorstore/internal/element_copy_function.h" #include "tensorstore/internal/intrusive_ptr.h" -#include "tensorstore/internal/thread_pool.h" +#include "tensorstore/internal/thread/thread_pool.h" #include "tensorstore/kvstore/generation.h" #include "tensorstore/progress.h" #include "tensorstore/rank.h" diff --git a/tensorstore/internal/cache/chunk_cache_test.cc b/tensorstore/internal/cache/chunk_cache_test.cc index 818f239c4..35f98655f 100644 --- a/tensorstore/internal/cache/chunk_cache_test.cc +++ b/tensorstore/internal/cache/chunk_cache_test.cc @@ -55,7 +55,7 @@ #include "tensorstore/internal/memory.h" #include "tensorstore/internal/meta.h" #include "tensorstore/internal/queue_testutil.h" -#include "tensorstore/internal/thread_pool.h" +#include "tensorstore/internal/thread/thread_pool.h" #include "tensorstore/kvstore/generation.h" #include "tensorstore/kvstore/memory/memory_key_value_store.h" #include "tensorstore/kvstore/mock_kvstore.h" diff --git a/tensorstore/internal/concurrency_resource.cc b/tensorstore/internal/concurrency_resource.cc index e6bc804a3..65a916554 100644 --- a/tensorstore/internal/concurrency_resource.cc +++ b/tensorstore/internal/concurrency_resource.cc @@ -26,7 +26,7 @@ #include "tensorstore/internal/json_binding/bindable.h" #include "tensorstore/internal/json_binding/json_binding.h" #include "tensorstore/internal/json_binding/std_optional.h" -#include "tensorstore/internal/thread_pool.h" +#include "tensorstore/internal/thread/thread_pool.h" #include "tensorstore/internal/type_traits.h" #include "tensorstore/util/executor.h" #include "tensorstore/util/result.h" diff --git a/tensorstore/internal/concurrent_testutil.h b/tensorstore/internal/concurrent_testutil.h index e2003003f..a539a5029 100644 --- a/tensorstore/internal/concurrent_testutil.h +++ b/tensorstore/internal/concurrent_testutil.h @@ -22,7 +22,7 @@ #include // NOLINT #include "tensorstore/internal/multi_barrier.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" namespace tensorstore { namespace internal { diff --git a/tensorstore/internal/http/BUILD b/tensorstore/internal/http/BUILD index b0e8f434a..0475bda82 100644 --- a/tensorstore/internal/http/BUILD +++ b/tensorstore/internal/http/BUILD @@ -47,9 +47,9 @@ tensorstore_cc_library( "//tensorstore/internal:cord_util", "//tensorstore/internal:env", "//tensorstore/internal:no_destructor", - "//tensorstore/internal:thread", "//tensorstore/internal/log:verbose_flag", "//tensorstore/internal/metrics", + "//tensorstore/internal/thread", "//tensorstore/util:future", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/log:absl_log", @@ -149,7 +149,7 @@ tensorstore_cc_test( deps = [ ":curl_transport", ":transport_test_utils", - "//tensorstore/internal:thread", + "//tensorstore/internal/thread", "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/log:absl_log", "@com_google_absl//absl/strings", @@ -165,7 +165,7 @@ tensorstore_cc_test( ":curl_transport", ":http", ":transport_test_utils", - "//tensorstore/internal:thread", + "//tensorstore/internal/thread", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/log:absl_log", diff --git a/tensorstore/internal/http/curl_transport.cc b/tensorstore/internal/http/curl_transport.cc index f36b6b165..e4ab55da3 100644 --- a/tensorstore/internal/http/curl_transport.cc +++ b/tensorstore/internal/http/curl_transport.cc @@ -50,7 +50,7 @@ #include "tensorstore/internal/metrics/gauge.h" #include "tensorstore/internal/metrics/histogram.h" #include "tensorstore/internal/no_destructor.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" #include "tensorstore/util/future.h" namespace tensorstore { diff --git a/tensorstore/internal/http/curl_transport_http2_test.cc b/tensorstore/internal/http/curl_transport_http2_test.cc index ae1aed31e..57da1ed7a 100644 --- a/tensorstore/internal/http/curl_transport_http2_test.cc +++ b/tensorstore/internal/http/curl_transport_http2_test.cc @@ -36,7 +36,7 @@ #include "tensorstore/internal/http/curl_transport.h" #include "tensorstore/internal/http/http_request.h" #include "tensorstore/internal/http/transport_test_utils.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" using ::tensorstore::internal_http::HttpRequestBuilder; using ::tensorstore::transport_test_utils::AcceptNonBlocking; diff --git a/tensorstore/internal/http/curl_transport_test.cc b/tensorstore/internal/http/curl_transport_test.cc index 999e1323b..9bc41ad3c 100644 --- a/tensorstore/internal/http/curl_transport_test.cc +++ b/tensorstore/internal/http/curl_transport_test.cc @@ -29,7 +29,7 @@ #include "absl/log/absl_log.h" #include "absl/strings/str_cat.h" #include "tensorstore/internal/http/transport_test_utils.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" using ::tensorstore::internal_http::HttpRequestBuilder; using ::tensorstore::transport_test_utils::AcceptNonBlocking; diff --git a/tensorstore/internal/multi_barrier_test.cc b/tensorstore/internal/multi_barrier_test.cc index a50100d5b..ec76b2c28 100644 --- a/tensorstore/internal/multi_barrier_test.cc +++ b/tensorstore/internal/multi_barrier_test.cc @@ -17,7 +17,7 @@ #include #include -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" namespace internal = tensorstore::internal; diff --git a/tensorstore/internal/thread/BUILD b/tensorstore/internal/thread/BUILD new file mode 100644 index 000000000..223de0e6a --- /dev/null +++ b/tensorstore/internal/thread/BUILD @@ -0,0 +1,101 @@ +load("//bazel:tensorstore.bzl", "tensorstore_cc_library", "tensorstore_cc_test") + +package(default_visibility = ["//tensorstore:internal_packages"]) + +licenses(["notice"]) + +tensorstore_cc_library( + name = "schedule_at", + srcs = ["schedule_at.cc"], + hdrs = ["schedule_at.h"], + deps = [ + ":thread", + "//tensorstore/internal:attributes", + "//tensorstore/internal:intrusive_red_black_tree", + "//tensorstore/internal:no_destructor", + "//tensorstore/internal:tagged_ptr", + "//tensorstore/internal/metrics", + "//tensorstore/internal/tracing", + "//tensorstore/util:stop_token", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/functional:any_invocable", + "@com_google_absl//absl/synchronization", + "@com_google_absl//absl/time", + ], +) + +tensorstore_cc_test( + name = "schedule_at_test", + size = "small", + srcs = ["schedule_at_test.cc"], + deps = [ + ":schedule_at", + "//tensorstore/util:stop_token", + "@com_google_absl//absl/synchronization", + "@com_google_absl//absl/time", + "@com_google_googletest//:gtest_main", + ], +) + +tensorstore_cc_library( + name = "thread", + srcs = ["thread.cc"], + hdrs = ["thread.h"], + deps = [ + "@com_google_absl//absl/functional:any_invocable", + "@com_google_absl//absl/log:absl_check", + ], +) + +tensorstore_cc_test( + name = "thread_test", + srcs = ["thread_test.cc"], + deps = [ + ":thread", + "@com_google_googletest//:gtest_main", + ], +) + +tensorstore_cc_library( + name = "thread_pool", + srcs = ["thread_pool.cc"], + hdrs = ["thread_pool.h"], + deps = [ + ":thread", + "//tensorstore/internal:attributes", + "//tensorstore/internal:intrusive_ptr", + "//tensorstore/internal:mutex", + "//tensorstore/internal:no_destructor", + "//tensorstore/internal/metrics", + "//tensorstore/util:executor", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/functional:any_invocable", + "@com_google_absl//absl/log:absl_check", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/synchronization", + "@com_google_absl//absl/time", + ], +) + +tensorstore_cc_library( + name = "thread_pool_test_cc", + testonly = 1, + textual_hdrs = ["thread_pool_test.inc"], + deps = [ + "//tensorstore/util:executor", + "@com_google_absl//absl/synchronization", + "@com_google_absl//absl/time", + "@com_google_googletest//:gtest", + ], +) + +tensorstore_cc_test( + name = "thread_pool_test", + size = "small", + srcs = ["thread_pool_test.cc"], + deps = [ + ":thread_pool", + ":thread_pool_test_cc", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/tensorstore/internal/schedule_at.cc b/tensorstore/internal/thread/schedule_at.cc similarity index 95% rename from tensorstore/internal/schedule_at.cc rename to tensorstore/internal/thread/schedule_at.cc index fa2cf84b8..b7bbac87d 100644 --- a/tensorstore/internal/schedule_at.cc +++ b/tensorstore/internal/thread/schedule_at.cc @@ -12,8 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "tensorstore/internal/schedule_at.h" +#include "tensorstore/internal/thread/schedule_at.h" +#include + +#include +#include +#include +#include #include #include "absl/base/optimization.h" @@ -22,13 +28,14 @@ #include "absl/synchronization/mutex.h" #include "absl/time/clock.h" #include "absl/time/time.h" +#include "tensorstore/internal/attributes.h" #include "tensorstore/internal/intrusive_red_black_tree.h" #include "tensorstore/internal/metrics/gauge.h" #include "tensorstore/internal/metrics/histogram.h" #include "tensorstore/internal/metrics/value.h" #include "tensorstore/internal/no_destructor.h" #include "tensorstore/internal/tagged_ptr.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" #include "tensorstore/internal/tracing/tracing.h" #include "tensorstore/util/stop_token.h" @@ -39,16 +46,16 @@ namespace { using ScheduleAtTask = absl::AnyInvocable; auto& schedule_at_queued_ops = internal_metrics::Gauge::New( - "/tensorstore/internal/schedule_at/queued_ops", + "/tensorstore/internal/thread/schedule_at/queued_ops", "Operations in flight on the schedule_at thread"); auto& schedule_at_next_event = internal_metrics::Value::New( - "/tensorstore/internal/schedule_at/next_event", + "/tensorstore/internal/thread/schedule_at/next_event", "Time of the next in-flight schedule_at operation"); auto& schedule_at_insert_histogram_ms = internal_metrics::Histogram::New( - "/tensorstore/internal/schedule_at/insert_histogram_ms", + "/tensorstore/internal/thread/schedule_at/insert_histogram_ms", "Histogram of schedule_at insert delays (ms)"); class DeadlineTaskQueue; diff --git a/tensorstore/internal/schedule_at.h b/tensorstore/internal/thread/schedule_at.h similarity index 87% rename from tensorstore/internal/schedule_at.h rename to tensorstore/internal/thread/schedule_at.h index 7b4622e47..a09ef156a 100644 --- a/tensorstore/internal/schedule_at.h +++ b/tensorstore/internal/thread/schedule_at.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef TENSORSTORE_INTERNAL_SCHEDULE_AT_H_ -#define TENSORSTORE_INTERNAL_SCHEDULE_AT_H_ +#ifndef TENSORSTORE_INTERNAL_THREAD_SCHEDULE_AT_H_ +#define TENSORSTORE_INTERNAL_THREAD_SCHEDULE_AT_H_ #include "absl/functional/any_invocable.h" #include "absl/time/time.h" @@ -32,4 +32,4 @@ void ScheduleAt(absl::Time target_time, absl::AnyInvocable task, } // namespace internal } // namespace tensorstore -#endif // TENSORSTORE_INTERNAL_SCHEDULE_AT_H_ +#endif // TENSORSTORE_INTERNAL_THREAD_SCHEDULE_AT_H_ diff --git a/tensorstore/internal/schedule_at_test.cc b/tensorstore/internal/thread/schedule_at_test.cc similarity index 96% rename from tensorstore/internal/schedule_at_test.cc rename to tensorstore/internal/thread/schedule_at_test.cc index fa0aba029..4e50d2853 100644 --- a/tensorstore/internal/schedule_at_test.cc +++ b/tensorstore/internal/thread/schedule_at_test.cc @@ -12,14 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "tensorstore/internal/schedule_at.h" +#include "tensorstore/internal/thread/schedule_at.h" +#include #include // NOLINT #include #include #include "absl/synchronization/notification.h" -#include "tensorstore/util/executor.h" +#include "absl/time/clock.h" +#include "absl/time/time.h" #include "tensorstore/util/stop_token.h" namespace { diff --git a/tensorstore/internal/thread.cc b/tensorstore/internal/thread/thread.cc similarity index 100% rename from tensorstore/internal/thread.cc rename to tensorstore/internal/thread/thread.cc diff --git a/tensorstore/internal/thread.h b/tensorstore/internal/thread/thread.h similarity index 100% rename from tensorstore/internal/thread.h rename to tensorstore/internal/thread/thread.h diff --git a/tensorstore/internal/thread_pool.cc b/tensorstore/internal/thread/thread_pool.cc similarity index 99% rename from tensorstore/internal/thread_pool.cc rename to tensorstore/internal/thread/thread_pool.cc index 79ee19b85..83d0bc6d9 100644 --- a/tensorstore/internal/thread_pool.cc +++ b/tensorstore/internal/thread/thread_pool.cc @@ -31,7 +31,7 @@ #include "tensorstore/internal/metrics/gauge.h" #include "tensorstore/internal/mutex.h" #include "tensorstore/internal/no_destructor.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" #include "tensorstore/util/executor.h" namespace tensorstore { diff --git a/tensorstore/internal/thread_pool.h b/tensorstore/internal/thread/thread_pool.h similarity index 88% rename from tensorstore/internal/thread_pool.h rename to tensorstore/internal/thread/thread_pool.h index 83ca869f3..26bb585e5 100644 --- a/tensorstore/internal/thread_pool.h +++ b/tensorstore/internal/thread/thread_pool.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef TENSORSTORE_INTERNAL_THREAD_POOL_H_ -#define TENSORSTORE_INTERNAL_THREAD_POOL_H_ +#ifndef TENSORSTORE_INTERNAL_THREAD_THREAD_POOL_H_ +#define TENSORSTORE_INTERNAL_THREAD_THREAD_POOL_H_ #include @@ -37,4 +37,4 @@ Executor DetachedThreadPool( } // namespace internal } // namespace tensorstore -#endif // TENSORSTORE_INTERNAL_THREAD_POOL_H_ +#endif // TENSORSTORE_INTERNAL_THREAD_THREAD_POOL_H_ diff --git a/tensorstore/internal/thread/thread_pool_benchmark.inc b/tensorstore/internal/thread/thread_pool_benchmark.inc new file mode 100644 index 000000000..cc0214260 --- /dev/null +++ b/tensorstore/internal/thread/thread_pool_benchmark.inc @@ -0,0 +1,80 @@ +// Copyright 2020 The TensorStore Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_TENSORSTORE_INTERNAL_THREAD_THREAD_POOL_BENCHMARK_INC_ +#define THIRD_PARTY_TENSORSTORE_INTERNAL_THREAD_THREAD_POOL_BENCHMARK_INC_ + +#include + +#include +#include + +#include "testing/base/public/benchmark.h" +#include "absl/random/random.h" +#include "absl/synchronization/blocking_counter.h" +#include "tensorstore/util/executor.h" + +namespace { + +using ::tensorstore::Executor; +using ::tensorstore::internal::DetachedThreadPool; + +// This is a benchmark which enqueues a lot of items into a thread pool. +// it doesn't have a goal of implementing an efficient matrix multiply; the +// multiply is incidental. +static void BM_ThreadPool_MatrixMultiply(benchmark::State& state) { + const size_t sz = state.range(0); + std::vector A(sz * sz); + std::vector B(sz * sz); + std::vector C(sz * sz); + + absl::BitGen rng; + std::generate(A.begin(), A.end(), + [&] { return absl::Gaussian(rng, 0, 1); }); + std::generate(B.begin(), B.end(), + [&] { return absl::Gaussian(rng, 0, 1); }); + + for (auto s : state) { + absl::BlockingCounter done(sz * sz); + auto executor = DetachedThreadPool(state.range(1)); + for (size_t i = 0; i < sz; i++) { + executor([&, i] { + for (size_t j = 0; j < sz; ++j) { + executor([&, i, j] { + float sum = 0; + for (size_t l = 0; l < sz; ++l) { + sum += A[i + l * sz] * B[l + j * sz]; + } + C[i + j * sz] = sum; + done.DecrementCount(); + }); + } + }); + } + done.Wait(); + } + state.SetItemsProcessed(state.iterations() * sz * sz); +} + +BENCHMARK(BM_ThreadPool_MatrixMultiply) // + ->Args({256, 12}) + ->Args({1024, 12}) + ->Args({1024, 24}) + ->Args({2048, 24}) + ->Args({2048, 36}) + ->UseRealTime(); + +} // namespace + +#endif // THIRD_PARTY_TENSORSTORE_INTERNAL_THREAD_THREAD_POOL_BENCHMARK_INC_ diff --git a/tensorstore/internal/thread/thread_pool_test.cc b/tensorstore/internal/thread/thread_pool_test.cc new file mode 100644 index 000000000..5b106b763 --- /dev/null +++ b/tensorstore/internal/thread/thread_pool_test.cc @@ -0,0 +1,17 @@ +// Copyright 2020 The TensorStore Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "tensorstore/internal/thread/thread_pool.h" // IWYU pragma: keep + +#include "tensorstore/internal/thread/thread_pool_test.inc" // IWYU pragma: keep diff --git a/tensorstore/internal/thread_pool_test.cc b/tensorstore/internal/thread/thread_pool_test.inc similarity index 90% rename from tensorstore/internal/thread_pool_test.cc rename to tensorstore/internal/thread/thread_pool_test.inc index 9a91df7fe..106414de8 100644 --- a/tensorstore/internal/thread_pool_test.cc +++ b/tensorstore/internal/thread/thread_pool_test.inc @@ -12,23 +12,25 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef THIRD_PARTY_TENSORSTORE_INTERNAL_THREAD_THREAD_POOL_TEST_INC_ +#define THIRD_PARTY_TENSORSTORE_INTERNAL_THREAD_THREAD_POOL_TEST_INC_ + +#include + #include -#include +#include +#include #include #include #include #include "absl/synchronization/notification.h" #include "absl/time/clock.h" +#include "absl/time/time.h" #include "tensorstore/util/executor.h" -namespace tensorstore { -namespace internal { -Executor DetachedThreadPool(std::size_t num_threads); -} // namespace internal -} // namespace tensorstore - namespace { + using ::tensorstore::Executor; using ::tensorstore::internal::DetachedThreadPool; @@ -63,7 +65,7 @@ TEST(DetachedThreadPoolTest, Concurrent) { TEST(DetachedThreadPoolTest, ThreadLimit) { constexpr static size_t kThreadLimit = 3; auto executor = DetachedThreadPool(kThreadLimit); - std::atomic num_running_tasks{0}; + std::atomic num_running_tasks{0}; std::vector notifications(5); for (size_t i = 0; i < notifications.size(); ++i) { executor([&num_running_tasks, ¬ifications, i] { @@ -116,3 +118,5 @@ TEST(DetachedThreadPoolTest, EnqueueFromTaskDestructor) { } } // namespace + +#endif // THIRD_PARTY_TENSORSTORE_INTERNAL_THREAD_THREAD_POOL_TEST_INC_ diff --git a/tensorstore/internal/thread_test.cc b/tensorstore/internal/thread/thread_test.cc similarity index 95% rename from tensorstore/internal/thread_test.cc rename to tensorstore/internal/thread/thread_test.cc index 16820babf..84b058c1d 100644 --- a/tensorstore/internal/thread_test.cc +++ b/tensorstore/internal/thread/thread_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" #include diff --git a/tensorstore/kvstore/file/BUILD b/tensorstore/kvstore/file/BUILD index e94abba7e..906ec2d7c 100644 --- a/tensorstore/kvstore/file/BUILD +++ b/tensorstore/kvstore/file/BUILD @@ -64,8 +64,8 @@ tensorstore_cc_test( "//tensorstore:context", "//tensorstore/internal:file_io_concurrency_resource", "//tensorstore/internal:test_util", - "//tensorstore/internal:thread", "//tensorstore/internal/os:filesystem", + "//tensorstore/internal/thread", "//tensorstore/kvstore", "//tensorstore/kvstore:generation", "//tensorstore/kvstore:generation_testutil", diff --git a/tensorstore/kvstore/file/file_key_value_store_test.cc b/tensorstore/kvstore/file/file_key_value_store_test.cc index b09f420d1..db4177b64 100644 --- a/tensorstore/kvstore/file/file_key_value_store_test.cc +++ b/tensorstore/kvstore/file/file_key_value_store_test.cc @@ -30,7 +30,7 @@ #include "tensorstore/context.h" #include "tensorstore/internal/os/filesystem.h" #include "tensorstore/internal/test_util.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" #include "tensorstore/kvstore/generation.h" #include "tensorstore/kvstore/generation_testutil.h" #include "tensorstore/kvstore/key_range.h" diff --git a/tensorstore/kvstore/gcs_grpc/BUILD b/tensorstore/kvstore/gcs_grpc/BUILD index b8039d9d8..be17842b0 100644 --- a/tensorstore/kvstore/gcs_grpc/BUILD +++ b/tensorstore/kvstore/gcs_grpc/BUILD @@ -15,7 +15,6 @@ tensorstore_cc_library( "//tensorstore/internal:data_copy_concurrency_resource", "//tensorstore/internal:intrusive_ptr", "//tensorstore/internal:retry", - "//tensorstore/internal:schedule_at", "//tensorstore/internal:uri_utils", "//tensorstore/internal/cache_key", "//tensorstore/internal/grpc:utils", @@ -24,6 +23,7 @@ tensorstore_cc_library( "//tensorstore/internal/json_binding:bindable", "//tensorstore/internal/log:verbose_flag", "//tensorstore/internal/metrics", + "//tensorstore/internal/thread:schedule_at", "//tensorstore/kvstore", "//tensorstore/kvstore:byte_range", "//tensorstore/kvstore:generation", @@ -121,7 +121,7 @@ tensorstore_cc_test( deps = [ ":gcs_grpc", "//tensorstore/internal:no_destructor", - "//tensorstore/internal:thread", + "//tensorstore/internal/thread", "//tensorstore/kvstore", "//tensorstore/kvstore:generation", "//tensorstore/kvstore:test_util", diff --git a/tensorstore/kvstore/gcs_grpc/gcs_grpc.cc b/tensorstore/kvstore/gcs_grpc/gcs_grpc.cc index 966eb1e73..5a2fb7367 100644 --- a/tensorstore/kvstore/gcs_grpc/gcs_grpc.cc +++ b/tensorstore/kvstore/gcs_grpc/gcs_grpc.cc @@ -54,7 +54,7 @@ #include "tensorstore/internal/metrics/counter.h" #include "tensorstore/internal/metrics/histogram.h" #include "tensorstore/internal/retry.h" -#include "tensorstore/internal/schedule_at.h" +#include "tensorstore/internal/thread/schedule_at.h" #include "tensorstore/internal/uri_utils.h" #include "tensorstore/kvstore/byte_range.h" #include "tensorstore/kvstore/driver.h" diff --git a/tensorstore/kvstore/gcs_grpc/gcs_grpc_testbench_test.cc b/tensorstore/kvstore/gcs_grpc/gcs_grpc_testbench_test.cc index 712f98274..57a0d5a43 100644 --- a/tensorstore/kvstore/gcs_grpc/gcs_grpc_testbench_test.cc +++ b/tensorstore/kvstore/gcs_grpc/gcs_grpc_testbench_test.cc @@ -26,7 +26,7 @@ #include "absl/strings/cord.h" #include "absl/strings/str_cat.h" #include "tensorstore/internal/no_destructor.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" #include "tensorstore/kvstore/gcs/gcs_testbench.h" #include "tensorstore/kvstore/generation.h" #include "tensorstore/kvstore/kvstore.h" diff --git a/tensorstore/kvstore/gcs_http/BUILD b/tensorstore/kvstore/gcs_http/BUILD index 071ca44ba..e2386de47 100644 --- a/tensorstore/kvstore/gcs_http/BUILD +++ b/tensorstore/kvstore/gcs_http/BUILD @@ -22,7 +22,6 @@ tensorstore_cc_library( "//tensorstore/internal:path", "//tensorstore/internal:retries_context_resource", "//tensorstore/internal:retry", - "//tensorstore/internal:schedule_at", "//tensorstore/internal:source_location", "//tensorstore/internal:uri_utils", "//tensorstore/internal/cache_key", @@ -36,6 +35,7 @@ tensorstore_cc_library( "//tensorstore/internal/metrics", "//tensorstore/internal/oauth2", "//tensorstore/internal/oauth2:google_auth_provider", + "//tensorstore/internal/thread:schedule_at", "//tensorstore/kvstore", "//tensorstore/kvstore:byte_range", "//tensorstore/kvstore:generation", @@ -79,12 +79,12 @@ tensorstore_cc_test( "//tensorstore:context", "//tensorstore:json_serialization_options_base", "//tensorstore/internal:json_gtest", - "//tensorstore/internal:schedule_at", "//tensorstore/internal:uri_utils", "//tensorstore/internal/http", "//tensorstore/internal/http:curl_transport", "//tensorstore/internal/oauth2:google_auth_provider", "//tensorstore/internal/oauth2:google_auth_test_utils", + "//tensorstore/internal/thread:schedule_at", "//tensorstore/kvstore", "//tensorstore/kvstore:generation", "//tensorstore/kvstore:key_range", @@ -188,8 +188,8 @@ tensorstore_cc_library( deps = [ ":rate_limiter", "//tensorstore:data_type", - "//tensorstore/internal:schedule_at", "//tensorstore/internal/poly", + "//tensorstore/internal/thread:schedule_at", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/synchronization", @@ -228,8 +228,8 @@ tensorstore_cc_test( ":gcs_http", "//tensorstore/internal:env", "//tensorstore/internal:no_destructor", - "//tensorstore/internal:thread", "//tensorstore/internal/http:transport_test_utils", + "//tensorstore/internal/thread", "//tensorstore/kvstore", "//tensorstore/kvstore:generation", "//tensorstore/kvstore:test_util", diff --git a/tensorstore/kvstore/gcs_http/gcs_key_value_store.cc b/tensorstore/kvstore/gcs_http/gcs_key_value_store.cc index f731d426e..f37875b55 100644 --- a/tensorstore/kvstore/gcs_http/gcs_key_value_store.cc +++ b/tensorstore/kvstore/gcs_http/gcs_key_value_store.cc @@ -56,8 +56,8 @@ #include "tensorstore/internal/path.h" #include "tensorstore/internal/retries_context_resource.h" #include "tensorstore/internal/retry.h" -#include "tensorstore/internal/schedule_at.h" #include "tensorstore/internal/source_location.h" +#include "tensorstore/internal/thread/schedule_at.h" #include "tensorstore/internal/uri_utils.h" #include "tensorstore/kvstore/byte_range.h" #include "tensorstore/kvstore/driver.h" diff --git a/tensorstore/kvstore/gcs_http/gcs_key_value_store_test.cc b/tensorstore/kvstore/gcs_http/gcs_key_value_store_test.cc index a462a558e..55ef0bfb7 100644 --- a/tensorstore/kvstore/gcs_http/gcs_key_value_store_test.cc +++ b/tensorstore/kvstore/gcs_http/gcs_key_value_store_test.cc @@ -43,7 +43,7 @@ #include "tensorstore/internal/json_gtest.h" #include "tensorstore/internal/oauth2/google_auth_provider.h" #include "tensorstore/internal/oauth2/google_auth_test_utils.h" -#include "tensorstore/internal/schedule_at.h" +#include "tensorstore/internal/thread/schedule_at.h" #include "tensorstore/internal/uri_utils.h" #include "tensorstore/json_serialization_options_base.h" #include "tensorstore/kvstore/gcs_http/gcs_mock.h" diff --git a/tensorstore/kvstore/gcs_http/gcs_testbench_test.cc b/tensorstore/kvstore/gcs_http/gcs_testbench_test.cc index c41a82864..246d15b7c 100644 --- a/tensorstore/kvstore/gcs_http/gcs_testbench_test.cc +++ b/tensorstore/kvstore/gcs_http/gcs_testbench_test.cc @@ -28,7 +28,7 @@ #include "absl/strings/str_cat.h" #include "tensorstore/internal/env.h" #include "tensorstore/internal/no_destructor.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" #include "tensorstore/kvstore/generation.h" #include "tensorstore/kvstore/kvstore.h" #include "tensorstore/kvstore/operations.h" diff --git a/tensorstore/kvstore/gcs_http/scaling_rate_limiter.cc b/tensorstore/kvstore/gcs_http/scaling_rate_limiter.cc index c7b0281a8..2911df270 100644 --- a/tensorstore/kvstore/gcs_http/scaling_rate_limiter.cc +++ b/tensorstore/kvstore/gcs_http/scaling_rate_limiter.cc @@ -21,7 +21,7 @@ #include "absl/synchronization/mutex.h" #include "absl/time/time.h" #include "tensorstore/data_type.h" -#include "tensorstore/internal/schedule_at.h" +#include "tensorstore/internal/thread/schedule_at.h" #include "tensorstore/kvstore/gcs_http/rate_limiter.h" namespace tensorstore { diff --git a/tensorstore/kvstore/neuroglancer_uint64_sharded/BUILD b/tensorstore/kvstore/neuroglancer_uint64_sharded/BUILD index 882c307f7..acea651ac 100644 --- a/tensorstore/kvstore/neuroglancer_uint64_sharded/BUILD +++ b/tensorstore/kvstore/neuroglancer_uint64_sharded/BUILD @@ -187,10 +187,10 @@ tensorstore_cc_test( "//tensorstore/internal:global_initializer", "//tensorstore/internal:intrusive_ptr", "//tensorstore/internal:test_util", - "//tensorstore/internal:thread_pool", "//tensorstore/internal/cache", "//tensorstore/internal/cache:kvs_backed_cache_testutil", "//tensorstore/internal/compression:zlib", + "//tensorstore/internal/thread:thread_pool", "//tensorstore/kvstore", "//tensorstore/kvstore:byte_range", "//tensorstore/kvstore:generation", diff --git a/tensorstore/kvstore/neuroglancer_uint64_sharded/neuroglancer_uint64_sharded_test.cc b/tensorstore/kvstore/neuroglancer_uint64_sharded/neuroglancer_uint64_sharded_test.cc index 084692192..49433115b 100644 --- a/tensorstore/kvstore/neuroglancer_uint64_sharded/neuroglancer_uint64_sharded_test.cc +++ b/tensorstore/kvstore/neuroglancer_uint64_sharded/neuroglancer_uint64_sharded_test.cc @@ -41,7 +41,7 @@ #include "tensorstore/internal/global_initializer.h" #include "tensorstore/internal/intrusive_ptr.h" #include "tensorstore/internal/test_util.h" -#include "tensorstore/internal/thread_pool.h" +#include "tensorstore/internal/thread/thread_pool.h" #include "tensorstore/kvstore/byte_range.h" #include "tensorstore/kvstore/generation.h" #include "tensorstore/kvstore/generation_testutil.h" diff --git a/tensorstore/kvstore/ocdbt/io/BUILD b/tensorstore/kvstore/ocdbt/io/BUILD index 8380454c1..ad04ccfae 100644 --- a/tensorstore/kvstore/ocdbt/io/BUILD +++ b/tensorstore/kvstore/ocdbt/io/BUILD @@ -15,8 +15,8 @@ cc_library( "//tensorstore:transaction", "//tensorstore/internal:flat_cord_builder", "//tensorstore/internal:intrusive_ptr", - "//tensorstore/internal:schedule_at", "//tensorstore/internal/log:verbose_flag", + "//tensorstore/internal/thread:schedule_at", "//tensorstore/kvstore", "//tensorstore/kvstore:byte_range", "//tensorstore/kvstore:generation", @@ -44,7 +44,7 @@ tensorstore_cc_test( srcs = ["coalesce_kvstore_test.cc"], deps = [ ":coalesce_kvstore", - "//tensorstore/internal:thread_pool", + "//tensorstore/internal/thread:thread_pool", "//tensorstore/kvstore", "//tensorstore/kvstore:mock_kvstore", "//tensorstore/kvstore:test_util", diff --git a/tensorstore/kvstore/ocdbt/io/coalesce_kvstore.cc b/tensorstore/kvstore/ocdbt/io/coalesce_kvstore.cc index 8db1b4469..82b550172 100644 --- a/tensorstore/kvstore/ocdbt/io/coalesce_kvstore.cc +++ b/tensorstore/kvstore/ocdbt/io/coalesce_kvstore.cc @@ -40,7 +40,7 @@ #include "tensorstore/internal/flat_cord_builder.h" #include "tensorstore/internal/intrusive_ptr.h" #include "tensorstore/internal/log/verbose_flag.h" -#include "tensorstore/internal/schedule_at.h" +#include "tensorstore/internal/thread/schedule_at.h" #include "tensorstore/kvstore/byte_range.h" #include "tensorstore/kvstore/driver.h" #include "tensorstore/kvstore/generation.h" diff --git a/tensorstore/kvstore/ocdbt/io/coalesce_kvstore_test.cc b/tensorstore/kvstore/ocdbt/io/coalesce_kvstore_test.cc index df3b47472..774daef24 100644 --- a/tensorstore/kvstore/ocdbt/io/coalesce_kvstore_test.cc +++ b/tensorstore/kvstore/ocdbt/io/coalesce_kvstore_test.cc @@ -18,7 +18,7 @@ #include #include "absl/strings/cord.h" #include "absl/time/time.h" -#include "tensorstore/internal/thread_pool.h" +#include "tensorstore/internal/thread/thread_pool.h" #include "tensorstore/kvstore/kvstore.h" #include "tensorstore/kvstore/mock_kvstore.h" #include "tensorstore/kvstore/operations.h" diff --git a/tensorstore/kvstore/s3/BUILD b/tensorstore/kvstore/s3/BUILD index f0cedd24c..9cc312ddb 100644 --- a/tensorstore/kvstore/s3/BUILD +++ b/tensorstore/kvstore/s3/BUILD @@ -30,7 +30,6 @@ tensorstore_cc_library( "//tensorstore/internal:data_copy_concurrency_resource", "//tensorstore/internal:intrusive_ptr", "//tensorstore/internal:retry", - "//tensorstore/internal:schedule_at", "//tensorstore/internal:source_location", "//tensorstore/internal:uri_utils", "//tensorstore/internal/cache_key", @@ -40,6 +39,7 @@ tensorstore_cc_library( "//tensorstore/internal/json_binding", "//tensorstore/internal/log:verbose_flag", "//tensorstore/internal/metrics", + "//tensorstore/internal/thread:schedule_at", "//tensorstore/kvstore", "//tensorstore/kvstore:byte_range", "//tensorstore/kvstore:generation", diff --git a/tensorstore/kvstore/s3/s3_key_value_store.cc b/tensorstore/kvstore/s3/s3_key_value_store.cc index 5a9026a47..3d74da894 100644 --- a/tensorstore/kvstore/s3/s3_key_value_store.cc +++ b/tensorstore/kvstore/s3/s3_key_value_store.cc @@ -49,8 +49,8 @@ #include "tensorstore/internal/metrics/counter.h" #include "tensorstore/internal/metrics/histogram.h" #include "tensorstore/internal/retry.h" -#include "tensorstore/internal/schedule_at.h" #include "tensorstore/internal/source_location.h" +#include "tensorstore/internal/thread/schedule_at.h" #include "tensorstore/internal/uri_utils.h" #include "tensorstore/kvstore/byte_range.h" #include "tensorstore/kvstore/gcs/validate.h" diff --git a/tensorstore/kvstore/zarr3_sharding_indexed/BUILD b/tensorstore/kvstore/zarr3_sharding_indexed/BUILD index 7d66d4c0b..2665c7e14 100644 --- a/tensorstore/kvstore/zarr3_sharding_indexed/BUILD +++ b/tensorstore/kvstore/zarr3_sharding_indexed/BUILD @@ -87,10 +87,10 @@ tensorstore_cc_test( "//tensorstore/internal:global_initializer", "//tensorstore/internal:intrusive_ptr", "//tensorstore/internal:test_util", - "//tensorstore/internal:thread_pool", "//tensorstore/internal/cache", "//tensorstore/internal/cache:kvs_backed_cache_testutil", "//tensorstore/internal/riegeli:digest_suffixed_writer", + "//tensorstore/internal/thread:thread_pool", "//tensorstore/kvstore", "//tensorstore/kvstore:byte_range", "//tensorstore/kvstore:generation", diff --git a/tensorstore/kvstore/zarr3_sharding_indexed/zarr3_sharding_indexed_test.cc b/tensorstore/kvstore/zarr3_sharding_indexed/zarr3_sharding_indexed_test.cc index 9cc49bfb6..8e71b0aa9 100644 --- a/tensorstore/kvstore/zarr3_sharding_indexed/zarr3_sharding_indexed_test.cc +++ b/tensorstore/kvstore/zarr3_sharding_indexed/zarr3_sharding_indexed_test.cc @@ -48,7 +48,7 @@ #include "tensorstore/internal/intrusive_ptr.h" #include "tensorstore/internal/riegeli/digest_suffixed_writer.h" #include "tensorstore/internal/test_util.h" -#include "tensorstore/internal/thread_pool.h" +#include "tensorstore/internal/thread/thread_pool.h" #include "tensorstore/kvstore/byte_range.h" #include "tensorstore/kvstore/generation.h" #include "tensorstore/kvstore/generation_testutil.h" diff --git a/tensorstore/util/execution/BUILD b/tensorstore/util/execution/BUILD index 01f23f2c9..3eeda8ad5 100644 --- a/tensorstore/util/execution/BUILD +++ b/tensorstore/util/execution/BUILD @@ -136,8 +136,7 @@ tensorstore_cc_test( ":execution", ":sender_testutil", ":sync_flow_sender", - "//tensorstore/internal:mutex", - "//tensorstore/internal:thread", + "//tensorstore/internal/thread", "@com_google_googletest//:gtest_main", ], ) diff --git a/tensorstore/util/execution/sync_flow_sender_test.cc b/tensorstore/util/execution/sync_flow_sender_test.cc index 6d9a5a656..3dbbd0d7b 100644 --- a/tensorstore/util/execution/sync_flow_sender_test.cc +++ b/tensorstore/util/execution/sync_flow_sender_test.cc @@ -14,26 +14,27 @@ #include "tensorstore/util/execution/sync_flow_sender.h" +#include + #include #include #include #include -#include "tensorstore/internal/mutex.h" -#include "tensorstore/internal/thread.h" +#include "tensorstore/internal/thread/thread.h" #include "tensorstore/util/execution/execution.h" #include "tensorstore/util/execution/sender_testutil.h" namespace { struct ConcurrentSender { - std::size_t num_threads; + size_t num_threads; bool error; template void submit(Receiver receiver) { tensorstore::execution::set_starting(receiver, [] {}); std::vector threads; - for (std::size_t i = 0; i < num_threads; ++i) { + for (size_t i = 0; i < num_threads; ++i) { threads.emplace_back(tensorstore::internal::Thread( {"sender"}, [i, &receiver] { tensorstore::execution::set_value(receiver, i); })); @@ -50,7 +51,7 @@ struct ConcurrentSender { TEST(SyncFlowSender, Values) { std::vector log; - const std::size_t num_threads = 10; + const size_t num_threads = 10; tensorstore::execution::submit( tensorstore::MakeSyncFlowSender( ConcurrentSender{num_threads, /*.error=*/false}), @@ -69,7 +70,7 @@ TEST(SyncFlowSender, Values) { TEST(SyncFlowSender, Error) { std::vector log; - const std::size_t num_threads = 10; + const size_t num_threads = 10; tensorstore::execution::submit( tensorstore::MakeSyncFlowSender( ConcurrentSender{num_threads, /*.error=*/true}),