public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Rodgers <rodgertq@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r12-1732] libstdc++: Fix for deadlock in std::counting_semaphore [PR100806]
Date: Tue, 22 Jun 2021 18:07:06 +0000 (GMT)	[thread overview]
Message-ID: <20210622180706.69AF33858023@sourceware.org> (raw)

https://gcc.gnu.org/g:e02840c1a92abecd211ffaf05b28329bcb534583

commit r12-1732-ge02840c1a92abecd211ffaf05b28329bcb534583
Author: Thomas Rodgers <rodgert@appliantology.com>
Date:   Tue Jun 22 10:59:07 2021 -0700

    libstdc++: Fix for deadlock in std::counting_semaphore [PR100806]
    
    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.

Diff:
---
 libstdc++-v3/include/bits/semaphore_base.h         |  4 +-
 .../testsuite/30_threads/semaphore/100806.cc       | 57 ++++++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/bits/semaphore_base.h b/libstdc++-v3/include/bits/semaphore_base.h
index 9a55978068f..c4565d7e560 100644
--- a/libstdc++-v3/include/bits/semaphore_base.h
+++ b/libstdc++-v3/include/bits/semaphore_base.h
@@ -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
index 00000000000..2fa2628a278
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc
@@ -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();
+	}
+      }
+}


                 reply	other threads:[~2021-06-22 18:07 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210622180706.69AF33858023@sourceware.org \
    --to=rodgertq@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).