(Wangle) Fix possible race in updating FSM state
authorHannes Roth <hannesr@fb.com>
Thu, 25 Jun 2015 15:44:29 +0000 (08:44 -0700)
committerSara Golemon <sgolemon@fb.com>
Fri, 26 Jun 2015 18:45:38 +0000 (11:45 -0700)
Summary: Storing the new state could be a memory race according to C++ (but wasn't in practice). I only checked GCC though.

Reviewed By: @nbronson

Differential Revision: D2189287

folly/futures/detail/FSM.h

index ce77c551173637f85efe3f8624432c577b172056..b706ed04503e414ed780985a14dee70b980c564d 100644 (file)
@@ -55,12 +55,12 @@ public:
     if (!mutex_.try_lock()) {
       mutex_.lock();
     }
-    if (state_.load(std::memory_order_relaxed) != A) {
+    if (state_.load(std::memory_order_acquire) != A) {
       mutex_.unlock();
       return false;
     }
     action();
-    state_.store(B, std::memory_order_relaxed);
+    state_.store(B, std::memory_order_release);
     mutex_.unlock();
     return true;
   }