atomic_intrusive_ptr_ut.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/yt/memory/new.h>
  3. #include <library/cpp/yt/memory/ref_counted.h>
  4. #include <library/cpp/yt/memory/atomic_intrusive_ptr.h>
  5. #include <library/cpp/yt/memory/leaky_singleton.h>
  6. #include <util/system/compiler.h>
  7. namespace NYT {
  8. namespace {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. #ifndef _lsan_enabled_
  11. using ::testing::IsNull;
  12. using ::testing::NotNull;
  13. using ::testing::InSequence;
  14. using ::testing::MockFunction;
  15. using ::testing::StrictMock;
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Auxiliary types and functions.
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // This object tracks number of increments and decrements
  20. // to the reference counter (see traits specialization below).
  21. struct TIntricateObject
  22. : private TNonCopyable
  23. {
  24. mutable int Increments = 0;
  25. mutable int Decrements = 0;
  26. mutable int Zeros = 0;
  27. void Ref(int n) const
  28. {
  29. Increments += n;
  30. }
  31. void Unref(int n) const
  32. {
  33. Decrements += n;
  34. if (Increments == Decrements) {
  35. ++Zeros;
  36. }
  37. }
  38. };
  39. using TIntricateObjectPtr = TIntrusivePtr<TIntricateObject>;
  40. using TConstIntricateObjectPtr = TIntrusivePtr<const TIntricateObject>;
  41. void Ref(TIntricateObject* obj, int n = 1)
  42. {
  43. obj->Ref(n);
  44. }
  45. void Unref(TIntricateObject* obj, int n = 1)
  46. {
  47. obj->Unref(n);
  48. }
  49. void Ref(const TIntricateObject* obj, int n = 1)
  50. {
  51. obj->Ref(n);
  52. }
  53. void Unref(const TIntricateObject* obj, int n = 1)
  54. {
  55. obj->Unref(n);
  56. }
  57. MATCHER_P3(HasRefCounts, increments, decrements, zeros,
  58. "Reference counter " \
  59. "was incremented " + ::testing::PrintToString(increments) + " times, " +
  60. "was decremented " + ::testing::PrintToString(decrements) + " times, " +
  61. "vanished to zero " + ::testing::PrintToString(zeros) + " times")
  62. {
  63. Y_UNUSED(result_listener);
  64. return
  65. arg.Increments == increments &&
  66. arg.Decrements == decrements &&
  67. arg.Zeros == zeros;
  68. }
  69. void PrintTo(const TIntricateObject& arg, ::std::ostream* os)
  70. {
  71. *os << arg.Increments << " increments, "
  72. << arg.Decrements << " decrements and "
  73. << arg.Zeros << " times vanished";
  74. }
  75. // This is an object which creates intrusive pointers to the self
  76. // during its construction.
  77. class TObjectWithSelfPointers
  78. : public TRefCounted
  79. {
  80. public:
  81. explicit TObjectWithSelfPointers(IOutputStream* output)
  82. : Output_(output)
  83. {
  84. *Output_ << "Cb";
  85. for (int i = 0; i < 3; ++i) {
  86. *Output_ << '!';
  87. TIntrusivePtr<TObjectWithSelfPointers> ptr(this);
  88. }
  89. *Output_ << "Ca";
  90. }
  91. virtual ~TObjectWithSelfPointers()
  92. {
  93. *Output_ << 'D';
  94. }
  95. private:
  96. IOutputStream* const Output_;
  97. };
  98. // This is a simple object with simple reference counting.
  99. class TObjectWithSimpleRC
  100. : public TRefCounted
  101. {
  102. public:
  103. explicit TObjectWithSimpleRC(IOutputStream* output)
  104. : Output_(output)
  105. {
  106. *Output_ << 'C';
  107. }
  108. virtual ~TObjectWithSimpleRC()
  109. {
  110. *Output_ << 'D';
  111. }
  112. void DoSomething()
  113. {
  114. *Output_ << '!';
  115. }
  116. private:
  117. IOutputStream* const Output_;
  118. };
  119. // This is a simple object with full-fledged reference counting.
  120. class TObjectWithFullRC
  121. : public TRefCounted
  122. {
  123. public:
  124. explicit TObjectWithFullRC(IOutputStream* output)
  125. : Output_(output)
  126. {
  127. *Output_ << 'C';
  128. }
  129. virtual ~TObjectWithFullRC()
  130. {
  131. *Output_ << 'D';
  132. }
  133. void DoSomething()
  134. {
  135. *Output_ << '!';
  136. }
  137. private:
  138. IOutputStream* const Output_;
  139. };
  140. ////////////////////////////////////////////////////////////////////////////////
  141. TEST(TIntrusiveAtomicPtrTest, Empty)
  142. {
  143. TIntricateObjectPtr emptyPointer;
  144. EXPECT_EQ(nullptr, emptyPointer.Get());
  145. }
  146. constexpr int ReservedRefCount = 65535;
  147. TEST(TIntrusiveAtomicPtrTest, Reset)
  148. {
  149. TIntricateObject object;
  150. TIntricateObjectPtr owningPointer(&object);
  151. TAtomicIntrusivePtr<TIntricateObject> atomicPointer(owningPointer);
  152. atomicPointer.Reset();
  153. EXPECT_EQ(nullptr, atomicPointer.Get());
  154. EXPECT_THAT(object, HasRefCounts(1 + ReservedRefCount, ReservedRefCount, 0));
  155. }
  156. TEST(TIntrusiveAtomicPtrTest, Basic)
  157. {
  158. TIntricateObject object;
  159. EXPECT_THAT(object, HasRefCounts(0, 0, 0));
  160. {
  161. TIntricateObjectPtr owningPointer(&object);
  162. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  163. EXPECT_EQ(&object, owningPointer.Get());
  164. }
  165. EXPECT_THAT(object, HasRefCounts(1, 1, 1));
  166. {
  167. TIntricateObjectPtr owningPointer(&object);
  168. TAtomicIntrusivePtr<TIntricateObject> atomicPointer(owningPointer);
  169. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 1, 1));
  170. EXPECT_EQ(&object, owningPointer.Get());
  171. auto p1 = atomicPointer.Acquire();
  172. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 1, 1));
  173. p1.Reset();
  174. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 2, 1));
  175. owningPointer.Reset();
  176. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 3, 1));
  177. }
  178. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 2 + ReservedRefCount, 2));
  179. }
  180. TEST(TIntrusiveAtomicPtrTest, BasicConst)
  181. {
  182. const TIntricateObject object;
  183. EXPECT_THAT(object, HasRefCounts(0, 0, 0));
  184. {
  185. TConstIntricateObjectPtr owningPointer(&object);
  186. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  187. EXPECT_EQ(&object, owningPointer.Get());
  188. }
  189. EXPECT_THAT(object, HasRefCounts(1, 1, 1));
  190. {
  191. TConstIntricateObjectPtr owningPointer(&object);
  192. TAtomicIntrusivePtr<const TIntricateObject> atomicPointer(owningPointer);
  193. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 1, 1));
  194. EXPECT_EQ(&object, owningPointer.Get());
  195. auto p1 = atomicPointer.Acquire();
  196. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 1, 1));
  197. p1.Reset();
  198. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 2, 1));
  199. owningPointer.Reset();
  200. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 3, 1));
  201. }
  202. EXPECT_THAT(object, HasRefCounts(2 + ReservedRefCount, 2 + ReservedRefCount, 2));
  203. }
  204. TEST(TIntrusiveAtomicPtrTest, Acquire)
  205. {
  206. TIntricateObject object;
  207. {
  208. TAtomicIntrusivePtr<TIntricateObject> atomicPtr{TIntricateObjectPtr(&object)};
  209. EXPECT_THAT(object, HasRefCounts(ReservedRefCount, 0, 0));
  210. for (int i = 0; i < ReservedRefCount / 2; ++i) {
  211. {
  212. auto tmp = atomicPtr.Acquire();
  213. EXPECT_THAT(object, HasRefCounts(ReservedRefCount, i, 0));
  214. }
  215. EXPECT_THAT(object, HasRefCounts(ReservedRefCount, i + 1, 0));
  216. }
  217. {
  218. auto tmp = atomicPtr.Acquire();
  219. EXPECT_THAT(object, HasRefCounts(ReservedRefCount + ReservedRefCount / 2, ReservedRefCount / 2, 0));
  220. }
  221. EXPECT_THAT(object, HasRefCounts(ReservedRefCount + ReservedRefCount / 2, ReservedRefCount / 2 + 1, 0));
  222. }
  223. EXPECT_THAT(object, HasRefCounts(ReservedRefCount + ReservedRefCount / 2, ReservedRefCount + ReservedRefCount / 2, 1));
  224. }
  225. TEST(TIntrusiveAtomicPtrTest, AcquireConst)
  226. {
  227. const TIntricateObject object;
  228. {
  229. TAtomicIntrusivePtr<const TIntricateObject> atomicPtr{TConstIntricateObjectPtr(&object)};
  230. EXPECT_THAT(object, HasRefCounts(ReservedRefCount, 0, 0));
  231. for (int i = 0; i < ReservedRefCount / 2; ++i) {
  232. {
  233. auto tmp = atomicPtr.Acquire();
  234. EXPECT_THAT(object, HasRefCounts(ReservedRefCount, i, 0));
  235. }
  236. EXPECT_THAT(object, HasRefCounts(ReservedRefCount, i + 1, 0));
  237. }
  238. {
  239. auto tmp = atomicPtr.Acquire();
  240. EXPECT_THAT(object, HasRefCounts(ReservedRefCount + ReservedRefCount / 2, ReservedRefCount / 2, 0));
  241. }
  242. EXPECT_THAT(object, HasRefCounts(ReservedRefCount + ReservedRefCount / 2, ReservedRefCount / 2 + 1, 0));
  243. }
  244. EXPECT_THAT(object, HasRefCounts(ReservedRefCount + ReservedRefCount / 2, ReservedRefCount + ReservedRefCount / 2, 1));
  245. }
  246. TEST(TIntrusiveAtomicPtrTest, CAS)
  247. {
  248. TIntricateObject o1;
  249. TIntricateObject o2;
  250. {
  251. TAtomicIntrusivePtr<TIntricateObject> atomicPtr{TIntricateObjectPtr(&o1)};
  252. EXPECT_THAT(o1, HasRefCounts(ReservedRefCount, 0, 0));
  253. TIntricateObjectPtr p2(&o2);
  254. EXPECT_THAT(o2, HasRefCounts(1, 0, 0));
  255. void* rawPtr = &o1;
  256. EXPECT_TRUE(atomicPtr.CompareAndSwap(rawPtr, std::move(p2)));
  257. EXPECT_EQ(rawPtr, &o1);
  258. EXPECT_THAT(o1, HasRefCounts(ReservedRefCount, ReservedRefCount, 1));
  259. EXPECT_THAT(o2, HasRefCounts(ReservedRefCount, 0, 0));
  260. rawPtr = nullptr;
  261. EXPECT_FALSE(atomicPtr.CompareAndSwap(rawPtr, TIntricateObjectPtr(&o1)));
  262. EXPECT_EQ(rawPtr, &o2);
  263. EXPECT_THAT(o1, HasRefCounts(2 * ReservedRefCount, 2 * ReservedRefCount, 2));
  264. EXPECT_THAT(o2, HasRefCounts(ReservedRefCount, 0, 0));
  265. }
  266. EXPECT_THAT(o2, HasRefCounts(ReservedRefCount, ReservedRefCount, 1));
  267. }
  268. TEST(TIntrusiveAtomicPtrTest, CASConst)
  269. {
  270. const TIntricateObject o1;
  271. const TIntricateObject o2;
  272. {
  273. TAtomicIntrusivePtr<const TIntricateObject> atomicPtr{TConstIntricateObjectPtr(&o1)};
  274. EXPECT_THAT(o1, HasRefCounts(ReservedRefCount, 0, 0));
  275. TConstIntricateObjectPtr p2(&o2);
  276. EXPECT_THAT(o2, HasRefCounts(1, 0, 0));
  277. const void* rawPtr = &o1;
  278. EXPECT_TRUE(atomicPtr.CompareAndSwap(rawPtr, std::move(p2)));
  279. EXPECT_EQ(rawPtr, &o1);
  280. EXPECT_THAT(o1, HasRefCounts(ReservedRefCount, ReservedRefCount, 1));
  281. EXPECT_THAT(o2, HasRefCounts(ReservedRefCount, 0, 0));
  282. rawPtr = nullptr;
  283. EXPECT_FALSE(atomicPtr.CompareAndSwap(rawPtr, TConstIntricateObjectPtr(&o1)));
  284. EXPECT_EQ(rawPtr, &o2);
  285. EXPECT_THAT(o1, HasRefCounts(2 * ReservedRefCount, 2 * ReservedRefCount, 2));
  286. EXPECT_THAT(o2, HasRefCounts(ReservedRefCount, 0, 0));
  287. }
  288. EXPECT_THAT(o2, HasRefCounts(ReservedRefCount, ReservedRefCount, 1));
  289. }
  290. TEST(TIntrusiveAtomicPtrTest, LSan)
  291. {
  292. struct S final
  293. { };
  294. struct TSingleton
  295. {
  296. TSingleton()
  297. {
  298. for (auto& ptr : Ptrs) {
  299. ptr.Store(New<S>());
  300. // Clobber pointer bits to prevent LSan from tracing the pointer.
  301. ptr.Acquire();
  302. }
  303. }
  304. // LSan has some issues detecting leaks when just one allocation is made.
  305. std::array<TAtomicIntrusivePtr<S>, 100> Ptrs;
  306. };
  307. LeakySingleton<TSingleton>();
  308. }
  309. #endif
  310. ////////////////////////////////////////////////////////////////////////////////
  311. } // namespace
  312. } // namespace NYT