atomic.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef _ATOMIC_H
  22. #define _ATOMIC_H
  23. typedef struct {
  24. volatile int val;
  25. } atomic_t;
  26. #define ATOMIC_INITIALIZER { 0 }
  27. /**
  28. * atomic_cmpxchg() - Atomic compare and exchange
  29. * @uaddr: The address of the futex to be modified
  30. * @oldval: The expected value of the futex
  31. * @newval: The new value to try and assign the futex
  32. *
  33. * Return the old value of addr->val.
  34. */
  35. static inline int
  36. atomic_cmpxchg(atomic_t *addr, int oldval, int newval)
  37. {
  38. return __sync_val_compare_and_swap(&addr->val, oldval, newval);
  39. }
  40. /**
  41. * atomic_inc() - Atomic incrememnt
  42. * @addr: Address of the variable to increment
  43. *
  44. * Return the new value of addr->val.
  45. */
  46. static inline int
  47. atomic_inc(atomic_t *addr)
  48. {
  49. return __sync_add_and_fetch(&addr->val, 1);
  50. }
  51. /**
  52. * atomic_dec() - Atomic decrement
  53. * @addr: Address of the variable to decrement
  54. *
  55. * Return the new value of addr-val.
  56. */
  57. static inline int
  58. atomic_dec(atomic_t *addr)
  59. {
  60. return __sync_sub_and_fetch(&addr->val, 1);
  61. }
  62. /**
  63. * atomic_set() - Atomic set
  64. * @addr: Address of the variable to set
  65. * @newval: New value for the atomic_t
  66. *
  67. * Return the new value of addr->val.
  68. */
  69. static inline int
  70. atomic_set(atomic_t *addr, int newval)
  71. {
  72. addr->val = newval;
  73. return newval;
  74. }
  75. #endif