Commit 4bd057b2 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Fix missing "last-minute-reprieve" load-acquire in Baton and SaturatingSemaphore

Summary:
[Folly] Fix missing "last-minute-reprieve" load-acquire in `Baton` and `SaturatingSemaphore`.

If the CAS fails, it is because `post` with its store-release was invoked just after spinning but just before parking the thread. So the failure load needs a matching load-acquire.

We cannot actually move the load-acquire into the CAS until C++17, since the failure memory order must be not stronger than the success memory order until C++17. Therefore, simulate the effect with an acquire fence in the CAS failure case.

The CAS success case does not need a load-acquire; there is already a load-acquire after the thread is unparked.

Reviewed By: davidtgoldblatt

Differential Revision: D6851304

fbshipit-source-id: a43243ad6a35bd9a53a9d2a5958b50a6c8806419
parent 7052c751
......@@ -281,6 +281,8 @@ class Baton {
std::memory_order_relaxed)) {
// CAS failed, last minute reprieve
assert(expected == EARLY_DELIVERY);
// TODO: move the acquire to the compare_exchange failure load after C++17
std::atomic_thread_fence(std::memory_order_acquire);
return true;
}
......
......@@ -296,6 +296,8 @@ FOLLY_NOINLINE bool SaturatingSemaphore<MayBlock, Atom>::tryWaitSlow(
std::memory_order_relaxed,
std::memory_order_relaxed)) {
if (before == READY) {
// TODO: move the acquire to the compare_exchange failure load after C++17
std::atomic_thread_fence(std::memory_order_acquire);
return true;
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment