From 30e68e821c4639c1d628a947d393b88b20cf127c Mon Sep 17 00:00:00 2001 From: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Date: Sun, 6 Jul 2025 07:41:46 +0800 Subject: [PATCH 1/2] `atomic.h`: Use explicit logic for 32-bit #else branches These branches are only active for 32-bit Windows and Solaris platforms, codify the fact by changing `#else` to `#elif`'s that explicitly include those targets and `#error`-out otherwise. --- include/ruby/atomic.h | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/include/ruby/atomic.h b/include/ruby/atomic.h index 4cf891b345c3b9..8ab563de92142c 100644 --- a/include/ruby/atomic.h +++ b/include/ruby/atomic.h @@ -436,12 +436,14 @@ rbimpl_atomic_size_fetch_add(volatile size_t *ptr, size_t val) RBIMPL_ASSERT_OR_ASSUME(val <= LONG_MAX); atomic_add_long(ptr, val); -#else +#elif defined(__sun) && defined(HAVE_ATOMIC_H) RBIMPL_STATIC_ASSERT(size_of_rb_atomic_t, sizeof *ptr == sizeof(rb_atomic_t)); volatile rb_atomic_t *const tmp = RBIMPL_CAST((volatile rb_atomic_t *)ptr); rbimpl_atomic_fetch_add(tmp, val); +#else +# error Unsupported platform. #endif } @@ -505,12 +507,14 @@ rbimpl_atomic_size_add(volatile size_t *ptr, size_t val) RBIMPL_ASSERT_OR_ASSUME(val <= LONG_MAX); atomic_add_long(ptr, val); -#else +#elif defined(_WIN32) || (defined(__sun) && defined(HAVE_ATOMIC_H)) RBIMPL_STATIC_ASSERT(size_of_rb_atomic_t, sizeof *ptr == sizeof(rb_atomic_t)); volatile rb_atomic_t *const tmp = RBIMPL_CAST((volatile rb_atomic_t *)ptr); rbimpl_atomic_add(tmp, val); +#else +# error Unsupported platform. #endif } @@ -532,8 +536,7 @@ rbimpl_atomic_inc(volatile rb_atomic_t *ptr) atomic_inc_uint(ptr); #else - rbimpl_atomic_add(ptr, 1); - +# error Unsupported platform. #endif } @@ -554,11 +557,13 @@ rbimpl_atomic_size_inc(volatile size_t *ptr) #elif defined(__sun) && defined(HAVE_ATOMIC_H) && (defined(_LP64) || defined(_I32LPx)) atomic_inc_ulong(ptr); -#else +#elif defined(_WIN32) || (defined(__sun) && defined(HAVE_ATOMIC_H)) RBIMPL_STATIC_ASSERT(size_of_size_t, sizeof *ptr == sizeof(rb_atomic_t)); rbimpl_atomic_size_add(ptr, 1); +#else +# error Unsupported platform. #endif } @@ -641,12 +646,14 @@ rbimpl_atomic_size_sub(volatile size_t *ptr, size_t val) RBIMPL_ASSERT_OR_ASSUME(val <= LONG_MAX); atomic_add_long(ptr, neg * val); -#else +#elif defined(_WIN32) || (defined(__sun) && defined(HAVE_ATOMIC_H)) RBIMPL_STATIC_ASSERT(size_of_rb_atomic_t, sizeof *ptr == sizeof(rb_atomic_t)); volatile rb_atomic_t *const tmp = RBIMPL_CAST((volatile rb_atomic_t *)ptr); rbimpl_atomic_sub(tmp, val); +#else +# error Unsupported platform. #endif } @@ -668,8 +675,7 @@ rbimpl_atomic_dec(volatile rb_atomic_t *ptr) atomic_dec_uint(ptr); #else - rbimpl_atomic_sub(ptr, 1); - +# error Unsupported platform. #endif } @@ -690,11 +696,13 @@ rbimpl_atomic_size_dec(volatile size_t *ptr) #elif defined(__sun) && defined(HAVE_ATOMIC_H) && (defined(_LP64) || defined(_I32LPx)) atomic_dec_ulong(ptr); -#else +#elif defined(_WIN32) || (defined(__sun) && defined(HAVE_ATOMIC_H)) RBIMPL_STATIC_ASSERT(size_of_size_t, sizeof *ptr == sizeof(rb_atomic_t)); rbimpl_atomic_size_sub(ptr, 1); +#else +# error Unsupported platform. #endif } @@ -790,13 +798,15 @@ rbimpl_atomic_size_exchange(volatile size_t *ptr, size_t val) #elif defined(__sun) && defined(HAVE_ATOMIC_H) && (defined(_LP64) || defined(_I32LPx)) return atomic_swap_ulong(ptr, val); -#else +#elif defined(_WIN32) || (defined(__sun) && defined(HAVE_ATOMIC_H)) RBIMPL_STATIC_ASSERT(size_of_size_t, sizeof *ptr == sizeof(rb_atomic_t)); volatile rb_atomic_t *const tmp = RBIMPL_CAST((volatile rb_atomic_t *)ptr); const rb_atomic_t ret = rbimpl_atomic_exchange(tmp, val); return RBIMPL_CAST((size_t)ret); +#else +# error Unsupported platform. #endif } @@ -983,12 +993,14 @@ rbimpl_atomic_size_cas(volatile size_t *ptr, size_t oldval, size_t newval) #elif defined(__sun) && defined(HAVE_ATOMIC_H) && (defined(_LP64) || defined(_I32LPx)) return atomic_cas_ulong(ptr, oldval, newval); -#else +#elif defined(_WIN32) || (defined(__sun) && defined(HAVE_ATOMIC_H)) RBIMPL_STATIC_ASSERT(size_of_size_t, sizeof *ptr == sizeof(rb_atomic_t)); volatile rb_atomic_t *tmp = RBIMPL_CAST((volatile rb_atomic_t *)ptr); return rbimpl_atomic_cas(tmp, oldval, newval); +#else +# error Unsupported platform. #endif } From 5af24efea8f0daede31ffbfcd28dc756af25b093 Mon Sep 17 00:00:00 2001 From: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Date: Sun, 6 Jul 2025 08:03:36 +0800 Subject: [PATCH 2/2] `atomic.h`: Add C11 implementation The implementation is only active if `HAVE_STDATOMIC_H` is defined, and only after the compiler fails to match all currently supported systems. --- include/ruby/atomic.h | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/include/ruby/atomic.h b/include/ruby/atomic.h index 8ab563de92142c..b778276f62d887 100644 --- a/include/ruby/atomic.h +++ b/include/ruby/atomic.h @@ -77,6 +77,9 @@ typedef unsigned int rb_atomic_t; typedef LONG rb_atomic_t; #elif defined(__sun) && defined(HAVE_ATOMIC_H) typedef unsigned int rb_atomic_t; +#elif defined(HAVE_STDATOMIC_H) +# include +typedef unsigned int rb_atomic_t; #else # error No atomic operation found #endif @@ -408,6 +411,9 @@ rbimpl_atomic_fetch_add(volatile rb_atomic_t *ptr, rb_atomic_t val) RBIMPL_ASSERT_OR_ASSUME(val <= INT_MAX); return atomic_add_int_nv(ptr, val) - val; +#elif defined(HAVE_STDATOMIC_H) + return atomic_fetch_add((_Atomic volatile rb_atomic_t *)ptr, val); + #else # error Unsupported platform. #endif @@ -442,6 +448,9 @@ rbimpl_atomic_size_fetch_add(volatile size_t *ptr, size_t val) volatile rb_atomic_t *const tmp = RBIMPL_CAST((volatile rb_atomic_t *)ptr); rbimpl_atomic_fetch_add(tmp, val); +#elif defined(HAVE_STDATOMIC_H) + return atomic_fetch_add((_Atomic volatile size_t *)ptr, val); + #else # error Unsupported platform. #endif @@ -479,6 +488,9 @@ rbimpl_atomic_add(volatile rb_atomic_t *ptr, rb_atomic_t val) RBIMPL_ASSERT_OR_ASSUME(val <= INT_MAX); atomic_add_int(ptr, val); +#elif defined(HAVE_STDATOMIC_H) + *(_Atomic volatile rb_atomic_t *)ptr += val; + #else # error Unsupported platform. #endif @@ -513,6 +525,9 @@ rbimpl_atomic_size_add(volatile size_t *ptr, size_t val) volatile rb_atomic_t *const tmp = RBIMPL_CAST((volatile rb_atomic_t *)ptr); rbimpl_atomic_add(tmp, val); +#elif defined(HAVE_STDATOMIC_H) + *(_Atomic volatile size_t *)ptr += val; + #else # error Unsupported platform. #endif @@ -535,6 +550,9 @@ rbimpl_atomic_inc(volatile rb_atomic_t *ptr) #elif defined(__sun) && defined(HAVE_ATOMIC_H) atomic_inc_uint(ptr); +#elif defined(HAVE_STDATOMIC_H) + rbimpl_atomic_add(ptr, 1); + #else # error Unsupported platform. #endif @@ -562,6 +580,9 @@ rbimpl_atomic_size_inc(volatile size_t *ptr) rbimpl_atomic_size_add(ptr, 1); +#elif defined(HAVE_STDATOMIC_H) + rbimpl_atomic_size_add(ptr, 1); + #else # error Unsupported platform. #endif @@ -591,6 +612,9 @@ rbimpl_atomic_fetch_sub(volatile rb_atomic_t *ptr, rb_atomic_t val) RBIMPL_ASSERT_OR_ASSUME(val <= INT_MAX); return atomic_add_int_nv(ptr, neg * val) + val; +#elif defined(HAVE_STDATOMIC_H) + return atomic_fetch_sub((_Atomic volatile rb_atomic_t *)ptr, val); + #else # error Unsupported platform. #endif @@ -618,6 +642,9 @@ rbimpl_atomic_sub(volatile rb_atomic_t *ptr, rb_atomic_t val) RBIMPL_ASSERT_OR_ASSUME(val <= INT_MAX); atomic_add_int(ptr, neg * val); +#elif defined(HAVE_STDATOMIC_H) + *(_Atomic volatile rb_atomic_t *)ptr -= val; + #else # error Unsupported platform. #endif @@ -652,6 +679,9 @@ rbimpl_atomic_size_sub(volatile size_t *ptr, size_t val) volatile rb_atomic_t *const tmp = RBIMPL_CAST((volatile rb_atomic_t *)ptr); rbimpl_atomic_sub(tmp, val); +#elif defined(HAVE_STDATOMIC_H) + *(_Atomic volatile size_t *)ptr -= val; + #else # error Unsupported platform. #endif @@ -674,6 +704,9 @@ rbimpl_atomic_dec(volatile rb_atomic_t *ptr) #elif defined(__sun) && defined(HAVE_ATOMIC_H) atomic_dec_uint(ptr); +#elif defined(HAVE_STDATOMIC_H) + rbimpl_atomic_sub(ptr, 1); + #else # error Unsupported platform. #endif @@ -701,6 +734,9 @@ rbimpl_atomic_size_dec(volatile size_t *ptr) rbimpl_atomic_size_sub(ptr, 1); +#elif defined(HAVE_STDATOMIC_H) + rbimpl_atomic_size_sub(ptr, 1); + #else # error Unsupported platform. #endif @@ -739,6 +775,9 @@ rbimpl_atomic_or(volatile rb_atomic_t *ptr, rb_atomic_t val) #elif defined(__sun) && defined(HAVE_ATOMIC_H) atomic_or_uint(ptr, val); +#elif !defined(_WIN32) && defined(HAVE_STDATOMIC_H) + *(_Atomic volatile rb_atomic_t *)ptr |= val; + #else # error Unsupported platform. #endif @@ -773,6 +812,9 @@ rbimpl_atomic_exchange(volatile rb_atomic_t *ptr, rb_atomic_t val) #elif defined(__sun) && defined(HAVE_ATOMIC_H) return atomic_swap_uint(ptr, val); +#elif defined(HAVE_STDATOMIC_H) + return atomic_exchange((_Atomic volatile rb_atomic_t *)ptr, val); + #else # error Unsupported platform. #endif @@ -805,6 +847,9 @@ rbimpl_atomic_size_exchange(volatile size_t *ptr, size_t val) const rb_atomic_t ret = rbimpl_atomic_exchange(tmp, val); return RBIMPL_CAST((size_t)ret); +#elif defined(HAVE_STDATOMIC_H) + return atomic_exchange((_Atomic volatile size_t *)ptr, val); + #else # error Unsupported platform. #endif @@ -957,6 +1002,11 @@ rbimpl_atomic_cas(volatile rb_atomic_t *ptr, rb_atomic_t oldval, rb_atomic_t new #elif defined(__sun) && defined(HAVE_ATOMIC_H) return atomic_cas_uint(ptr, oldval, newval); +#elif defined(HAVE_STDATOMIC_H) + atomic_compare_exchange_strong( + (_Atomic volatile rb_atomic_t *)ptr, &oldval, newval); + return oldval; + #else # error Unsupported platform. #endif @@ -999,6 +1049,11 @@ rbimpl_atomic_size_cas(volatile size_t *ptr, size_t oldval, size_t newval) volatile rb_atomic_t *tmp = RBIMPL_CAST((volatile rb_atomic_t *)ptr); return rbimpl_atomic_cas(tmp, oldval, newval); +#elif defined(HAVE_STDATOMIC_H) + atomic_compare_exchange_strong( + (_Atomic volatile size_t *)ptr, &oldval, newval); + return oldval; + #else # error Unsupported platform. #endif pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy