From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id A81783858D37; Fri, 28 Apr 2023 12:31:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A81783858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682685082; bh=1OWYLvP2bSNytDPw/IWB41Yt2OD/uq/dVwBE7JXf878=; h=From:To:Subject:Date:From; b=UsIQ5Q3XdtaYRZKuUMZIzS/mWHb3CuvNmNFsCZhAfqibPpic7Mss0V7fBhqQlkdy3 x09m+kf1X6yOXXOve93mH+3iRIlIlu6mc/+TUjRQg8xEiWCVtBXCV4aCyNEbCA3EjY SU8uCPnpkM/JN5SheK/g/+tJePsdeZG5tA/LuWOI= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r10-11325] libstdc++: Throw instead of segfaulting in std::thread constructor [PR 67791] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 256c9824a5c2d8def1105f077327e097657905d0 X-Git-Newrev: e2babb0540cfa66752214f3f111461e1da4b26ce Message-Id: <20230428123122.A81783858D37@sourceware.org> Date: Fri, 28 Apr 2023 12:31:22 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e2babb0540cfa66752214f3f111461e1da4b26ce commit r10-11325-ge2babb0540cfa66752214f3f111461e1da4b26ce Author: Jonathan Wakely Date: Tue Nov 24 12:48:31 2020 +0000 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. (cherry picked from commit 4bbd5d0c5fb2b7527938ad44a6d8a2f2ef8bbe12) Diff: --- libstdc++-v3/src/c++11/thread.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libstdc++-v3/src/c++11/thread.cc b/libstdc++-v3/src/c++11/thread.cc index 2d8781724f2..b175ad5413e 100644 --- a/libstdc++-v3/src/c++11/thread.cc +++ b/libstdc++-v3/src/c++11/thread.cc @@ -132,6 +132,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());