--- zzzz-none-000/linux-4.9.279/arch/x86/include/asm/atomic.h 2021-08-08 06:38:54.000000000 +0000 +++ puma7-atom-6591-750/linux-4.9.279/arch/x86/include/asm/atomic.h 2023-02-08 11:43:42.000000000 +0000 @@ -249,6 +249,49 @@ return c; } +/* + * 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 = atomic_read(v); + for (;;) { + add = c + 1; + if (unlikely(add >= max_value)) { + add = -1; + break; + } + old = 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 = atomic_read(v); + for (;;) { + add = c + 1; + if (unlikely(add >= max_value)) { + add = 0; + } + old = atomic_cmpxchg((v), c, add); + if (likely(old == c)) { + break; + } + c = old; + } + return add; +} + #ifdef CONFIG_X86_32 # include #else