From a2206d8115d4ae508a45c5960425e858808ea869 Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Fri, 26 Jun 2015 09:19:32 -0700 Subject: [PATCH] Fix stop_ thread race Summary: make it std::atomic Reviewed By: @tudor Differential Revision: D2191861 --- folly/io/async/EventBase.cpp | 3 +-- folly/io/async/EventBase.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/folly/io/async/EventBase.cpp b/folly/io/async/EventBase.cpp index b8a1c256..9280f001 100644 --- a/folly/io/async/EventBase.cpp +++ b/folly/io/async/EventBase.cpp @@ -315,8 +315,7 @@ bool EventBase::loopBody(int flags) { std::chrono::steady_clock::now().time_since_epoch()).count(); } - // TODO: Read stop_ atomically with an acquire barrier. - while (!stop_) { + while (!stop_.load(std::memory_order_acquire)) { ++nextLoopCnt_; // Run the before loop callbacks diff --git a/folly/io/async/EventBase.h b/folly/io/async/EventBase.h index 369754bd..872d7ada 100644 --- a/folly/io/async/EventBase.h +++ b/folly/io/async/EventBase.h @@ -671,7 +671,7 @@ bool runImmediatelyOrRunInEventBaseThreadAndWait(const Cob& fn); // stop_ is set by terminateLoopSoon() and is used by the main loop // to determine if it should exit - bool stop_; + std::atomic stop_; // The ID of the thread running the main loop. // 0 if loop is not running. -- 2.34.1