Skip to content

Commit

Permalink
GHI #35 Add std implementation (#36)
Browse files Browse the repository at this point in the history
- add macro wrappers for direct and cmpxchg implementations
- implement all operations using c11 std atomics
  • Loading branch information
doodspav authored Nov 13, 2024
1 parent fb23f02 commit 324dbc8
Show file tree
Hide file tree
Showing 34 changed files with 3,731 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:

jobs:
test-native:
if: false
strategy:
matrix:
# verbose labels make things easier to read in GitHub Actions
Expand Down Expand Up @@ -55,6 +56,7 @@ jobs:
architecture: ${{ matrix.architecture }}

test-qemu:
if: false
strategy:
matrix:
# architecture gets converted to triple
Expand Down Expand Up @@ -133,6 +135,7 @@ jobs:
skip_llvm: ${{ matrix.skip_llvm == true }}

publish-results:
if: false
runs-on: ubuntu-latest
needs:
- test-native
Expand Down
8 changes: 4 additions & 4 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"name": "_patomic-ci-flags-ansi-gnu",
"hidden": true,
"cacheVariables": {
"CMAKE_C_FLAGS": "-Wall -Wextra -Werror -Wpedantic -Wno-unused-function -Wno-atomic-alignment",
"CMAKE_C_FLAGS": "-Wall -Wextra -Werror -Wpedantic -Wno-unused-function -Wno-atomic-alignment -Wno-unused-local-typedef",
"CMAKE_C_STANDARD": "90"
}
},
Expand Down Expand Up @@ -94,23 +94,23 @@
"name": "_patomic-ci-flags-warning-clang",
"hidden": true,
"cacheVariables": {
"CMAKE_C_FLAGS_INIT": "-Weverything -Werror -Wpedantic -Wno-c++98-compat -Wno-covered-switch-default -Wno-padded -Wno-unused-function -Wno-atomic-alignment -Wno-poison-system-directories -Wno-reserved-identifier -Wno-documentation-unknown-command",
"CMAKE_C_FLAGS_INIT": "-Weverything -Werror -Wpedantic -Wno-c++98-compat -Wno-covered-switch-default -Wno-padded -Wno-unused-function -Wno-atomic-alignment -Wno-poison-system-directories -Wno-reserved-identifier -Wno-documentation-unknown-command -Wno-unused-local-typedef",
"CMAKE_CXX_FLAGS_INIT": "-Wall -Wextra -Werror -Wpedantic -Wno-c++17-attribute-extensions"
}
},
{
"name": "_patomic-ci-flags-warning-gcc",
"hidden": true,
"cacheVariables": {
"CMAKE_C_FLAGS_INIT": "-Wall -Wextra -Werror -Wpedantic -Wshadow -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference -Wdouble-promotion -Wstrict-prototypes -Wmisleading-indentation -Wduplicated-branches -Wlogical-op -Wdeclaration-after-statement -Wno-unused-function",
"CMAKE_C_FLAGS_INIT": "-Wall -Wextra -Werror -Wpedantic -Wshadow -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference -Wdouble-promotion -Wstrict-prototypes -Wmisleading-indentation -Wduplicated-branches -Wlogical-op -Wdeclaration-after-statement -Wno-unused-function -Wno-unused-local-typedef",
"CMAKE_CXX_FLAGS_INIT": "-Wall -Wextra -Werror -Wpedantic"
}
},
{
"name": "_patomic-ci-flags-warning-msvc",
"hidden": true,
"cacheVariables": {
"CMAKE_C_FLAGS_INIT": "/permissive- /volatile:iso /Wall /WX /wd4464 /wd4132 /wd4820 /wd4127 /wd5045 /wd4710 /wd4711 /wd4668",
"CMAKE_C_FLAGS_INIT": "/permissive- /volatile:iso /Wall /WX /wd4464 /wd4132 /wd4820 /wd4127 /wd5045 /wd4710 /wd4711 /wd4668 /wd4146",
"CMAKE_CXX_FLAGS_INIT": "/permissive- /volatile:iso /W4 /WX"
}
},
Expand Down
92 changes: 92 additions & 0 deletions cmake/check/HasFunction.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
# | COMPILER_HAS_BUILTIN_UNREACHABLE | '__builtin_unreachable(void)' is available as a function |
# | COMPILER_HAS_WCHAR_FWIDE | '<wchar.h>' header is available and makes 'fwide(FILE*, int)' available as a function |
# | COMPILER_HAS_WCHAR_FWPRINTF | '<wchar.h>' header is available and makes 'fwprintf(FILE*, const wchar_t*, ...)' available as a function |
# | COMPILER_HAS_C23_ALIGNOF | 'alignof(T)' is available as a function |
# | COMPILER_HAS_C23_ALIGNOF_EXTN | '__extension__ alignof(T)' is available as a function |
# | COMPILER_HAS_C11_ALIGNOF | '_Alignof(T)' is available as a function |
# | COMPILER_HAS_C11_ALIGNOF_EXTN | '__extension__ _Alignof(T)' is available as a function |
# | COMPILER_HAS_MS_ALIGNOF | '__alignof(T)' is available as a function |
# | COMPILER_HAS_MS_ALIGNOF_EXTN | '__extension__ __alignof(T)' is available as a function |
# | COMPILER_HAS_GNU_ALIGNOF | '__alignof__(T)' is available as a function |
# | COMPILER_HAS_GNU_ALIGNOF_EXTN | '__extension__ __alignof__(T)' is available as a function |
# -----------------------------------------------------------------------------------------------------------------------------------------------


