commit:     ae4a95da4cdb44ac97adfc4c48d89317c315b5d1
Author:     Sam James <sam <AT> gentoo <DOT> org>
AuthorDate: Sat Nov 19 02:27:29 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat Nov 19 03:19:13 2022 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=ae4a95da

dev-cpp/tbb: backport pthread EAGAIN patch for mold

Closes: https://bugs.gentoo.org/881161
Signed-off-by: Sam James <sam <AT> gentoo.org>

 .../tbb/files/tbb-2021.7.0-pthread-eagain.patch    | 135 +++++++++++++++++++++
 dev-cpp/tbb/tbb-2021.7.0-r1.ebuild                 |  41 +++++++
 2 files changed, 176 insertions(+)

diff --git a/dev-cpp/tbb/files/tbb-2021.7.0-pthread-eagain.patch 
b/dev-cpp/tbb/files/tbb-2021.7.0-pthread-eagain.patch
new file mode 100644
index 000000000000..6c0912ec96b7
--- /dev/null
+++ b/dev-cpp/tbb/files/tbb-2021.7.0-pthread-eagain.patch
@@ -0,0 +1,135 @@
+https://bugs.gentoo.org/881161
+https://github.com/rui314/mold/issues/410
+https://github.com/oneapi-src/oneTBB/commit/ceacd2207edfb72a8fc235213265afe68ce74ad0
+https://github.com/oneapi-src/oneTBB/commit/137c1a88b690acf3525e0f279720ac489ce66481
+
+From ceacd2207edfb72a8fc235213265afe68ce74ad0 Mon Sep 17 00:00:00 2001
+From: Ilya Isaev <[email protected]>
+Date: Wed, 26 Oct 2022 13:13:51 +0200
+Subject: [PATCH] Rework test_eh_thread to avoid sporadic failures (#946)
+
+Signed-off-by: Isaev, Ilya <[email protected]>
+--- a/test/tbb/test_eh_thread.cpp
++++ b/test/tbb/test_eh_thread.cpp
+@@ -54,15 +54,16 @@ void limitThreads(size_t limit)
+     CHECK_MESSAGE(0 == ret, "setrlimit has returned an error");
+ }
+ 
+-static bool g_exception_caught = false;
+-static std::mutex m;
+-static std::condition_variable cv;
+-static std::atomic<bool> stop{ false };
++size_t getThreadLimit() {
++    rlimit rlim;
++
++    int ret = getrlimit(RLIMIT_NPROC, &rlim);
++    CHECK_MESSAGE(0 == ret, "getrlimit has returned an error");
++    return rlim.rlim_cur;
++}
+ 
+ static void* thread_routine(void*)
+ {
+-    std::unique_lock<std::mutex> lock(m);
+-    cv.wait(lock, [] { return stop == true; });
+     return nullptr;
+ }
+ 
+@@ -94,32 +95,17 @@ TEST_CASE("Too many threads") {
+     }
+ 
+     // Some systems set really big limit (e.g. >45К) for the number of 
processes/threads
+-    limitThreads(1024);
+-
+-    std::thread /* isolate test */ ([] {
+-        std::vector<Thread> threads;
+-        stop = false;
+-        auto finalize = [&] {
+-            stop = true;
+-            cv.notify_all();
+-            for (auto& t : threads) {
+-                t.join();
+-            }
+-        };
+-
+-        for (int i = 0;; ++i) {
++    limitThreads(1);
++    if (getThreadLimit() == 1) {
++        for (int attempt = 0; attempt < 5; ++attempt) {
+             Thread thread;
+-            if (!thread.isValid()) {
+-                break;
+-            }
+-            threads.push_back(thread);
+-            if (i == 1024) {
+-                WARN_MESSAGE(false, "setrlimit seems having no effect");
+-                finalize();
++            if (thread.isValid()) {
++                WARN_MESSAGE(false, "We were able to create a thread. 
setrlimit seems having no effect");
++                thread.join();
+                 return;
+             }
+         }
+-        g_exception_caught = false;
++        bool g_exception_caught = false;
+         try {
+             // Initialize the library to create worker threads
+             tbb::parallel_for(0, 2, [](int) {});
+@@ -132,9 +118,10 @@ TEST_CASE("Too many threads") {
+         }
+         // Do not CHECK to avoid memory allocation (we can be out of memory)
+         if (!g_exception_caught) {
+-            FAIL("No exception was caught");
++            FAIL("No exception was thrown on library initialization");
+         }
+-        finalize();
+-    }).join();
++    } else {
++        WARN_MESSAGE(false, "setrlimit seems having no effect");
++    }
+ }
+ #endif
+
+From 137c1a88b690acf3525e0f279720ac489ce66481 Mon Sep 17 00:00:00 2001
+From: Rui Ueyama <[email protected]>
+Date: Wed, 26 Oct 2022 04:54:20 -0700
+Subject: [PATCH] Retry if pthread_create fails with EAGAIN (#824)
+
+Signed-off-by: Rui Ueyama <[email protected]>
+--- a/src/tbb/rml_thread_monitor.h
++++ b/src/tbb/rml_thread_monitor.h
+@@ -31,6 +31,7 @@
+ #include <pthread.h>
+ #include <cstring>
+ #include <cstdlib>
++#include <time.h>
+ #else
+ #error Unsupported platform
+ #endif
+@@ -191,8 +192,25 @@ inline thread_monitor::handle_type 
thread_monitor::launch( void* (*thread_routin
+     check(pthread_attr_init( &s ), "pthread_attr_init has failed");
+     if( stack_size>0 )
+         check(pthread_attr_setstacksize( &s, stack_size ), 
"pthread_attr_setstack_size has failed" );
++
++    // pthread_create(2) can spuriously fail with EAGAIN. We retry
++    // max_num_tries times with progressively longer wait times.
+     pthread_t handle;
+-    check( pthread_create( &handle, &s, thread_routine, arg ), 
"pthread_create has failed" );
++    const int max_num_tries = 20;
++    int error = EAGAIN;
++
++    for (int i = 0; i < max_num_tries && error == EAGAIN; i++) {
++      if (i != 0) {
++        // Wait i milliseconds
++        struct timespec ts = {0, i * 1000 * 1000};
++        nanosleep(&ts, NULL);
++      }
++      error = pthread_create(&handle, &s, thread_routine, arg);
++    }
++
++    if (error)
++      handle_perror(error, "pthread_create has failed");
++
+     check( pthread_attr_destroy( &s ), "pthread_attr_destroy has failed" );
+     return handle;
+ }
+

diff --git a/dev-cpp/tbb/tbb-2021.7.0-r1.ebuild 
b/dev-cpp/tbb/tbb-2021.7.0-r1.ebuild
new file mode 100644
index 000000000000..d88f920769a0
--- /dev/null
+++ b/dev-cpp/tbb/tbb-2021.7.0-r1.ebuild
@@ -0,0 +1,41 @@
+# Copyright 1999-2022 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=8
+
+inherit cmake-multilib flag-o-matic
+
+DESCRIPTION="High level abstract threading library"
+HOMEPAGE="https://www.threadingbuildingblocks.org";
+SRC_URI="https://github.com/oneapi-src/oneTBB/archive/refs/tags/v${PV}.tar.gz 
-> ${P}.tar.gz"
+S="${WORKDIR}/oneTBB-${PV}"
+
+LICENSE="Apache-2.0"
+# https://github.com/oneapi-src/oneTBB/blob/master/CMakeLists.txt#L53
+# libtbb<SONAME>-libtbbmalloc<SONAME>-libtbbbind<SONAME>
+SLOT="0/12.5-2.5-3.5"
+KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~ppc ~ppc64 ~riscv ~sparc ~x86 
~amd64-linux ~x86-linux ~x64-macos"
+IUSE="test"
+RESTRICT="!test? ( test )"
+
+RDEPEND="!kernel_Darwin? ( sys-apps/hwloc:= )"
+DEPEND="${RDEPEND}"
+BDEPEND="virtual/pkgconfig"
+
+PATCHES=(
+       "${FILESDIR}"/${PN}-2021.7.0-pthread-eagain.patch
+)
+
+src_configure() {
+       # bug #872287
+       filter-flags -D_GLIBCXX_ASSERTIONS
+       append-cppflags -U_GLIBCXX_ASSERTIONS
+
+       local mycmakeargs=(
+               -DTBB_TEST=$(usex test)
+               -DTBB_ENABLE_IPO=OFF
+               -DTBB_STRICT=OFF
+       )
+
+       cmake-multilib_src_configure
+}

Reply via email to