public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Throw instead of segfaulting in std::thread constructor [PR 67791]
@ 2020-11-24 15:00 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2020-11-24 15:00 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 559 bytes --]

This turns a mysterious segfault into an exception with a more useful
message. If the exception isn't caught, the user sees this instead of
just a segfault:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

libstdc++-v3/ChangeLog:

	PR libstdc++/67791
	* src/c++11/thread.cc (thread::_M_start_thread(_State_ptr, void (*)())):
	Check that gthreads is available before calling __gthread_create.

Tested powerpc64le-linux. Committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1475 bytes --]

commit 4bbd5d0c5fb2b7527938ad44a6d8a2f2ef8bbe12
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Nov 24 12:48:31 2020

    libstdc++: Throw instead of segfaulting in std::thread constructor [PR 67791]
    
    This turns a mysterious segfault into an exception with a more useful
    message. If the exception isn't caught, the user sees this instead of
    just a segfault:
    
    terminate called after throwing an instance of 'std::system_error'
      what():  Enable multithreading to use std::thread: Operation not permitted
    Aborted (core dumped)
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/67791
            * src/c++11/thread.cc (thread::_M_start_thread(_State_ptr, void (*)())):
            Check that gthreads is available before calling __gthread_create.

diff --git a/libstdc++-v3/src/c++11/thread.cc b/libstdc++-v3/src/c++11/thread.cc
index e4dd1687a4b2..a9c928049599 100644
--- a/libstdc++-v3/src/c++11/thread.cc
+++ b/libstdc++-v3/src/c++11/thread.cc
@@ -133,6 +133,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   void
   thread::_M_start_thread(_State_ptr state, void (*)())
   {
+    if (!__gthread_active_p())
+      {
+#if __cpp_exceptions
+	throw system_error(make_error_code(errc::operation_not_permitted),
+			   "Enable multithreading to use std::thread");
+#else
+	__builtin_abort();
+#endif
+      }
+
     const int err = __gthread_create(&_M_id._M_thread,
 				     &execute_native_thread_routine,
 				     state.get());

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-11-24 15:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-24 15:00 [committed] libstdc++: Throw instead of segfaulting in std::thread constructor [PR 67791] Jonathan Wakely

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