From b52e2a6e271b5e7a5865a2f934540822c7222643 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Wed, 25 Aug 2021 11:26:44 +0100 Subject: [PATCH] Expose pthread feature flag The code was able to use pthread destructors rather than C++ thread local destructors. This removes the dependence on a C++ .so on linux. However, this is not stable on other platforms such as Apple. Where the C++ thread local state can be cleared before the pthread destructor runs. --- .github/workflows/main.yml | 11 +++++++++-- CMakeLists.txt | 5 +++++ src/mem/threadalloc.h | 3 ++- .../thread_alloc_external/thread_alloc_external.cc | 4 ++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index feabade..c04e28d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -38,16 +38,23 @@ jobs: build-type: Debug build-only: yes # Add the self-host build - - os: ubuntu-latest + - os: "ubuntu-latest" build-type: Debug self-host: true + # Extra build to check using pthread library for destructing local state. + - os: "ubuntu-latest" + variant: "Ubuntu (with pthread destructors)." + dependencies: "sudo apt install ninja-build" + build-type: Debug + self-host: true + extra-cmake-flags: "-DSNMALLOC_USE_PTHREAD_DESTRUCTORS=On -DCMAKE_CXX_COMPILER=clang++-10 -DCMAKE_C_COMPILER=clang-10" # Add an extra element to the matrix that does a build with clang 12 # but doesn't run tests. - os: "freebsd-13.0" variant: Clang 12 (Build only) extra-cmake-flags: "-DCMAKE_CXX_COMPILER=clang++12" build-only: yes - - os: ubuntu-latest + - os: "ubuntu-latest" variant: Clang 10 libstdc++ (Build only) dependencies: "sudo apt install ninja-build" extra-cmake-flags: "-DCMAKE_CXX_COMPILER=clang++-10 -DCMAKE_C_COMPILER=clang-10 -DCMAKE_CXX_FLAGS=-stdlib=libstdc++" diff --git a/CMakeLists.txt b/CMakeLists.txt index c313118..977b31c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ option(SNMALLOC_QEMU_WORKAROUND "Disable using madvise(DONT_NEED) to zero memory option(SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE "Compile for current machine architecture" Off) set(SNMALLOC_STATIC_LIBRARY_PREFIX "sn_" CACHE STRING "Static library function prefix") option(SNMALLOC_USE_CXX17 "Build as C++17 for legacy support." OFF) +option(SNMALLOC_USE_PTHREAD_DESTRUCTORS "Build using pthread destructors. Reduces the system dependencies, but may interact badly with C++ on some platforms." OFF) # malloc.h will error if you include it on FreeBSD, so this test must not # unconditionally include it. @@ -150,6 +151,10 @@ if(SNMALLOC_PLATFORM_HAS_GETENTROPY) target_compile_definitions(snmalloc_lib INTERFACE -DSNMALLOC_PLATFORM_HAS_GETENTROPY) endif() +if(SNMALLOC_USE_PTHREAD_DESTRUCTORS) + target_compile_definitions(snmalloc_lib INTERFACE -DSNMALLOC_USE_PTHREAD_DESTRUCTORS) +endif() + if(CONST_QUALIFIED_MALLOC_USABLE_SIZE) target_compile_definitions(snmalloc_lib INTERFACE -DMALLOC_USABLE_SIZE_QUALIFIER=const) endif() diff --git a/src/mem/threadalloc.h b/src/mem/threadalloc.h index 3777d44..3c75c42 100644 --- a/src/mem/threadalloc.h +++ b/src/mem/threadalloc.h @@ -19,6 +19,7 @@ # if defined(SNMALLOC_THREAD_TEARDOWN_DEFINED) # error At most one out of method of thread teardown can be specified. # else +# include # define SNMALLOC_THREAD_TEARDOWN_DEFINED # endif #endif @@ -95,7 +96,7 @@ namespace snmalloc } }; -# ifdef SNMALLOC_USE_PTHREAD_DESTRUCTOR +# ifdef SNMALLOC_USE_PTHREAD_DESTRUCTORS /** * Used to give correct signature to teardown required by pthread_key. */ diff --git a/src/test/func/thread_alloc_external/thread_alloc_external.cc b/src/test/func/thread_alloc_external/thread_alloc_external.cc index effb62f..832d400 100644 --- a/src/test/func/thread_alloc_external/thread_alloc_external.cc +++ b/src/test/func/thread_alloc_external/thread_alloc_external.cc @@ -1,3 +1,7 @@ +#ifdef SNMALLOC_USE_PTHREAD_DESTRUCTORS +# undef SNMALLOC_USE_PTHREAD_DESTRUCTORS +#endif + #include #include