Expand Down Expand Up @@ -55,3 +63,87 @@ check_c_source_compiles_or_zero(
WILL_FAIL_IF_ANY_NOT
${COMPILER_HAS_WCHAR_H}
)

# 'alignof(T)' is available as a function
check_c_source_compiles_or_zero(
SOURCE
"int main(void) { return (int) alignof(int); }"
OUTPUT_VARIABLE
COMPILER_HAS_C23_ALIGNOF
)

# '__extension__ alignof(T)' is available as a function
check_c_source_compiles_or_zero(
SOURCE
"int main(void) { return (int) __extension__ alignof(int); }"
OUTPUT_VARIABLE
COMPILER_HAS_C23_ALIGNOF_EXTN
WILL_SUCCEED_IF_ALL
${COMPILER_HAS_C23_ALIGNOF}
${COMPILER_HAS_EXTN}
WILL_FAIL_IF_ANY_NOT
${COMPILER_HAS_EXTN}
)

# '_Alignof(T)' is available as a function
check_c_source_compiles_or_zero(
SOURCE
"int main(void) { return (int) _Alignof(int); }"
OUTPUT_VARIABLE
COMPILER_HAS_C11_ALIGNOF
)

# '__extension__ _Alignof(T)' is available as a function
check_c_source_compiles_or_zero(
SOURCE
"int main(void) { return (int) __extension__ _Alignof(int); }"
OUTPUT_VARIABLE
COMPILER_HAS_C11_ALIGNOF_EXTN
WILL_SUCCEED_IF_ALL
${COMPILER_HAS_C11_ALIGNOF}
${COMPILER_HAS_EXTN}
WILL_FAIL_IF_ANY_NOT
${COMPILER_HAS_EXTN}
)

# '__alignof(T)' is available as a function
check_c_source_compiles_or_zero(
SOURCE
"int main(void) { return (int) __alignof(int); }"
OUTPUT_VARIABLE
COMPILER_HAS_MS_ALIGNOF
)

# '__extension__ __alignof(T)' is available as a function
check_c_source_compiles_or_zero(
SOURCE
"int main(void) { return (int) __extension__ __alignof(int); }"
OUTPUT_VARIABLE
COMPILER_HAS_MS_ALIGNOF_EXTN
WILL_SUCCEED_IF_ALL
${COMPILER_HAS_MS_ALIGNOF}
${COMPILER_HAS_EXTN}
WILL_FAIL_IF_ANY_NOT
${COMPILER_HAS_EXTN}
)

# '__alignof__(T)' is available as a function
check_c_source_compiles_or_zero(
SOURCE
"int main(void) { return (int) __alignof__(int); }"
OUTPUT_VARIABLE
COMPILER_HAS_GNU_ALIGNOF
)

# '__extension__ __alignof__(T)' is available as a function
check_c_source_compiles_or_zero(
SOURCE
"int main(void) { return (int) __extension__ __alignof__(int); }"
OUTPUT_VARIABLE
COMPILER_HAS_GNU_ALIGNOF_EXTN
WILL_SUCCEED_IF_ALL
${COMPILER_HAS_GNU_ALIGNOF}
${COMPILER_HAS_EXTN}
WILL_FAIL_IF_ANY_NOT
${COMPILER_HAS_EXTN}
)
22 changes: 16 additions & 6 deletions cmake/check/HasHeader.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# ---- Has Header ----

