From 3dea8e210a6116e9b1291dc857eeabda94cbe649 Mon Sep 17 00:00:00 2001 From: Erik Rigtorp Date: Tue, 17 Nov 2026 18:48:41 -0500 Subject: [PATCH] Modernize CMake packaging and require C++20 --- .github/workflows/ccpp.yml | 58 ------------------ .github/workflows/ci.yml | 16 +++++ .gitignore | 3 +- CMakeLists.txt | 116 ++++++++++++++++++++---------------- CMakePresets.json | 72 ++++++++++++++++++++++ README.md | 4 +- include/rigtorp/MPMCQueue.h | 8 +-- src/MPMCQueueTest.cpp | 22 +++---- 8 files changed, 171 insertions(+), 128 deletions(-) delete mode 100644 .github/workflows/ccpp.yml create mode 100644 .github/workflows/ci.yml create mode 100644 CMakePresets.json diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml deleted file mode 100644 index 4695b8d..0000000 --- a/.github/workflows/ccpp.yml +++ /dev/null @@ -1,58 +0,0 @@ -name: C/C++ CI - -on: [push] - -jobs: - build-ubuntu: - - runs-on: ubuntu-latest - strategy: - matrix: - config: [Debug, Release] - standard: [11, 17, 23] - - steps: - - uses: actions/checkout@v1 - - name: Build & Test - run: | - cmake -E remove_directory build - cmake -B build -S . -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DCMAKE_CXX_FLAGS="-Werror -fsanitize=address,undefined -Wno-interference-size" - cmake --build build - cd build - ctest --output-on-failure - - build-windows: - - runs-on: windows-latest - strategy: - matrix: - config: [Debug, Release] - standard: [11, 17, 23] - - steps: - - uses: actions/checkout@v1 - - name: Build & Test - run: | - cmake -E remove_directory build - cmake -B build -S . -DCMAKE_CXX_STANDARD=${{ matrix.standard }} # -DCMAKE_CXX_FLAGS="/WX" - cmake --build build --config ${{ matrix.config }} - cd build - ctest --output-on-failure - - build-macos: - - runs-on: macOS-latest - strategy: - matrix: - config: [Debug, Release] - standard: [11, 17, 23] - - steps: - - uses: actions/checkout@v1 - - name: Build & Test - run: | - cmake -E remove_directory build - cmake -B build -S . -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DCMAKE_CXX_FLAGS="-Werror -fsanitize=address,undefined" - cmake --build build - cd build - ctest --output-on-failure \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..20f9faf --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,16 @@ +name: CI + +on: + push: + branches: [master, main] + pull_request: + +jobs: + ci: + uses: rigtorp/.github/.github/workflows/cpp-ci.yml@master + with: + cxx-standards: '["20"]' + run-sanitizers: true + enable-windows: true + enable-macos: true + warnings-as-errors: true diff --git a/.gitignore b/.gitignore index 1ad1abb..29f8640 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ cmake-build-* .vscode/ .idea/ .project -*~ \ No newline at end of file +*~ +CMakeUserPresets.json \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index fc21baa..69b6b8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,71 +1,83 @@ -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.24...4.3) -project(MPMCQueue VERSION 1.0 LANGUAGES CXX) +project(MPMCQueue + VERSION 1.0.0 + HOMEPAGE_URL "https://github.com/rigtorp/MPMCQueue" + LANGUAGES CXX +) +# ----------------------------------------------------------------------------- +# Library Target +# ----------------------------------------------------------------------------- add_library(${PROJECT_NAME} INTERFACE) add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) -target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_11) - -target_include_directories(${PROJECT_NAME} INTERFACE - $ - $) - -# Tests and examples -if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) - if (MSVC) - add_compile_options(/permissive- /W4 /wd4172 /wd4324 /wd4530) - else() - add_compile_options(-Wall -Wextra -Wpedantic) - endif() +target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_20) +target_sources(${PROJECT_NAME} INTERFACE + FILE_SET HEADERS + BASE_DIRS include + FILES include/rigtorp/MPMCQueue.h +) - find_package(Threads REQUIRED) +# ----------------------------------------------------------------------------- +# Tests & Examples (Top-Level Only) +# ----------------------------------------------------------------------------- +include(CTest) - add_executable(MPMCQueueExample src/MPMCQueueExample.cpp) - target_link_libraries(MPMCQueueExample MPMCQueue Threads::Threads) +if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING) + find_package(Threads REQUIRED) - add_executable(MPMCQueueTest src/MPMCQueueTest.cpp) - target_link_libraries(MPMCQueueTest MPMCQueue Threads::Threads) + function(mpmcqueue_executable target source) + add_executable(${target} ${source}) + target_link_libraries(${target} PRIVATE + ${PROJECT_NAME}::${PROJECT_NAME} + Threads::Threads + ) + set_target_properties(${target} PROPERTIES + CXX_EXTENSIONS OFF + COMPILE_WARNING_AS_ERROR ON + ) + target_compile_options(${target} PRIVATE + "$<$:/permissive-;/W4;/wd4324;/w14242>" + "$<$:-Wall;-Wextra;-Wpedantic;-Wshadow;-Wconversion;-Wsign-conversion;-Wold-style-cast;-Wundef;-Wcast-align;-Wzero-as-null-pointer-constant;-Wformat=2>" + "$<$,$,12>>:-Wno-interference-size>" + ) + endfunction() - enable_testing() - add_test(MPMCQueueTest MPMCQueueTest) + mpmcqueue_executable(MPMCQueueExample src/MPMCQueueExample.cpp) + mpmcqueue_executable(MPMCQueueTest src/MPMCQueueTest.cpp) + add_test(NAME MPMCQueueTest COMMAND MPMCQueueTest) endif() -# Install -include(GNUInstallDirs) -include(CMakePackageConfigHelpers) - -write_basic_package_version_file( - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" - COMPATIBILITY SameMajorVersion -) +# ----------------------------------------------------------------------------- +# Installation & Package Export +# ----------------------------------------------------------------------------- +if(PROJECT_IS_TOP_LEVEL) + include(GNUInstallDirs) + include(CMakePackageConfigHelpers) -export( + export( TARGETS ${PROJECT_NAME} NAMESPACE ${PROJECT_NAME}:: FILE "${PROJECT_NAME}Config.cmake" -) + ) -if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) - install( - DIRECTORY "include/" - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} - ) + write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + COMPATIBILITY SameMajorVersion + ARCH_INDEPENDENT + ) - install( - TARGETS ${PROJECT_NAME} - EXPORT "${PROJECT_NAME}Config" - INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} - ) + install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Config FILE_SET HEADERS) - install( - EXPORT "${PROJECT_NAME}Config" - NAMESPACE ${PROJECT_NAME}:: - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" - ) + install( + EXPORT ${PROJECT_NAME}Config + NAMESPACE ${PROJECT_NAME}:: + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) - install( - FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" - ) -endif() \ No newline at end of file + install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) +endif() diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..6f08a65 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,72 @@ +{ + "version": 3, + "cmakeMinimumRequired": { + "major": 3, + "minor": 24, + "patch": 0 + }, + "configurePresets": [ + { + "name": "base", + "hidden": true, + "binaryDir": "${sourceDir}/build/${presetName}", + "cacheVariables": { + "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", + "BUILD_TESTING": "ON" + } + }, + { + "name": "debug", + "displayName": "Debug", + "inherits": "base", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "release", + "displayName": "Release", + "inherits": "base", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + }, + { + "name": "asan", + "displayName": "Debug (ASan + UBSan)", + "inherits": "debug", + "cacheVariables": { + "CMAKE_CXX_FLAGS": "-fsanitize=address,undefined -fno-omit-frame-pointer" + } + }, + { + "name": "tsan", + "displayName": "Debug (TSan)", + "inherits": "debug", + "cacheVariables": { + "CMAKE_CXX_FLAGS": "-fsanitize=thread -fno-omit-frame-pointer" + } + } + ], + "buildPresets": [ + { + "name": "base", + "hidden": true + }, + { "name": "debug", "inherits": "base", "configurePreset": "debug" }, + { "name": "release", "inherits": "base", "configurePreset": "release" }, + { "name": "asan", "inherits": "base", "configurePreset": "asan" }, + { "name": "tsan", "inherits": "base", "configurePreset": "tsan" } + ], + "testPresets": [ + { + "name": "base", + "hidden": true, + "output": { "outputOnFailure": true } + }, + { "name": "debug", "inherits": "base", "configurePreset": "debug" }, + { "name": "release", "inherits": "base", "configurePreset": "release" }, + { "name": "asan", "inherits": "base", "configurePreset": "asan" }, + { "name": "tsan", "inherits": "base", "configurePreset": "tsan" } + ] +} diff --git a/README.md b/README.md index 0949ef0..b17d329 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # MPMCQueue.h -![C/C++ CI](https://github.com/rigtorp/MPMCQueue/workflows/C/C++%20CI/badge.svg) +![C/C++ CI](https://github.com/rigtorp/MPMCQueue/actions/workflows/ci.yml/badge.svg) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/rigtorp/MPMCQueue/master/LICENSE) -A bounded multi-producer multi-consumer concurrent queue written in C++11. +A bounded multi-producer multi-consumer concurrent queue for C++20. Build with CMake 3.24 or newer. It's battle hardened and used daily in production: - In the [Frostbite game engine](https://www.ea.com/frostbite) developed by diff --git a/include/rigtorp/MPMCQueue.h b/include/rigtorp/MPMCQueue.h index a4ed6fc..5bc5ab2 100644 --- a/include/rigtorp/MPMCQueue.h +++ b/include/rigtorp/MPMCQueue.h @@ -43,15 +43,15 @@ SOFTWARE. namespace rigtorp { namespace mpmc { #if defined(__cpp_lib_hardware_interference_size) && !defined(__APPLE__) -#ifdef __GNUC__ +#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 12 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Winterference-size" -#endif // #ifdef __GNUC__ +#endif static constexpr size_t hardwareInterferenceSize = std::hardware_destructive_interference_size; -#ifdef __GNUC__ +#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 12 #pragma GCC diagnostic pop -#endif // #ifdef __GNUC__ +#endif #else static constexpr size_t hardwareInterferenceSize = 64; #endif diff --git a/src/MPMCQueueTest.cpp b/src/MPMCQueueTest.cpp index 690f344..a226c38 100644 --- a/src/MPMCQueueTest.cpp +++ b/src/MPMCQueueTest.cpp @@ -23,8 +23,8 @@ SOFTWARE. #undef NDEBUG #include -#include -#include +#include +#include #include #include #include @@ -216,28 +216,28 @@ int main(int argc, char *argv[]) { // Fuzz test { - const uint64_t numOps = 1000; - const uint64_t numThreads = 10; - MPMCQueue q(numThreads); + const std::size_t numOps = 1000; + const std::size_t numThreads = 10; + MPMCQueue q(numThreads); std::atomic flag(false); std::vector threads; - std::atomic sum(0); - for (uint64_t i = 0; i < numThreads; ++i) { + std::atomic sum(0); + for (std::size_t i = 0; i < numThreads; ++i) { threads.push_back(std::thread([&, i] { while (!flag) ; for (auto j = i; j < numOps; j += numThreads) { - q.push(j); + q.push(static_cast(j)); } })); } - for (uint64_t i = 0; i < numThreads; ++i) { + for (std::size_t i = 0; i < numThreads; ++i) { threads.push_back(std::thread([&, i] { while (!flag) ; - uint64_t threadSum = 0; + std::uint64_t threadSum = 0; for (auto j = i; j < numOps; j += numThreads) { - uint64_t v; + std::uint64_t v; q.pop(v); threadSum += v; }