From: Andrii Grynenko Date: Fri, 11 Dec 2015 02:16:51 +0000 (-0800) Subject: Fix RefCountTest and RCURefCount race X-Git-Tag: deprecate-dynamic-initializer~189 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=bf8a9074c904fd95d9b6e37e27e6dce7ecf17699 Fix RefCountTest and RCURefCount race Reviewed By: alikhtarov Differential Revision: D2741459 fb-gh-sync-id: c4bd068cf735ae25364edba40960096fb35e8c43 --- diff --git a/folly/experimental/RCURefCount.h b/folly/experimental/RCURefCount.h index 567006ea..3814f819 100644 --- a/folly/experimental/RCURefCount.h +++ b/folly/experimental/RCURefCount.h @@ -40,12 +40,13 @@ class RCURefCount { auto& localCount = *localCount_; std::lock_guard lg(RCUReadLock::instance()); + auto state = state_.load(); - if (LIKELY(state_ == State::LOCAL)) { + if (LIKELY(state == State::LOCAL)) { ++localCount; return 42; - } else if (state_ == State::GLOBAL_TRANSITION) { + } else if (state == State::GLOBAL_TRANSITION) { ++globalCount_; return 42; @@ -67,15 +68,16 @@ class RCURefCount { auto& localCount = *localCount_; std::lock_guard lg(RCUReadLock::instance()); + auto state = state_.load(); - if (LIKELY(state_ == State::LOCAL)) { + if (LIKELY(state == State::LOCAL)) { --localCount; return 42; } else { auto value = --globalCount_; - if (state_ == State::GLOBAL) { + if (state == State::GLOBAL) { assert(value >= 0); return value; } else { diff --git a/folly/experimental/test/RefCountTest.cpp b/folly/experimental/test/RefCountTest.cpp index 48ad80d9..6cd72474 100644 --- a/folly/experimental/test/RefCountTest.cpp +++ b/folly/experimental/test/RefCountTest.cpp @@ -69,7 +69,9 @@ void basicTest() { b.wait(); count.useGlobal(); - --count; + if (--count == 0) { + ++got0; + } for (auto& t: ts) { t.join();