# ----------------------------------------------------------
# | Variable | Check |
# |=======================|================================|
# | COMPILER_HAS_STDINT_H | <stdint.h> header is available |
# | COMPILER_HAS_WCHAR_H | <wchar.h> header is available |
# ----------------------------------------------------------
# ----------------------------------------------------------------
# | Variable | Check |
# |==========================|===================================|
# | COMPILER_HAS_STDATOMIC_H | <stdatomic.h> header is available |
# | COMPILER_HAS_STDINT_H | <stdint.h> header is available |
# | COMPILER_HAS_WCHAR_H | <wchar.h> header is available |
# ----------------------------------------------------------------


# <stdatomic.h> header is available
check_c_source_compiles_or_zero(
SOURCE
"#include <stdatomic.h> \n\
int main(void) {}"
OUTPUT_VARIABLE
COMPILER_HAS_STDATOMIC_H
)

# <stdint.h> header is available
check_c_source_compiles_or_zero(
SOURCE
Expand Down
67 changes: 62 additions & 5 deletions cmake/check/HasKeyword.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# ---- Has Keyword ----

# ----------------------------------------------------------------------
# | Variable | Check |
# |========================|===========================================|
# | COMPILER_HAS_EXTN | '__extension__' is available as a keyword |
# ----------------------------------------------------------------------
# -------------------------------------------------------------------------
# | Variable | Check |
# |===========================|===========================================|
# | COMPILER_HAS_EXTN | '__extension__' is available as a keyword |
# | COMPILER_HAS_RESTRICT | 'restrict' is available as a keyword |
# | COMPILER_HAS_MS_RESTRICT | '__restrict' is available as a keyword |
# | COMPILER_HAS_GNU_RESTRICT | '__restrict__' is available as a keyword |
# | COMPILER_HAS_ATOMIC | '_Atomic' is available as a keyword |
# -------------------------------------------------------------------------


# '__extension__' is available as a keyword
Expand All @@ -14,3 +18,56 @@ check_c_source_compiles_or_zero(
OUTPUT_VARIABLE
COMPILER_HAS_EXTN
)

# 'restrict' is available as a keyword
check_c_source_compiles_or_zero(
SOURCE
"static int get(const int *const restrict p) { return *p; } \n\
int main(void) { int x = 0; return get(&x); }"
OUTPUT_VARIABLE
COMPILER_HAS_RESTRICT
)

# '__restrict' is available as a keyword
check_c_source_compiles_or_zero(
SOURCE
"static int get(const int *const __restrict p) { return *p; } \n\
int main(void) { int x = 0; return get(&x); }"
OUTPUT_VARIABLE
COMPILER_HAS_MS_RESTRICT
)

# '__restrict__' is available as a keyword
check_c_source_compiles_or_zero(
SOURCE
"static int get(const int *const __restrict__ p) { return *p; } \n\
int main(void) { int x = 0; return get(&x); }"
OUTPUT_VARIABLE
COMPILER_HAS_GNU_RESTRICT
)

# '_Atomic' is available as a keyword
check_c_source_compiles_or_zero(
SOURCE
"#include <stdatomic.h> \n\
int main(void) { \n\
static const _Atomic(unsigned char) a1; \n\
static const _Atomic(unsigned short) a2; \n\
static const _Atomic(unsigned int) a3; \n\
static const _Atomic(unsigned long) a4; \n\
const volatile _Atomic(unsigned char) *const p1 = &a1; \n\
const volatile _Atomic(unsigned short) *const p2 = &a2; \n\
const volatile _Atomic(unsigned int) *const p3 = &a3; \n\
const volatile _Atomic(unsigned long) *const p4 = &a4; \n\
unsigned long sum = 0; \n\
sum += (unsigned long) atomic_load(p1); \n\
sum += (unsigned long) atomic_load(p2); \n\
sum += (unsigned long) atomic_load(p3); \n\
sum += (unsigned long) atomic_load(p4); \n\
return (int) sum; \n\
}"
OUTPUT_VARIABLE
COMPILER_HAS_ATOMIC
WILL_FAIL_IF_ANY_NOT
${COMPILER_HAS_STDATOMIC_H}
)
Loading

0 comments on commit 324dbc8

Please sign in to comment.