diff options
author | Mitya Selivanov <0x7fffff@guattari.ru> | 2022-08-31 03:22:54 +0400 |
---|---|---|
committer | Mitya Selivanov <0x7fffff@guattari.ru> | 2022-08-31 03:22:54 +0400 |
commit | d98e34df99adc34da0846f20fa5f13b748943873 (patch) | |
tree | 6ea2ed45f3b6b21e4ac26c118bb82e6ce79ae711 /source | |
parent | 4d6b5fc65f22ed704f05e04d41771fb4579ee5a8 (diff) | |
download | kit-d98e34df99adc34da0846f20fa5f13b748943873.zip |
test
Diffstat (limited to 'source')
-rw-r--r-- | source/kit/atomic.h | 56 | ||||
-rw-r--r-- | source/test/unittests/atomic.test.c | 16 |
2 files changed, 41 insertions, 31 deletions
diff --git a/source/kit/atomic.h b/source/kit/atomic.h index 23962a2..012d48d 100644 --- a/source/kit/atomic.h +++ b/source/kit/atomic.h @@ -81,31 +81,33 @@ uint64_t kit_atomic_fetch_add_explicit_64(uint64_t volatile *var, uint64_t value, int memory_order); -# define atomic_store_explicit(var_, value_, memory_order_) \ - do { \ - assert(sizeof *(var_) == 1 || sizeof *(var_) == 2 || \ - sizeof *(var_) == 4 || sizeof *(var_) == 8); \ - if (sizeof *(var_) == 1) \ - kit_atomic_store_explicit_8((uint8_t volatile *) (var_), \ - (uint8_t) (value_), \ - (memory_order_)); \ - if (sizeof *(var_) == 2) \ - kit_atomic_store_explicit_16((uint16_t volatile *) (var_), \ - (uint16_t) (value_), \ - (memory_order_)); \ - if (sizeof *(var_) == 4) \ - kit_atomic_store_explicit_32((uint32_t volatile *) (var_), \ - (uint32_t) (value_), \ - (memory_order_)); \ - if (sizeof *(var_) == 8) \ - kit_atomic_store_explicit_64((uint64_t volatile *) (var_), \ - (uint64_t) (value_), \ - (memory_order_)); \ +# define atomic_store_explicit(var_, value_, memory_order_) \ + do { \ + static_assert(sizeof *(var_) == 1 || sizeof *(var_) == 2 || \ + sizeof *(var_) == 4 || sizeof *(var_) == 8, \ + "Wrong atomic variable type"); \ + if (sizeof *(var_) == 1) \ + kit_atomic_store_explicit_8((uint8_t volatile *) (var_), \ + (uint8_t) (value_), \ + (memory_order_)); \ + if (sizeof *(var_) == 2) \ + kit_atomic_store_explicit_16((uint16_t volatile *) (var_), \ + (uint16_t) (value_), \ + (memory_order_)); \ + if (sizeof *(var_) == 4) \ + kit_atomic_store_explicit_32((uint32_t volatile *) (var_), \ + (uint32_t) (value_), \ + (memory_order_)); \ + if (sizeof *(var_) == 8) \ + kit_atomic_store_explicit_64((uint64_t volatile *) (var_), \ + (uint64_t) (value_), \ + (memory_order_)); \ } while (0) # define atomic_load_explicit(var_, memory_order_) \ - (assert(sizeof *(var_) == 1 || sizeof *(var_) == 2 || \ - sizeof *(var_) == 4 || sizeof *(var_) == 8), \ + (static_assert(sizeof *(var_) == 1 || sizeof *(var_) == 2 || \ + sizeof *(var_) == 4 || sizeof *(var_) == 8, \ + "Wrong atomic variable type"), \ (sizeof *(var_) == 1 \ ? kit_atomic_load_explicit_8((uint8_t volatile *) (var_), \ (memory_order_)) \ @@ -119,8 +121,9 @@ uint64_t kit_atomic_fetch_add_explicit_64(uint64_t volatile *var, (uint64_t volatile *) (var_), (memory_order_)))) # define atomic_exchange_explicit(var_, value_, memory_order_) \ - (assert(sizeof *(var_) == 1 || sizeof *(var_) == 2 || \ - sizeof *(var_) == 4 || sizeof *(var_) == 8), \ + (static_assert(sizeof *(var_) == 1 || sizeof *(var_) == 2 || \ + sizeof *(var_) == 4 || sizeof *(var_) == 8, \ + "Wrong atomic variable type"), \ (sizeof *(var_) == 1 ? kit_atomic_exchange_explicit_8( \ (uint8_t volatile *) (var_), \ (uint8_t) (value_), (memory_order_)) \ @@ -137,8 +140,9 @@ uint64_t kit_atomic_fetch_add_explicit_64(uint64_t volatile *var, (memory_order_)))) # define atomic_fetch_add_explicit(var_, value_, memory_order_) \ - (assert(sizeof *(var_) == 1 || sizeof *(var_) == 2 || \ - sizeof *(var_) == 4 || sizeof *(var_) == 8), \ + (static_assert(sizeof *(var_) == 1 || sizeof *(var_) == 2 || \ + sizeof *(var_) == 4 || sizeof *(var_) == 8, \ + "Wrong atomic variable type"), \ (sizeof *(var_) == 1 ? kit_atomic_fetch_add_explicit_8( \ (uint8_t volatile *) (var_), \ (uint8_t) (value_), (memory_order_)) \ diff --git a/source/test/unittests/atomic.test.c b/source/test/unittests/atomic.test.c index 24cadca..c909cb0 100644 --- a/source/test/unittests/atomic.test.c +++ b/source/test/unittests/atomic.test.c @@ -42,25 +42,30 @@ static int test_8_(void *p) { } TEST("atomic types") { - ATOMIC(int8_t) byte; + ATOMIC(int8_t) b_1; + ATOMIC(int8_t) b_2; ATOMIC(int16_t) i16; ATOMIC(int32_t) i32; ATOMIC(int64_t) i64; - atomic_store_explicit(&byte, 42, memory_order_relaxed); + atomic_store_explicit(&b_1, 42, memory_order_relaxed); + atomic_store_explicit(&b_2, 43, memory_order_relaxed); atomic_store_explicit(&i16, 4242, memory_order_relaxed); atomic_store_explicit(&i32, 42424242, memory_order_relaxed); atomic_store_explicit(&i64, 4242424242424242ll, memory_order_relaxed); - atomic_fetch_add_explicit(&byte, -20, memory_order_relaxed); + atomic_fetch_add_explicit(&b_1, -20, memory_order_relaxed); + atomic_fetch_add_explicit(&b_2, -20, memory_order_relaxed); atomic_fetch_add_explicit(&i16, -2020, memory_order_relaxed); atomic_fetch_add_explicit(&i32, -20202020, memory_order_relaxed); atomic_fetch_add_explicit(&i64, -2020202020202020ll, memory_order_relaxed); - REQUIRE(atomic_exchange_explicit(&byte, 0, memory_order_relaxed) == + REQUIRE(atomic_exchange_explicit(&b_1, 0, memory_order_relaxed) == 22); + REQUIRE(atomic_exchange_explicit(&b_2, 0, memory_order_relaxed) == + 23); REQUIRE(atomic_exchange_explicit(&i16, 0, memory_order_relaxed) == 2222); REQUIRE(atomic_exchange_explicit(&i32, 0, memory_order_relaxed) == @@ -68,7 +73,8 @@ TEST("atomic types") { REQUIRE(atomic_exchange_explicit(&i64, 0, memory_order_relaxed) == 2222222222222222ll); - REQUIRE(atomic_load_explicit(&byte, memory_order_relaxed) == 0); + REQUIRE(atomic_load_explicit(&b_1, memory_order_relaxed) == 0); + REQUIRE(atomic_load_explicit(&b_2, memory_order_relaxed) == 0); REQUIRE(atomic_load_explicit(&i16, memory_order_relaxed) == 0); REQUIRE(atomic_load_explicit(&i32, memory_order_relaxed) == 0); REQUIRE(atomic_load_explicit(&i64, memory_order_relaxed) == 0ll); |