Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 0 additions & 58 deletions .github/workflows/ccpp.yml

This file was deleted.

16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ cmake-build-*
.vscode/
.idea/
.project
*~
*~
CMakeUserPresets.json
116 changes: 64 additions & 52 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

# 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
"$<$<CXX_COMPILER_ID:MSVC>:/permissive-;/W4;/wd4324;/w14242>"
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall;-Wextra;-Wpedantic;-Wshadow;-Wconversion;-Wsign-conversion;-Wold-style-cast;-Wundef;-Wcast-align;-Wzero-as-null-pointer-constant;-Wformat=2>"
"$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,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()
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
)
endif()
72 changes: 72 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -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" }
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions include/rigtorp/MPMCQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions src/MPMCQueueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ SOFTWARE.
#undef NDEBUG

#include <cassert>
#include <chrono>
#include <iostream>
#include <cstddef>
#include <cstdint>
#include <rigtorp/MPMCQueue.h>
#include <set>
#include <thread>
Expand Down Expand Up @@ -216,28 +216,28 @@ int main(int argc, char *argv[]) {

// Fuzz test
{
const uint64_t numOps = 1000;
const uint64_t numThreads = 10;
MPMCQueue<uint64_t> q(numThreads);
const std::size_t numOps = 1000;
const std::size_t numThreads = 10;
MPMCQueue<std::uint64_t> q(numThreads);
std::atomic<bool> flag(false);
std::vector<std::thread> threads;
std::atomic<uint64_t> sum(0);
for (uint64_t i = 0; i < numThreads; ++i) {
std::atomic<std::uint64_t> 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<std::uint64_t>(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;
}
Expand Down
Loading