Path: blob/master/tools/testing/selftests/futex/include/atomic.h
29270 views
/* SPDX-License-Identifier: GPL-2.0-or-later */1/******************************************************************************2*3* Copyright © International Business Machines Corp., 20094*5* DESCRIPTION6* GCC atomic builtin wrappers7* http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html8*9* AUTHOR10* Darren Hart <[email protected]>11*12* HISTORY13* 2009-Nov-17: Initial version by Darren Hart <[email protected]>14*15*****************************************************************************/1617#ifndef _ATOMIC_H18#define _ATOMIC_H1920typedef struct {21volatile int val;22} atomic_t;2324#define ATOMIC_INITIALIZER { 0 }2526/**27* atomic_cmpxchg() - Atomic compare and exchange28* @uaddr: The address of the futex to be modified29* @oldval: The expected value of the futex30* @newval: The new value to try and assign the futex31*32* Return the old value of addr->val.33*/34static inline int35atomic_cmpxchg(atomic_t *addr, int oldval, int newval)36{37return __sync_val_compare_and_swap(&addr->val, oldval, newval);38}3940/**41* atomic_inc() - Atomic incrememnt42* @addr: Address of the variable to increment43*44* Return the new value of addr->val.45*/46static inline int47atomic_inc(atomic_t *addr)48{49return __sync_add_and_fetch(&addr->val, 1);50}5152/**53* atomic_dec() - Atomic decrement54* @addr: Address of the variable to decrement55*56* Return the new value of addr-val.57*/58static inline int59atomic_dec(atomic_t *addr)60{61return __sync_sub_and_fetch(&addr->val, 1);62}6364/**65* atomic_set() - Atomic set66* @addr: Address of the variable to set67* @newval: New value for the atomic_t68*69* Return the new value of addr->val.70*/71static inline int72atomic_set(atomic_t *addr, int newval)73{74addr->val = newval;75return newval;76}7778#endif798081