libstdc++: Fix for deadlock in std::counting_semaphore [PR100806]
authorThomas Rodgers <rodgert@appliantology.com>
Tue, 22 Jun 2021 17:59:07 +0000 (10:59 -0700)
committerThomas Rodgers <rodgert@appliantology.com>
Tue, 22 Jun 2021 18:06:07 +0000 (11:06 -0700)
libstdc++-v3/ChangeLog:
PR libstdc++/100806
* include/bits/semaphore_base.h (__atomic_semaphore::_M_release):
Force _M_release() to wake all waiting threads.
* testsuite/30_threads/semaphore/100806.cc: New test.

libstdc++-v3/include/bits/semaphore_base.h
libstdc++-v3/testsuite/30_threads/semaphore/100806.cc [new file with mode: 0644]

index 9a55978..c4565d7 100644 (file)
@@ -256,7 +256,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       if (__update > 1)
        __atomic_notify_address_bare(&_M_counter, true);
       else
-       __atomic_notify_address_bare(&_M_counter, false);
+       __atomic_notify_address_bare(&_M_counter, true);
+// FIXME - Figure out why this does not wake a waiting thread
+//     __atomic_notify_address_bare(&_M_counter, false);
     }
 
   private:
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc
new file mode 100644 (file)
index 0000000..2fa2628
--- /dev/null
@@ -0,0 +1,57 @@
+// { dg-options "-std=gnu++2a -pthread" }
+// { dg-do run { target c++2a } }
+// { dg-require-effective-target pthread }
+// { dg-require-gthreads "" }
+// { dg-add-options libatomic }
+
+#include <iostream>
+#include <sstream>
+
+#include <thread>
+#include <semaphore>
+#include <mutex>
+#include <chrono>
+#include <vector>
+
+std::counting_semaphore<4> semaphore{6};
+
+std::mutex mtx;
+std::vector<std::string> results;
+
+void thread_main(size_t x)
+{
+  semaphore.acquire();
+  std::this_thread::sleep_for(std::chrono::milliseconds(100));
+  semaphore.release();
+  {
+    std::ostringstream stm;
+    stm << "Thread " << x << " finished.";
+    std::lock_guard g{ mtx };
+    results.push_back(stm.str());
+  }
+}
+
+int main()
+{
+    constexpr auto nthreads = 10;
+
+    std::vector<std::thread> threads(nthreads);
+
+    size_t counter{0};
+    for(auto& t : threads)
+      {
+       t = std::thread(thread_main, counter++);
+      }
+
+    for(auto& t : threads)
+      {
+       t.join();
+       {
+         std::lock_guard g{ mtx };
+         for (auto&& r : results)
+           std::cout << r << '\n';
+         std::cout.flush();
+         results.clear();
+       }
+      }
+}