proxygen
SingletonThreadLocalTest.cpp File Reference
#include <thread>
#include <unordered_set>
#include <vector>
#include <folly/SingletonThreadLocal.h>
#include <folly/Synchronized.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 TEST (SingletonThreadLocalTest, OneSingletonPerThread)
 
 TEST (SingletonThreadLocalTest, MoveConstructibleMake)
 
 TEST (SingletonThreadLocalTest, NotMoveConstructibleMake)
 
 TEST (SingletonThreadLocalTest, AccessAfterFastPathDestruction)
 
 TEST (ThreadLocal, DependencyTest)
 
 TEST (SingletonThreadLocalTest, Reused)
 

Function Documentation

TEST ( SingletonThreadLocalTest  ,
OneSingletonPerThread   
)

Definition at line 41 of file SingletonThreadLocalTest.cpp.

References EXPECT_EQ, folly::pushmi::operators::get, threads, and folly::fibers::yield().

41  {
42  static constexpr std::size_t targetThreadCount{64};
43  std::atomic<std::size_t> completedThreadCount{0};
45  std::vector<std::thread> threads{};
46  auto threadFunction = [&fooAddresses, &completedThreadCount] {
47  fooAddresses.wlock()->emplace(&FooSingletonTL::get());
48  ++completedThreadCount;
49  while (completedThreadCount < targetThreadCount) {
51  }
52  };
53  {
54  for (std::size_t threadCount{0}; threadCount < targetThreadCount;
55  ++threadCount) {
56  threads.emplace_back(threadFunction);
57  }
58  }
59  for (auto& thread : threads) {
60  thread.join();
61  }
62  EXPECT_EQ(threads.size(), fooAddresses.rlock()->size());
63  EXPECT_EQ(threads.size(), fooCreatedCount);
64  EXPECT_EQ(threads.size(), fooDeletedCount);
65 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::vector< std::thread::id > threads
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( SingletonThreadLocalTest  ,
MoveConstructibleMake   
)

Definition at line 67 of file SingletonThreadLocalTest.cpp.

References a, b, EXPECT_EQ, folly::SingletonThreadLocal< T, Tag, Make, TLTag >::get(), and single.

67  {
68  struct Foo {
69  int a, b;
70  Foo(int a_, int b_) : a(a_), b(b_) {}
71  Foo(Foo&&) = default;
72  Foo& operator=(Foo&&) = default;
73  };
74  struct Tag {};
75  struct Make {
76  Foo operator()() const {
77  return Foo(3, 4);
78  }
79  };
81  EXPECT_EQ(4, single.b);
82 }
Foo(std::atomic< int > &d)
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
countdownsingle single
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( SingletonThreadLocalTest  ,
NotMoveConstructibleMake   
)

Definition at line 84 of file SingletonThreadLocalTest.cpp.

References a, b, EXPECT_EQ, folly::SingletonThreadLocal< T, Tag, Make, TLTag >::get(), and single.

84  {
85  struct Foo {
86  int a, b;
87  Foo(int a_, int b_) : a(a_), b(b_) {}
88  Foo(Foo&&) = delete;
89  Foo& operator=(Foo&&) = delete;
90  };
91  struct Tag {};
92  struct Make {
93  Foo* operator()(unsigned char (&buf)[sizeof(Foo)]) const {
94  return new (buf) Foo(3, 4);
95  }
96  };
98  EXPECT_EQ(4, single.b);
99 }
Foo(std::atomic< int > &d)
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
countdownsingle single
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( SingletonThreadLocalTest  ,
AccessAfterFastPathDestruction   
)

Definition at line 101 of file SingletonThreadLocalTest.cpp.

References counter, EXPECT_EQ, folly::SingletonThreadLocal< T, Tag, Make, TLTag >::get(), and i.

101  {
102  static std::atomic<int> counter{};
103  struct Foo {
104  int i = 3;
105  };
106  struct Bar {
107  ~Bar() {
109  }
110  };
111  auto th = std::thread([] {
114  });
115  th.join();
116  EXPECT_EQ(6, counter);
117 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::atomic< int > counter
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST ( ThreadLocal  ,
DependencyTest   
)

Definition at line 119 of file SingletonThreadLocalTest.cpp.

References folly::data(), folly::pushmi::operators::get, and folly::join().

119  {
121 
122  struct mytag {};
123 
124  typedef SingletonThreadLocal<int> SingletonInt;
125  struct barstruct {
126  ~barstruct() {
127  SingletonInt::get()++;
128  Data data;
129  data.reset(new int(0));
130  }
131  };
132  typedef SingletonThreadLocal<barstruct, mytag> BarSingleton;
133 
134  std::thread([&]() {
135  Data data;
136  data.reset(new int(0));
139  })
140  .join();
141 }
#define join
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
TEST ( SingletonThreadLocalTest  ,
Reused   
)

Definition at line 143 of file SingletonThreadLocalTest.cpp.

References folly::data(), EXPECT_EQ, FOLLY_DECLARE_REUSED, i, and string.

143  {
144  for (auto i = 0u; i < 2u; ++i) {
146  if (i == 0u) {
147  data = "hello";
148  }
149  EXPECT_EQ(i == 0u ? "hello" : "", data);
150  }
151 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define FOLLY_DECLARE_REUSED(name,...)
const char * string
Definition: Conv.cpp:212
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43