Skip to content

Commit

Permalink
Replace usage of std::latch with folly::Latch
Browse files Browse the repository at this point in the history
Summary:
Reworking PR facebook#2073 - use `folly::Latch` instead of having to fix ungated
use of `std::latch`, which breaks open source builds that are still using
CMake since `CMAKE_CXX_STANDARD` is still set to `17` (and bumping it to
`20` might mean having to stop supporting the default compiler on some
enterprise distributions).

Differential Revision: D59985585
  • Loading branch information
michel-slm authored and facebook-github-bot committed Jul 22, 2024
1 parent dc5586d commit 95c7d3e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 16 deletions.
1 change: 1 addition & 0 deletions folly/concurrency/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ cpp_unittest(
"//folly/hash:hash",
"//folly/portability:gflags",
"//folly/portability:gtest",
"//folly/synchronization:latch",
"//folly/test:deterministic_schedule",
],
)
Expand Down
12 changes: 2 additions & 10 deletions folly/concurrency/test/ConcurrentHashMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@
#include <thread>
#include <vector>

#include <folly/Portability.h>
#include <folly/Traits.h>
#include <folly/container/test/TrackingTypes.h>
#include <folly/hash/Hash.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
#include <folly/synchronization/Latch.h>
#include <folly/test/DeterministicSchedule.h>

#if FOLLY_CPLUSPLUS >= 202002L
// std::latch becomes visible in C++20 mode
#include <latch>
#endif

using namespace folly::test;
using namespace folly;
using namespace std;
Expand Down Expand Up @@ -1140,8 +1135,6 @@ TYPED_TEST_P(ConcurrentHashMapTest, ConcurrentInsertClear) {
}

TYPED_TEST_P(ConcurrentHashMapTest, StressTestReclamation) {
// This needs C++20 for std::latch.
#if FOLLY_CPLUSPLUS >= 202002L
// Create a map where we keep reclaiming a lot of objects that are linked to
// one node.

Expand All @@ -1165,7 +1158,7 @@ TYPED_TEST_P(ConcurrentHashMapTest, StressTestReclamation) {
// It should be uncommon to have more than 2^32 concurrent accesses.
static constexpr uint64_t num_threads = std::numeric_limits<uint16_t>::max();
static constexpr uint64_t iters = 100;
std::latch start{num_threads};
folly::Latch start(num_threads);
for (uint64_t t = 0; t < num_threads; t++) {
threads.push_back(lib::thread([t, &map, &start]() {
start.arrive_and_wait();
Expand All @@ -1185,7 +1178,6 @@ TYPED_TEST_P(ConcurrentHashMapTest, StressTestReclamation) {
for (auto& t : threads) {
join;
}
#endif
}

REGISTER_TYPED_TEST_SUITE_P(
Expand Down
2 changes: 2 additions & 0 deletions folly/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,7 @@ cpp_unittest(
"//folly/experimental/io:fs_util",
"//folly/lang:keep",
"//folly/portability:gtest",
"//folly/synchronization:latch",
"//folly/testing:test_util",
],
external_deps = [
Expand Down Expand Up @@ -1722,6 +1723,7 @@ cpp_binary(
"//folly:thread_local",
"//folly/lang:keep",
"//folly/portability:gflags",
"//folly/synchronization:latch",
],
external_deps = [
"glog",
Expand Down
4 changes: 2 additions & 2 deletions folly/test/SingletonThreadLocalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <dlfcn.h>
#endif

#include <latch>
#include <thread>
#include <unordered_set>
#include <vector>
Expand All @@ -31,6 +30,7 @@
#include <folly/experimental/io/FsUtil.h>
#include <folly/lang/Keep.h>
#include <folly/portability/GTest.h>
#include <folly/synchronization/Latch.h>
#include <folly/testing/TestUtil.h>

using namespace folly;
Expand Down Expand Up @@ -60,7 +60,7 @@ TEST(SingletonThreadLocalTest, TryGet) {

TEST(SingletonThreadLocalTest, OneSingletonPerThread) {
static constexpr std::size_t targetThreadCount{64};
std::latch allSingletonsCreated(targetThreadCount);
folly::Latch allSingletonsCreated(targetThreadCount);
Synchronized<std::unordered_set<Foo*>> fooAddresses{};
std::vector<std::thread> threads{};
auto threadFunction = [&fooAddresses, &allSingletonsCreated] {
Expand Down
8 changes: 4 additions & 4 deletions folly/test/ThreadLocalBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <sys/types.h>

#include <latch>
#include <numeric>
#include <thread>

Expand All @@ -28,6 +27,7 @@
#include <folly/Benchmark.h>
#include <folly/lang/Keep.h>
#include <folly/portability/GFlags.h>
#include <folly/synchronization/Latch.h>

using namespace folly;

Expand Down Expand Up @@ -137,9 +137,9 @@ BENCHMARK(BM_tlp_access_all_threads_iterate, iters) {
folly::ThreadLocalPtr<int, tag> var;
folly::BenchmarkSuspender braces;
std::vector<std::jthread> threads{folly::to_unsigned(FLAGS_num_threads)};
std::latch prep{FLAGS_num_threads};
std::latch work{FLAGS_num_threads + 1};
std::latch done{1};
folly::Latch prep(FLAGS_num_threads);
folly::Latch work(FLAGS_num_threads + 1);
folly::Latch done(1);
for (auto& th : threads) {
th = std::jthread([&] {
var.reset(new int(3));
Expand Down

0 comments on commit 95c7d3e

Please sign in to comment.