1 /****************************************************************************** 2 * 3 * Copyright © International Business Machines Corp., 2009 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * DESCRIPTION 11 * GCC atomic builtin wrappers 12 * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html 13 * 14 * AUTHOR 15 * Darren Hart <dvhart@linux.intel.com> 16 * 17 * HISTORY 18 * 2009-Nov-17: Initial version by Darren Hart <dvhart@linux.intel.com> 19 * 20 *****************************************************************************/ 21 22 #ifndef _ATOMIC_H 23 #define _ATOMIC_H 24 25 typedef struct { 26 volatile int val; 27 } atomic_t; 28 29 #define ATOMIC_INITIALIZER { 0 } 30 31 /** 32 * atomic_cmpxchg() - Atomic compare and exchange 33 * @uaddr: The address of the futex to be modified 34 * @oldval: The expected value of the futex 35 * @newval: The new value to try and assign the futex 36 * 37 * Return the old value of addr->val. 38 */ 39 static inline int 40 atomic_cmpxchg(atomic_t *addr, int oldval, int newval) 41 { 42 return __sync_val_compare_and_swap(&addr->val, oldval, newval); 43 } 44 45 /** 46 * atomic_inc() - Atomic incrememnt 47 * @addr: Address of the variable to increment 48 * 49 * Return the new value of addr->val. 50 */ 51 static inline int 52 atomic_inc(atomic_t *addr) 53 { 54 return __sync_add_and_fetch(&addr->val, 1); 55 } 56 57 /** 58 * atomic_dec() - Atomic decrement 59 * @addr: Address of the variable to decrement 60 * 61 * Return the new value of addr-val. 62 */ 63 static inline int 64 atomic_dec(atomic_t *addr) 65 { 66 return __sync_sub_and_fetch(&addr->val, 1); 67 } 68 69 /** 70 * atomic_set() - Atomic set 71 * @addr: Address of the variable to set 72 * @newval: New value for the atomic_t 73 * 74 * Return the new value of addr->val. 75 */ 76 static inline int 77 atomic_set(atomic_t *addr, int newval) 78 { 79 addr->val = newval; 80 return newval; 81 } 82 83 #endif 84