atomic.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (C) 2010-2012 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Maxim Kuvyrkov <maxim@codesourcery.com>, 2010.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _OR1K_BITS_ATOMIC_H
  16. #define _OR1K_BITS_ATOMIC_H
  17. #include <stdint.h>
  18. typedef int8_t atomic8_t;
  19. typedef uint8_t uatomic8_t;
  20. typedef int_fast8_t atomic_fast8_t;
  21. typedef uint_fast8_t uatomic_fast8_t;
  22. typedef int16_t atomic16_t;
  23. typedef uint16_t uatomic16_t;
  24. typedef int_fast16_t atomic_fast16_t;
  25. typedef uint_fast16_t uatomic_fast16_t;
  26. typedef int32_t atomic32_t;
  27. typedef uint32_t uatomic32_t;
  28. typedef int_fast32_t atomic_fast32_t;
  29. typedef uint_fast32_t uatomic_fast32_t;
  30. typedef int64_t atomic64_t;
  31. typedef uint64_t uatomic64_t;
  32. typedef int_fast64_t atomic_fast64_t;
  33. typedef uint_fast64_t uatomic_fast64_t;
  34. typedef intptr_t atomicptr_t;
  35. typedef uintptr_t uatomicptr_t;
  36. typedef intmax_t atomic_max_t;
  37. typedef uintmax_t uatomic_max_t;
  38. #ifndef atomic_full_barrier
  39. # define atomic_full_barrier() __asm__ ("" ::: "memory")
  40. #endif
  41. #ifndef atomic_read_barrier
  42. # define atomic_read_barrier() atomic_full_barrier ()
  43. #endif
  44. #ifndef atomic_write_barrier
  45. # define atomic_write_barrier() atomic_full_barrier ()
  46. #endif
  47. #define atomic_exchange_acq(mem, newval) \
  48. ({ __typeof (newval) val; \
  49. \
  50. __asm__ volatile ( \
  51. "1: l.lwa %0, 0(%1) \n" \
  52. " l.swa 0(%1), %2 \n" \
  53. " l.bnf 1b \n" \
  54. " l.nop \n" \
  55. : "=&r"(val) \
  56. : "r"(mem), "r"(newval) \
  57. : "cc", "memory"); \
  58. val; })
  59. #define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
  60. ({ __typeof (newval) val; \
  61. \
  62. __asm__ volatile ( \
  63. "1: l.lwa %0, 0(%1) \n" \
  64. " l.sfeq %0, %2 \n" \
  65. " l.bnf 2f \n" \
  66. " l.nop \n" \
  67. " l.swa 0(%1), %3 \n" \
  68. " l.bnf 1b \n" \
  69. " l.nop \n" \
  70. "2: \n" \
  71. : "=&r"(val) \
  72. : "r"(mem), "r"(oldval), "r"(newval) \
  73. : "cc", "memory"); \
  74. val; })
  75. #endif