Skip to content

Commit

Permalink
Add boost.atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
lackhole committed Feb 7, 2025
1 parent 9483f6f commit 491e569
Show file tree
Hide file tree
Showing 416 changed files with 34,250 additions and 52,633 deletions.
33 changes: 31 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(PREVIEW_BASE_DIR "${CMAKE_CURRENT_LIST_DIR}")
set(PREVIEW_INCLUDE_DIR "${PREVIEW_BASE_DIR}/include")
set(PREVIEW_RELATIVE_GENERATED_INCLUDE_DIR "generated/include")
set(PREVIEW_DEFAULT_GENERATED_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${PREVIEW_RELATIVE_GENERATED_INCLUDE_DIR}")
set(PREVIEW_THIRD_PARTY_DIR "${PREVIEW_BASE_DIR}/third_party")

project(preview CXX)

Expand All @@ -31,12 +32,13 @@ if (MSVC)
target_compile_options(preview INTERFACE /permissive-)
endif ()

include(PreviewUtils)
include(RunConfiguration)
if (PREVIEW_TEST)
include(CTest)
enable_testing()

# Test will re-configure preview via ExternalProject to execute different RunConfiguration() for every tests
# Test will re-configure preview via ExternalProject to execute different configuration check for every tests
add_subdirectory(test)
else ()
set(flags)
Expand All @@ -51,6 +53,33 @@ else ()
endif ()
endif ()

RunConfiguration("${PREVIEW_DEFAULT_GENERATED_INCLUDE_DIR}/preview/cmake_config.h" "${flags}")
PREVIEW_RUN_CONFIGURATION_TEST("${flags}")

message("PREVIEW_HAVE_CXX20_ATOMIC: ${PREVIEW_USE_BOOST_ATOMIC}")
PREVIEW_OPTION(PREVIEW_USE_BOOST_ATOMIC "Use Boost.Atomic instead of std::atomic"
(NOT PREVIEW_HAVE_CXX20_ATOMIC)
VISIBLE_IF (PREVIEW_HAVE_CXX20_ATOMIC)
VERIFY (PREVIEW_USE_BOOST_ATOMIC OR PREVIEW_HAVE_CXX20_ATOMIC)
)
PREVIEW_OPTION(PREVIEW_USE_SYSTEM_BOOST "Use system Boost instead of bundled Boost"
OFF
VISIBLE_IF (PREVIEW_USE_BOOST_ATOMIC)
VERIFY (Boost_FOUND)
)
PREVIEW_OPTION(PREVIEW_BOOST_PATH "Path to Boost"
""
VISIBLE_IF (PREVIEW_USE_SYSTEM_BOOST)
VERIFY (Boost_FOUND)
)
message("PREVIEW_USE_BOOST_ATOMIC: ${PREVIEW_USE_BOOST_ATOMIC}")

preview_verify_config()

if (PREVIEW_USE_BOOST_ATOMIC)
include(FindBoost.Atomic)
target_link_libraries(preview INTERFACE Boost::atomic)
endif ()

PREVIEW_CONFIGURE_FILE("${PREVIEW_DEFAULT_GENERATED_INCLUDE_DIR}/preview/cmake_config.h")
target_include_directories(preview INTERFACE "${PREVIEW_DEFAULT_GENERATED_INCLUDE_DIR}")
endif ()
25 changes: 25 additions & 0 deletions cmake/FindBoost.Atomic.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
if (PREVIEW_USE_SYSTEM_BOOST)
if ("${PREVIEW_BOOST_PATH}" STREQUAL "")
find_package(Boost REQUIRED COMPONENTS atomic)
else ()
find_package(Boost REQUIRED COMPONENTS atomic PATHS "${PREVIEW_BOOST_PATH}")
endif ()
else()
include(FetchContent)

FetchContent_Declare(boost_config
GIT_REPOSITORY https://github.com/boostorg/config
GIT_TAG 5734e160e08b8df898c7f747000f27a3aafb7b2b
)
FetchContent_Declare(boost_predef
GIT_REPOSITORY https://github.com/boostorg/predef
GIT_TAG boost-1.86.0
)
FetchContent_Declare(boost_preprocessor
GIT_REPOSITORY https://github.com/boostorg/preprocessor
GIT_TAG boost-1.86.0
)
FetchContent_MakeAvailable(boost_config boost_predef boost_preprocessor)

add_subdirectory("${PREVIEW_THIRD_PARTY_DIR}/boost_1_86_0/atomic" "${CMAKE_CURRENT_BINARY_DIR}/boost")
endif ()
121 changes: 121 additions & 0 deletions cmake/PreviewUtils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Got from: https://github.com/opencv/opencv/blob/ff18c9cc7904e878b10273265196e0238490e692/cmake/OpenCVUtils.cmake#L723

# Provides an option that the user can optionally select.
# Can accept condition to control when option is available for user.
# Usage:
# option(<option_variable>
# "help string describing the option"
# <initial value or boolean expression>
# [VISIBLE_IF <condition>]
# [VERIFY <condition>])
macro(PREVIEW_OPTION variable description value)
set(__value ${value})
set(__condition "")
set(__verification)
set(__varname "__value")
foreach(arg ${ARGN})
if(arg STREQUAL "IF" OR arg STREQUAL "if" OR arg STREQUAL "VISIBLE_IF")
set(__varname "__condition")
elseif(arg STREQUAL "VERIFY")
set(__varname "__verification")
else()
list(APPEND ${__varname} ${arg})
endif()
endforeach()
unset(__varname)
if(__condition STREQUAL "")
set(__condition 2 GREATER 1)
endif()

if(${__condition})
if(__value MATCHES ";")
if(${__value})
option(${variable} "${description}" ON)
else()
option(${variable} "${description}" OFF)
endif()
elseif(DEFINED ${__value})
if(${__value})
option(${variable} "${description}" ON)
else()
option(${variable} "${description}" OFF)
endif()
else()
option(${variable} "${description}" ${__value})
endif()
else()
if(DEFINED ${variable} AND "${${variable}}" # emit warnings about turned ON options only.
AND NOT (PREVIEW_HIDE_WARNING_UNSUPPORTED_OPTION OR "$ENV{PREVIEW_HIDE_WARNING_UNSUPPORTED_OPTION}")
)
message(WARNING "Unexpected option: ${variable} (=${${variable}})\nCondition: IF (${__condition})")
endif()
if(PREVIEW_UNSET_UNSUPPORTED_OPTION)
unset(${variable} CACHE)
endif()
endif()
if(__verification)
set(PREVIEW_VERIFY_${variable} "${__verification}") # variable containing condition to verify
list(APPEND PREVIEW_VERIFICATIONS "${variable}") # list of variable names (WITH_XXX;WITH_YYY;...)
endif()
unset(__condition)
unset(__value)
endmacro()

# Check that each variable stored in PREVIEW_VERIFICATIONS list
# is consistent with actual detection result (stored as condition in PREVIEW_VERIFY_...) variables
function(preview_verify_config)
set(broken_options)
foreach(var ${PREVIEW_VERIFICATIONS})
set(evaluated FALSE)
if(${PREVIEW_VERIFY_${var}})
set(evaluated TRUE)
endif()
# status("Verifying ${var}=${${var}} => '${PREVIEW_VERIFY_${var}}'=${evaluated}")
if (${var} AND NOT evaluated)
list(APPEND broken_options ${var})
message(WARNING
"Option ${var} is enabled but corresponding dependency "
"have not been found: \"${PREVIEW_VERIFY_${var}}\" is FALSE")
elseif(NOT ${var} AND evaluated)
list(APPEND broken_options ${var})
message(WARNING
"Option ${var} is disabled or unset but corresponding dependency "
"have been explicitly turned on: \"${PREVIEW_VERIFY_${var}}\" is TRUE")
endif()
endforeach()
if(broken_options)
string(REPLACE ";" "\n" broken_options "${broken_options}")
message(FATAL_ERROR
"Some dependencies have not been found or have been forced, "
"unset ENABLE_CONFIG_VERIFICATION option to ignore these failures "
"or change following options:\n${broken_options}")
endif()
endfunction()

macro(PREVIEW_CHECK_COMPILES code out)
set(CMAKE_REQUIRED_FLAGS ${ARGN})
check_cxx_source_compiles("${code}" ${out})
if ("${${out}}" STREQUAL "")
set(${out} 0) # Workaround: CMake sometimes sets empty string instead of FALSE
endif ()
message("${out}=${${out}}")
set(${out} ${${out}} PARENT_SCOPE)
endmacro()

function(preview_get_cmake_args out)
get_cmake_property(vars CACHE_VARIABLES)
set(args)
foreach(var ${vars})
get_property(currentHelpString CACHE "${var}" PROPERTY HELPSTRING)
if("${currentHelpString}" MATCHES "No help, variable specified on the command line." OR "${currentHelpString}" STREQUAL "")
get_property(var_type CACHE ${var} PROPERTY TYPE)
if(var_type STREQUAL "UNINITIALIZED")
set(var_type)
else()
set(var_type :${var_type})
endif()
list(APPEND args "-D${var}${var_type}=${${var}}")
endif()
endforeach()
set(${out} "${args}" PARENT_SCOPE)
endfunction()
12 changes: 10 additions & 2 deletions cmake/RunConfiguration.cmake
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
include(CheckCXXSourceCompiles)
include(CheckCXXCompilerFlag)
include(CheckCXXSymbolExists)
include(CheckCXXSourceRuns)

include(config/HaveBuiltinBitCast)
include(config/HaveBuiltinConstexprAddressof)
include(config/HaveContiguousIteratorTag)
include(config/HaveCXX20Atomic)
include(config/HaveStringView)

set(PREVIEW_CONFIG_INPUT_FILE "${PREVIEW_INCLUDE_DIR}/preview/cmake_config.h.in")

function(RunConfiguration out_dir flags)
macro(PREVIEW_RUN_CONFIGURATION_TEST flags)
message("Test flags: ${flags}")

HaveBuiltinBitCast(PREVIEW_HAVE_BUILTIN_BIT_CAST ${flags})
HaveBuiltinConstexprAddressof(PREVIEW_HAVE_BUILTIN_CONSTEXPR_ADDRESSOF ${flags})

HaveContiguousIteratorTag(PREVIEW_HAVE_CONTIGUOUS_ITERATOR_TAG ${flags})
HaveStringView(PREVIEW_HAVE_STRING_VIEW ${flags})

HaveCXX20Atomic(PREVIEW_HAVE_CXX20_ATOMIC ${flags})
endmacro()

macro(PREVIEW_CONFIGURE_FILE out_dir)
configure_file("${PREVIEW_CONFIG_INPUT_FILE}" "${out_dir}")
endfunction()
endmacro()
21 changes: 21 additions & 0 deletions cmake/config/HaveCXX20Atomic.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set(HaveCXX20Atomic_Source "
#include <atomic>
#include <chrono>
#include <thread>
int main() {
std::atomic<int> a{1};
std::thread t1([&a] {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
a.fetch_add(1);
a.notify_one();
a.notify_all();
});
a.wait(1);
return 0;
}")

function(HaveCXX20Atomic out)
PREVIEW_CHECK_COMPILES("${HaveCXX20Atomic_Source}" ${out})
message("${out}: ${${out}}")
endfunction()
15 changes: 13 additions & 2 deletions include/preview/__atomic/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
#ifndef PREVIEW_ATOMIC_ATOMIC_H_
#define PREVIEW_ATOMIC_ATOMIC_H_

#include <atomic>
#include <type_traits>


#include "preview/__atomic/detail/atomic_floating_point.h"
#include "preview/__atomic/detail/atomic_generic.h"
#include "preview/__atomic/detail/atomic_integral.h"
Expand Down Expand Up @@ -39,6 +37,19 @@ class atomic : public detail::atomic_base_t<T> {
using base = detail::atomic_base_t<T>;
public:
using base::base;

inline T operator=(T desired) noexcept {
this->store(desired);
return desired;
}

inline T operator=(T desired) volatile noexcept {
this->store(desired);
return desired;
}

atomic<T>& operator=(const atomic<T>& other) = delete;
atomic<T>& operator=(const atomic<T>& other) volatile = delete;
};

} // namespace preview
Expand Down
68 changes: 0 additions & 68 deletions include/preview/__atomic/detail/atomic_base.h

This file was deleted.

19 changes: 0 additions & 19 deletions include/preview/__atomic/detail/atomic_common.h

This file was deleted.

6 changes: 3 additions & 3 deletions include/preview/__atomic/detail/atomic_floating_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
#ifndef PREVIEW_ATOMIC_DETAIL_ATOMIC_FLOATING_POINT_H_
#define PREVIEW_ATOMIC_DETAIL_ATOMIC_FLOATING_POINT_H_

#include "preview/__atomic/detail/atomic_base.h"
#include "preview/__atomic/detail/cxx20_atomic_base.h"

namespace preview {
namespace detail {

template<typename T>
struct atomic_floating_point : atomic_base<T> {
struct atomic_floating_point : public cxx20_atomic_base<T> {
using value_type = T;
using difference_type = value_type;

using atomic_base<T>::atomic_base;
using cxx20_atomic_base<T>::cxx20_atomic_base;
};

} // namespace detail
Expand Down
Loading

0 comments on commit 491e569

Please sign in to comment.