Skip to content

Commit

Permalink
Update C11 atomic support to rely on just the compiler version
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhop committed Aug 1, 2024
1 parent e4949cc commit 8254599
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
16 changes: 2 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,7 @@ if(CMAKE_VERSION VERSION_GREATER "3.1.0")
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (NOT DEFINED CMAKE_C_STANDARD)
try_compile(
RESULT
${PROJECT_BINARY_DIR}
SOURCES "${CMAKE_CURRENT_LIST_DIR}/tests/compiler_features_tests/simple_main.c"
CMAKE_FLAGS "-DCMAKE_C_FLAGS=\"-std=c11\"")
if(RESULT)
set(CMAKE_C_STANDARD 11)
else()
set(CMAKE_C_STANDARD 99)
endif()
set(CMAKE_C_STANDARD 99)
endif ()
set(CMAKE_C_STANDARD_REQUIRED ON)
endif ()
Expand Down Expand Up @@ -364,11 +355,8 @@ if(GCC OR CLANG)
# Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
# primarily on our normal Clang one.
if (NOT CMAKE_C_STANDARD)
message(STATUS "Adding CMAKE_C_FLAGS -std=c99")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
else()
message(STATUS "Setting CMAKE_C_STANDARD=${CMAKE_C_STANDARD}")
endif()
endif ()

# TODO(CryptoAlg-759): enable '-Wpedantic' if awslc has to follow c99 spec.
if(CLANG OR (GCC AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.1.3"))
Expand Down
9 changes: 6 additions & 3 deletions crypto/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,12 +564,15 @@ OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void));

// Reference counting.

// Automatically enable C11 atomics if implemented.
// Automatically enable C11 atomics if the compiler supports it (Clang or GCC > 4.8)
#if !defined(OPENSSL_C11_ATOMIC) && defined(OPENSSL_THREADS) && \
!defined(__STDC_NO_ATOMICS__) && defined(__STDC_VERSION__) && \
__STDC_VERSION__ >= 201112L
!defined(OPENSSL_WINDOWS) && !defined(__STDC_NO_ATOMICS__) && \
!(defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__))
#include <stdatomic.h>
#if ATOMIC_LONG_LOCK_FREE > 0
#define OPENSSL_C11_ATOMIC
#endif
#endif

// Older MSVC does not support C11 atomics, so we fallback to the Windows APIs.
// This can be removed once we can rely on
Expand Down
14 changes: 7 additions & 7 deletions crypto/refcount_c11.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
#include <openssl/type_check.h>


// See comment above the typedef of CRYPTO_refcount_t about these tests.
static_assert(alignof(CRYPTO_refcount_t) == alignof(_Atomic CRYPTO_refcount_t),
"_Atomic alters the needed alignment of a reference count");
static_assert(sizeof(CRYPTO_refcount_t) == sizeof(_Atomic CRYPTO_refcount_t),
"_Atomic alters the size of a reference count");
//// See comment above the typedef of CRYPTO_refcount_t about these tests.
OPENSSL_STATIC_ASSERT(alignof(CRYPTO_refcount_t) == alignof(_Atomic CRYPTO_refcount_t),
_Atomic_alters_the_needed_alignment_of_a_reference_count);
OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_refcount_t) == sizeof(_Atomic CRYPTO_refcount_t),
_Atomic_alters_the_size_of_a_reference_count);

static_assert((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX,
"CRYPTO_REFCOUNT_MAX is incorrect");
OPENSSL_STATIC_ASSERT((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX,
CRYPTO_REFCOUNT_MAX_is_incorrect);

void CRYPTO_refcount_inc(CRYPTO_refcount_t *in_count) {
_Atomic CRYPTO_refcount_t *count = (_Atomic CRYPTO_refcount_t *) in_count;
Expand Down

0 comments on commit 8254599

Please sign in to comment.