[libcxx-commits] [PATCH] D114119: [libcxx] Fix potential lost wake-up in counting semaphore

Fabian Wolff via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 17 14:46:24 PST 2021


fwolff updated this revision to Diff 388044.
fwolff added a comment.

I've added a simple test, although I couldn't reproduce the issue locally either.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114119/new/

https://reviews.llvm.org/D114119

Files:
  libcxx/include/semaphore
  libcxx/test/std/thread/thread.semaphore/lost_wakeup.pass.cpp


Index: libcxx/test/std/thread/thread.semaphore/lost_wakeup.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/std/thread/thread.semaphore/lost_wakeup.pass.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// REQUIRES: long_tests
+// UNSUPPORTED: libcpp-has-no-threads
+// UNSUPPORTED: c++03, c++11
+
+// This is a regression test for PR#47013.
+
+// <semaphore>
+
+#include <barrier>
+#include <semaphore>
+#include <thread>
+#include <vector>
+
+static std::counting_semaphore s(0);
+static std::barrier b(8 + 2);
+
+void acquire() {
+  for (auto i = 0; i < 100'000; ++i) {
+    s.acquire();
+    b.arrive_and_wait();
+  }
+}
+
+void release() {
+  for (auto i = 0; i < 100'000; ++i) {
+    for (auto j = 0; j < 4; ++j)
+      s.release(1);
+    b.arrive_and_wait();
+  }
+}
+
+int main(int, char**) {
+  std::vector<std::thread> threads;
+
+  for (auto i = 0; i < 8; ++i)
+    threads.emplace_back(acquire);
+  for (auto i = 0; i < 2; ++i)
+    threads.emplace_back(release);
+
+  for (auto& thread : threads)
+    thread.join();
+
+  return 0;
+}
Index: libcxx/include/semaphore
===================================================================
--- libcxx/include/semaphore
+++ libcxx/include/semaphore
@@ -87,10 +87,9 @@
     {
         if(0 < __a.fetch_add(__update, memory_order_release))
             ;
-        else if(__update > 1)
-            __a.notify_all();
         else
-            __a.notify_one();
+            // Always notify all, regardless of the value of __update (see PR47013)
+            __a.notify_all();
     }
     _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
     void acquire()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114119.388044.patch
Type: text/x-patch
Size: 2027 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211117/4a47a4bd/attachment.bin>


More information about the libcxx-commits mailing list