--- zzzz-none-000/linux-5.15.111/arch/x86/include/asm/atomic.h 2023-05-11 14:00:40.000000000 +0000 +++ puma7-atom-6670-761/linux-5.15.111/arch/x86/include/asm/atomic.h 2024-02-07 10:22:34.000000000 +0000 @@ -263,6 +263,49 @@ } #define arch_atomic_fetch_xor arch_atomic_fetch_xor +/* + * return the incremented value if value < max_value + * if incremented value == max_value so do not write and return -1 + */ +static inline int atomic_inc_with_max_return(atomic_t * v, unsigned int max_value) { + int c, old, add; + c = arch_atomic_read(v); + for (;;) { + add = c + 1; + if (unlikely(add >= max_value)) { + add = -1; + break; + } + old = arch_atomic_cmpxchg((v), c, add); + if (likely(old == c)) { + break; + } + c = old; + } + return add; +} + +/* + * return the incremented value if value < max_value + * if incremented value == max_value so write and return zero + */ +static inline int atomic_inc_with_wrap_return(atomic_t * v, unsigned int max_value) { + int c, old, add; + c = arch_atomic_read(v); + for (;;) { + add = c + 1; + if (unlikely(add >= max_value)) { + add = 0; + } + old = arch_atomic_cmpxchg((v), c, add); + if (likely(old == c)) { + break; + } + c = old; + } + return add; +} + #ifdef CONFIG_X86_32 # include #else