Relax stop_ memory order
authorDave Watson <davejwatson@fb.com>
Thu, 11 Jan 2018 16:00:19 +0000 (08:00 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 11 Jan 2018 16:11:54 +0000 (08:11 -0800)
Summary: stop_ can be relaxed everywhere, it implies only an asynchronous signal, not any sort of memory barrier.

Reviewed By: davidtgoldblatt

Differential Revision: D6690079

fbshipit-source-id: 6f193204e1619f9a8adf81db2d46d05954bfbf85

folly/io/async/EventBase.cpp

index 1466d72159f3ac1d74939dd0d9fef46889beaed3..9ca4ab014b141bf8ad885b17de11974724365ffa 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2017-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 #ifndef __STDC_FORMAT_MACROS
 #define __STDC_FORMAT_MACROS
 #endif
 #ifndef __STDC_FORMAT_MACROS
 #define __STDC_FORMAT_MACROS
 #endif
@@ -294,7 +293,7 @@ bool EventBase::loopBody(int flags) {
     idleStart = std::chrono::steady_clock::now();
   }
 
     idleStart = std::chrono::steady_clock::now();
   }
 
-  while (!stop_.load(std::memory_order_acquire)) {
+  while (!stop_.load(std::memory_order_relaxed)) {
     applyLoopKeepAlive();
     ++nextLoopCnt_;
 
     applyLoopKeepAlive();
     ++nextLoopCnt_;
 
@@ -385,7 +384,7 @@ bool EventBase::loopBody(int flags) {
     }
   }
   // Reset stop_ so loop() can be called again
     }
   }
   // Reset stop_ so loop() can be called again
-  stop_ = false;
+  stop_.store(false, std::memory_order_relaxed);
 
   if (res < 0) {
     LOG(ERROR) << "EventBase: -- error in event loop, res = " << res;
 
   if (res < 0) {
     LOG(ERROR) << "EventBase: -- error in event loop, res = " << res;
@@ -477,9 +476,7 @@ void EventBase::terminateLoopSoon() {
   VLOG(5) << "EventBase(): Received terminateLoopSoon() command.";
 
   // Set stop to true, so the event loop will know to exit.
   VLOG(5) << "EventBase(): Received terminateLoopSoon() command.";
 
   // Set stop to true, so the event loop will know to exit.
-  // TODO: We should really use an atomic operation here with a release
-  // barrier.
-  stop_ = true;
+  stop_.store(true, std::memory_order_relaxed);
 
   // Call event_base_loopbreak() so that libevent will exit the next time
   // around the loop.
 
   // Call event_base_loopbreak() so that libevent will exit the next time
   // around the loop.