Skip to content

Commit

Permalink
adjust memory order in Semaphore::signalSlow
Browse files Browse the repository at this point in the history
Summary: `signalSlow()` updates `tokens_` with a relaxed write when the wait list is empty. This won't synchronize with the acquire loads performed by calls to `wait()`. TSAN detects this: D66112566.

Reviewed By: yfeldblum

Differential Revision: D66114255

fbshipit-source-id: ce6f937b58fe0d004c1d4582db7e2cc3c1bfd6d6
  • Loading branch information
David Geraghty (SEA) authored and facebook-github-bot committed Nov 22, 2024
1 parent 7d79235 commit 611cfd8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion folly/fibers/Semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool Semaphore::signalSlow() {
// If the waitlist is now empty, ensure the token count increments
// No need for CAS here as we will always be under the mutex
CHECK(tokens_.compare_exchange_strong(
testVal, testVal + 1, std::memory_order_relaxed));
testVal, testVal + 1, std::memory_order_release));
return true;
}
waiter = &waitList.front();
Expand Down
12 changes: 12 additions & 0 deletions folly/fibers/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,15 @@ cpp_binary(
"//folly/init:init",
],
)

cpp_unittest(
name = "semaphore_test",
srcs = ["SemaphoreTest.cpp"],
headers = [],
deps = [
"//folly/fibers:semaphore",
"//folly/portability:gtest",
"//folly/synchronization:relaxed_atomic",
"//folly/synchronization/detail:sleeper",
],
)
46 changes: 46 additions & 0 deletions folly/fibers/test/SemaphoreTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <folly/fibers/Semaphore.h>
#include <folly/portability/GTest.h>
#include <folly/synchronization/RelaxedAtomic.h>
#include <folly/synchronization/detail/Sleeper.h>

using namespace folly::fibers;

TEST(SemaphoreTest, MessagePassing) {
int data = 0;
Semaphore sem{0};

// Provides no memory ordering: just used to reproduce conditions for a
// bug caught by TSAN in an earlier version of the implementation.
folly::relaxed_atomic<bool> signalled{false};

std::thread t{[&]() {
folly::detail::Sleeper sleeper;
while (!signalled) {
sleeper.wait();
}
sem.wait();
EXPECT_NE(0, data);
}};

data = 1;
sem.signal();
signalled = true;

t.join();
}

0 comments on commit 611cfd8

Please sign in to comment.