diff --git a/third-party/folly/src/folly/fibers/SemaphoreBase.cpp b/third-party/folly/src/folly/fibers/SemaphoreBase.cpp index a6ad891a7d9b27..5832f5e9bd859a 100644 --- a/third-party/folly/src/folly/fibers/SemaphoreBase.cpp +++ b/third-party/folly/src/folly/fibers/SemaphoreBase.cpp @@ -34,7 +34,7 @@ bool SemaphoreBase::signalSlow(int64_t tokens) { // waitlist, ensure the token count increments. No need for CAS here as // we will always be under the mutex if (tokens_.compare_exchange_strong( - testVal, testVal + tokens, std::memory_order_relaxed)) { + testVal, testVal + tokens, std::memory_order_release)) { return true; } continue; diff --git a/third-party/folly/src/folly/fibers/test/BatchSemaphoreTest.cpp b/third-party/folly/src/folly/fibers/test/BatchSemaphoreTest.cpp new file mode 100644 index 00000000000000..25f0d5a2f52a6f --- /dev/null +++ b/third-party/folly/src/folly/fibers/test/BatchSemaphoreTest.cpp @@ -0,0 +1,38 @@ +/* + * 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 +#include + +TEST(BatchSemaphoreTest, WaitSignalSynchronization) { + folly::fibers::BatchSemaphore sem{0}; + + int64_t data = 0; + folly::relaxed_atomic_bool signalled = false; + + std::jthread t{[&]() { + while (!signalled) { + std::this_thread::yield(); + } + + sem.wait(1); + EXPECT_NE(data, 0); + }}; + + data = 1; + sem.signal(1); + signalled = true; +}