X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Fexperimental%2Ftest%2FRefCountTest.cpp;h=7a6fc8103d2fa68ec1d0f4cd856f71fe8899c3a6;hb=61d6143a96e87b2bea87514e0d545799ca3b77de;hp=b9ef475bbdda32c8c6a1f6c86dc9e6a453b07382;hpb=321542683a01c3f334047531e9b487f047129775;p=folly.git diff --git a/folly/experimental/test/RefCountTest.cpp b/folly/experimental/test/RefCountTest.cpp index b9ef475b..7a6fc810 100644 --- a/folly/experimental/test/RefCountTest.cpp +++ b/folly/experimental/test/RefCountTest.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. @@ -18,8 +18,7 @@ #include #include #include - -#include +#include namespace folly { @@ -83,6 +82,40 @@ void basicTest() { EXPECT_EQ(0, ++count); } +template +void stressTest() { + constexpr size_t kItersCount = 10000; + + for (size_t i = 0; i < kItersCount; ++i) { + RefCount count; + std::mutex mutex; + int a{1}; + + std::thread t1([&]() { + if (++count) { + { + std::lock_guard lg(mutex); + EXPECT_EQ(1, a); + } + --count; + } + }); + + std::thread t2([&]() { + count.useGlobal(); + if (--count == 0) { + std::lock_guard lg(mutex); + a = 0; + } + }); + + t1.join(); + t2.join(); + + EXPECT_EQ(0, ++count); + } +} + TEST(RCURefCount, Basic) { basicTest(); } @@ -91,4 +124,11 @@ TEST(TLRefCount, Basic) { basicTest(); } +TEST(RCURefCount, Stress) { + stressTest(); +} + +TEST(TLRefCount, Stress) { + stressTest(); +} }