From a1e56851b1d1db0f8c687c769c1f0a37373c34f9 Mon Sep 17 00:00:00 2001 From: doodspav Date: Wed, 6 Mar 2024 16:40:28 +0200 Subject: [PATCH] GHI #32 Add memory_order.h header --- CMakeLists.txt | 1 + include/patomic/types/align.h | 2 +- include/patomic/types/ids.h | 4 +- include/patomic/types/memory_order.h | 114 +++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 include/patomic/types/memory_order.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d306c81f4..7833a1bf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ target_sources( # include/types include/patomic/types/align.h include/patomic/types/ids.h + include/patomic/types/memory_order.h ) # add /src files to target diff --git a/include/patomic/types/align.h b/include/patomic/types/align.h index 28cc0b431..af5370fe0 100644 --- a/include/patomic/types/align.h +++ b/include/patomic/types/align.h @@ -161,7 +161,7 @@ patomic_align_meets_minimum( #ifdef __cplusplus -}; +} /* extern "C" */ #endif #endif /* PATOMIC_ALIGN_H */ diff --git a/include/patomic/types/ids.h b/include/patomic/types/ids.h index 574aeac90..8c2f34974 100644 --- a/include/patomic/types/ids.h +++ b/include/patomic/types/ids.h @@ -70,6 +70,7 @@ typedef unsigned long patomic_id_t; */ typedef enum { + /** @brief The implementation kind is unknown. */ patomic_kind_UNKN = 0x0 @@ -94,6 +95,7 @@ typedef enum { patomic_kind_LIB | patomic_kind_BLTN | patomic_kind_ASM + } patomic_kind_t; @@ -137,7 +139,7 @@ patomic_get_kind( #ifdef __cplusplus -}; +} /* extern "C" */ #endif #endif /* PATOMIC_IDS_H */ diff --git a/include/patomic/types/memory_order.h b/include/patomic/types/memory_order.h new file mode 100644 index 000000000..db87673eb --- /dev/null +++ b/include/patomic/types/memory_order.h @@ -0,0 +1,114 @@ +#ifndef PATOMIC_MEMORY_ORDER_H +#define PATOMIC_MEMORY_ORDER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @addtogroup mem_order + * + * @brief + * Enum constants specifying how memory accesses are to be ordered around an + * atomic operation. + * + * @details + * Each enum label's value and semantics are identical to C++11's + * std::memory_order's values and semantics. + * + * @note + * Consume is only present for compatibility. It is not and will not be + * implemented, and will always be treated as patomic_ACQUIRE. + */ + +typedef enum { + patomic_RELAXED, + patomic_CONSUME, + patomic_ACQUIRE, + patomic_RELEASE, + patomic_ACQ_REL, + patomic_SEQ_CST +} patomic_memory_order_t; + + +/** + * @addtogroup mem_order + * + * @brief + * Checks that order has a value corresponding to a label in + * patomic_memory_order_t. + * + * @returns If the check succeeds returns 1, else 0. + */ + +PATOMIC_EXPORT int +patomic_is_valid_order(int order); + + +/** + * @addtogroup mem_order + * + * @brief + * Checks that order has a value corresponding to a label in + * patomic_memory_order_t, and is valid to use for an atomic store operation. + * + * @returns If the check succeeds returns 1, else 0. + */ + +PATOMIC_EXPORT int +patomic_is_valid_store_order(int order); + + +/** + * @addtogroup mem_order + * + * @brief + * Checks that order has a value corresponding to a label in + * patomic_memory_order_t, and is valid to use for an atomic load operation. + * + * @returns If the check succeeds returns 1, else 0. + */ + +PATOMIC_EXPORT int +patomic_is_valid_load_order(int order); + + +/** + * @addtogroup mem_order + * + * @brief + * Checks that both succ and fail have a value corresponding to a label in + * patomic_memory_order_t, and fail is not stronger than succ or equal to + * patomic_RELEASE or patomic_ACQ_REL. + * + * @returns If the check succeeds returns 1, else 0. + */ + +PATOMIC_EXPORT int +patomic_is_valid_fail_order(int succ, int fail); + + +/** + * @addtogroup mem_order + * + * @brief + * Returns the strictest memory order that would satisfy the checks done by + * patomic_is_valid_fail_order(succ, ). + * + * @warning + * If an invalid memory order is passed to this function, it will be returned + * unmodified. + */ + +PATOMIC_EXPORT int +patomic_cmpxchg_fail_order(int succ); + + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* PATOMIC_MEMORY_ORDER_H */