X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2FThreadLocalTest.cpp;h=6791c0f42902d1a4c731c9cfc3efb40a447eb4a6;hb=0fd994133fbaafa1e4b6c86f5c14a101f2086e8c;hp=e2e2fce0ead5a8dced6088e8830eb604f5c86f91;hpb=76551ae88ffd2588d05d94814af75c489d1454f7;p=folly.git diff --git a/folly/test/ThreadLocalTest.cpp b/folly/test/ThreadLocalTest.cpp index e2e2fce0..6791c0f4 100644 --- a/folly/test/ThreadLocalTest.cpp +++ b/folly/test/ThreadLocalTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,21 +26,23 @@ #include #include #include +#include #include -#include #include +#include #include #include #include #include #include -#include #include #include #include +#include #include +#include using namespace folly; @@ -102,7 +104,7 @@ TEST(ThreadLocalPtr, DefaultDeleterOwnershipTransfer) { Widget::totalVal_ = 0; { ThreadLocalPtr w; - auto source = folly::make_unique(); + auto source = std::make_unique(); std::thread([&w, &source]() { w.reset(std::move(source)); w.get()->val_ += 10; @@ -268,7 +270,7 @@ TEST(ThreadLocal, InterleavedDestructors) { { std::lock_guard g(lock); thIterPrev = thIter; - w.reset(new ThreadLocal()); + w = std::make_unique>(); ++wVersion; } while (true) { @@ -312,10 +314,12 @@ TEST(ThreadLocalPtr, AccessAllThreadsCounter) { std::atomic totalAtomic(0); std::vector threads; for (int i = 0; i < kNumThreads; ++i) { - threads.push_back(std::thread([&,i]() { + threads.push_back(std::thread([&]() { stci.add(1); totalAtomic.fetch_add(1); - while (run.load()) { usleep(100); } + while (run.load()) { + usleep(100); + } })); } while (totalAtomic.load() != kNumThreads) { usleep(100); } @@ -342,7 +346,7 @@ struct Tag {}; struct Foo { folly::ThreadLocal tl; }; -} // namespace +} // namespace TEST(ThreadLocal, Movable1) { Foo a; @@ -405,27 +409,26 @@ class FillObject { private: uint64_t val() const { - return (idx_ << 40) | uint64_t(pthread_self()); + return (idx_ << 40) | folly::getCurrentThreadID(); } uint64_t idx_; uint64_t data_[kFillObjectSize]; }; -} // namespace +} // namespace -#if FOLLY_HAVE_STD_THIS_THREAD_SLEEP_FOR TEST(ThreadLocal, Stress) { - constexpr size_t numFillObjects = 250; + static constexpr size_t numFillObjects = 250; std::array, numFillObjects> objects; - constexpr size_t numThreads = 32; - constexpr size_t numReps = 20; + static constexpr size_t numThreads = 32; + static constexpr size_t numReps = 20; std::vector threads; threads.reserve(numThreads); - for (size_t i = 0; i < numThreads; ++i) { + for (size_t k = 0; k < numThreads; ++k) { threads.emplace_back([&objects] { for (size_t rep = 0; rep < numReps; ++rep) { for (size_t i = 0; i < objects.size(); ++i) { @@ -445,7 +448,6 @@ TEST(ThreadLocal, Stress) { EXPECT_EQ(numFillObjects * numThreads * numReps, gDestroyed); } -#endif // Yes, threads and fork don't mix // (http://cppwisdom.quora.com/Why-threads-and-fork-dont-mix) but if you're @@ -472,7 +474,7 @@ int totalValue() { return value; } -} // namespace +} // namespace #ifdef FOLLY_HAVE_PTHREAD_ATFORK TEST(ThreadLocal, Fork) { @@ -529,7 +531,7 @@ TEST(ThreadLocal, Fork) { EXPECT_TRUE(WIFEXITED(status)); EXPECT_EQ(0, WEXITSTATUS(status)); } else { - EXPECT_TRUE(false) << "fork failed"; + ADD_FAILURE() << "fork failed"; } EXPECT_EQ(2, totalValue()); @@ -572,7 +574,7 @@ TEST(ThreadLocal, Fork2) { EXPECT_TRUE(WIFEXITED(status)); EXPECT_EQ(0, WEXITSTATUS(status)); } else { - EXPECT_TRUE(false) << "fork failed"; + ADD_FAILURE() << "fork failed"; } } @@ -584,7 +586,7 @@ TEST(ThreadLocal, Fork2) { TEST(ThreadLocal, SharedLibrary) { auto exe = fs::executable_path(); - auto lib = exe.parent_path() / "lib_thread_local_test.so"; + auto lib = exe.parent_path() / "thread_local_test_lib.so"; auto handle = dlopen(lib.string().c_str(), RTLD_LAZY); EXPECT_NE(nullptr, handle); @@ -631,7 +633,8 @@ struct PthreadKeyUnregisterTester { PthreadKeyUnregister p; constexpr PthreadKeyUnregisterTester() = default; }; -}} +} // namespace threadlocal_detail +} // namespace folly TEST(ThreadLocal, UnregisterClassHasConstExprCtor) { folly::threadlocal_detail::PthreadKeyUnregisterTester x;