public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: Fix for deadlock in std::counting_semaphore [PR100806]
@ 2021-06-16 18:35 Thomas Rodgers
  2021-06-16 19:52 ` Thomas Rodgers
  0 siblings, 1 reply; 4+ messages in thread
From: Thomas Rodgers @ 2021-06-16 18:35 UTC (permalink / raw)
  To: gcc-patches, libstdc++; +Cc: trodgers, Thomas Rodgers

This is an 'interim' fix. For now it forces all waiting threads to wake
on _M_release(). This isn't exactly efficient but resolves the issue
in the immediate term.

libstdc++-v3/ChangeLog:
	libstdc++/PR100806
	* 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    |  4 +-
 .../testsuite/30_threads/semaphore/100806.cc  | 77 +++++++++++++++++++
 2 files changed, 80 insertions(+), 1 deletion(-)
 create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/100806.cc

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..483779caf0a
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc
@@ -0,0 +1,77 @@
+// Copyright (C) 2020-2021 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { 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 <array>
+#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();
+        }
+      }
+}
-- 
2.26.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] libstdc++: Fix for deadlock in std::counting_semaphore [PR100806]
  2021-06-16 18:35 [PATCH] libstdc++: Fix for deadlock in std::counting_semaphore [PR100806] Thomas Rodgers
@ 2021-06-16 19:52 ` Thomas Rodgers
  2021-06-17 16:45   ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Thomas Rodgers @ 2021-06-16 19:52 UTC (permalink / raw)
  To: gcc-patches, libstdc++; +Cc: trodgers, Thomas Rodgers

Same as previous version except removing the copyright notice from the
test.

libstdc++-v3/ChangeLog:
	libstdc++/PR100806
	* 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    |  4 +-
 .../testsuite/30_threads/semaphore/100806.cc  | 60 +++++++++++++++++++
 2 files changed, 63 insertions(+), 1 deletion(-)
 create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/100806.cc

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..938c2793be1
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc
@@ -0,0 +1,60 @@
+// { 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 <array>
+#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();
+        }
+      }
+}
-- 
2.26.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] libstdc++: Fix for deadlock in std::counting_semaphore [PR100806]
  2021-06-16 19:52 ` Thomas Rodgers
@ 2021-06-17 16:45   ` Jonathan Wakely
  2021-06-22 18:09     ` Thomas Rodgers
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2021-06-17 16:45 UTC (permalink / raw)
  To: Thomas Rodgers; +Cc: gcc-patches, libstdc++, Thomas Rodgers

On Wed, 16 Jun 2021 at 20:53, Thomas Rodgers <rodgert@appliantology.com> wrote:
>
> Same as previous version except removing the copyright notice from the
> test.
>
> libstdc++-v3/ChangeLog:
>         libstdc++/PR100806
>         * 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.

OK for trunk and 11, thanks.


> ---
>  libstdc++-v3/include/bits/semaphore_base.h    |  4 +-
>  .../testsuite/30_threads/semaphore/100806.cc  | 60 +++++++++++++++++++
>  2 files changed, 63 insertions(+), 1 deletion(-)
>  create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/100806.cc
>
> 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..938c2793be1
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc
> @@ -0,0 +1,60 @@
> +// { 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 <array>
> +#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();
> +        }
> +      }
> +}
> --
> 2.26.2
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] libstdc++: Fix for deadlock in std::counting_semaphore [PR100806]
  2021-06-17 16:45   ` Jonathan Wakely
@ 2021-06-22 18:09     ` Thomas Rodgers
  0 siblings, 0 replies; 4+ messages in thread
From: Thomas Rodgers @ 2021-06-22 18:09 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: Thomas Rodgers, gcc-patches, libstdc++

Tested x86_64-pc-linux-gnu.
Committed to master, backported to releases/gcc-11.

On Thu, Jun 17, 2021 at 9:46 AM Jonathan Wakely <jwakely.gcc@gmail.com>
wrote:

> On Wed, 16 Jun 2021 at 20:53, Thomas Rodgers <rodgert@appliantology.com>
> wrote:
> >
> > Same as previous version except removing the copyright notice from the
> > test.
> >
> > libstdc++-v3/ChangeLog:
> >         libstdc++/PR100806
> >         * 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.
>
> OK for trunk and 11, thanks.
>
>
> > ---
> >  libstdc++-v3/include/bits/semaphore_base.h    |  4 +-
> >  .../testsuite/30_threads/semaphore/100806.cc  | 60 +++++++++++++++++++
> >  2 files changed, 63 insertions(+), 1 deletion(-)
> >  create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/100806.cc
> >
> > 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..938c2793be1
> > --- /dev/null
> > +++ b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc
> > @@ -0,0 +1,60 @@
> > +// { 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 <array>
> > +#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();
> > +        }
> > +      }
> > +}
> > --
> > 2.26.2
> >
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-22 18:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-16 18:35 [PATCH] libstdc++: Fix for deadlock in std::counting_semaphore [PR100806] Thomas Rodgers
2021-06-16 19:52 ` Thomas Rodgers
2021-06-17 16:45   ` Jonathan Wakely
2021-06-22 18:09     ` Thomas Rodgers

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).