atomic.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #if defined __thumb__ && !defined __thumb2__
  15. #include_next <common/bits/atomic.h>
  16. #else
  17. #include <stdint.h>
  18. #include <sysdep.h>
  19. typedef int8_t atomic8_t;
  20. typedef uint8_t uatomic8_t;
  21. typedef int_fast8_t atomic_fast8_t;
  22. typedef uint_fast8_t uatomic_fast8_t;
  23. typedef int32_t atomic32_t;
  24. typedef uint32_t uatomic32_t;
  25. typedef int_fast32_t atomic_fast32_t;
  26. typedef uint_fast32_t uatomic_fast32_t;
  27. typedef intptr_t atomicptr_t;
  28. typedef uintptr_t uatomicptr_t;
  29. typedef intmax_t atomic_max_t;
  30. typedef uintmax_t uatomic_max_t;
  31. void __arm_link_error (void);
  32. /* Use the atomic builtins provided by GCC in case the backend provides
  33. a pattern to do this efficiently. */
  34. #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
  35. #define atomic_full_barrier() __sync_synchronize ()
  36. #elif defined __thumb2__
  37. #define atomic_full_barrier() \
  38. __asm__ __volatile__ \
  39. ("movw\tip, #0x0fa0\n\t" \
  40. "movt\tip, #0xffff\n\t" \
  41. "blx\tip" \
  42. : : : "ip", "lr", "cc", "memory");
  43. #else
  44. #define atomic_full_barrier() \
  45. __asm__ __volatile__ \
  46. ("mov\tip, #0xffff0fff\n\t" \
  47. "mov\tlr, pc\n\t" \
  48. "add\tpc, ip, #(0xffff0fa0 - 0xffff0fff)" \
  49. : : : "ip", "lr", "cc", "memory");
  50. #endif
  51. /* Atomic compare and exchange. This sequence relies on the kernel to
  52. provide a compare and exchange operation which is atomic on the
  53. current architecture, either via cleverness on pre-ARMv6 or via
  54. ldrex / strex on ARMv6. */
  55. #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
  56. ({ __arm_link_error (); oldval; })
  57. #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
  58. ({ __arm_link_error (); oldval; })
  59. #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
  60. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  61. __sync_val_compare_and_swap ((mem), (oldval), (newval))
  62. /* It doesn't matter what register is used for a_oldval2, but we must
  63. specify one to work around GCC PR rtl-optimization/21223. Otherwise
  64. it may cause a_oldval or a_tmp to be moved to a different register. */
  65. #elif defined __thumb2__
  66. /* Thumb-2 has ldrex/strex. However it does not have barrier instructions,
  67. so we still need to use the kernel helper. */
  68. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  69. ({ register __typeof (oldval) a_oldval __asm__ ("r0"); \
  70. register __typeof (oldval) a_newval __asm__ ("r1") = (newval); \
  71. register __typeof (mem) a_ptr __asm__ ("r2") = (mem); \
  72. register __typeof (oldval) a_tmp __asm__ ("r3"); \
  73. register __typeof (oldval) a_oldval2 __asm__ ("r4") = (oldval); \
  74. __asm__ __volatile__ \
  75. ("0:\tldr\t%[tmp],[%[ptr]]\n\t" \
  76. "cmp\t%[tmp], %[old2]\n\t" \
  77. "bne\t1f\n\t" \
  78. "mov\t%[old], %[old2]\n\t" \
  79. "movw\t%[tmp], #0x0fc0\n\t" \
  80. "movt\t%[tmp], #0xffff\n\t" \
  81. "blx\t%[tmp]\n\t" \
  82. "bcc\t0b\n\t" \
  83. "mov\t%[tmp], %[old2]\n\t" \
  84. "1:" \
  85. : [old] "=&r" (a_oldval), [tmp] "=&r" (a_tmp) \
  86. : [new] "r" (a_newval), [ptr] "r" (a_ptr), \
  87. [old2] "r" (a_oldval2) \
  88. : "ip", "lr", "cc", "memory"); \
  89. a_tmp; })
  90. #else
  91. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  92. ({ register __typeof (oldval) a_oldval __asm__ ("r0"); \
  93. register __typeof (oldval) a_newval __asm__ ("r1") = (newval); \
  94. register __typeof (mem) a_ptr __asm__ ("r2") = (mem); \
  95. register __typeof (oldval) a_tmp __asm__ ("r3"); \
  96. register __typeof (oldval) a_oldval2 __asm__ ("r4") = (oldval); \
  97. __asm__ __volatile__ \
  98. ("0:\tldr\t%[tmp],[%[ptr]]\n\t" \
  99. "cmp\t%[tmp], %[old2]\n\t" \
  100. "bne\t1f\n\t" \
  101. "mov\t%[old], %[old2]\n\t" \
  102. "mov\t%[tmp], #0xffff0fff\n\t" \
  103. "mov\tlr, pc\n\t" \
  104. "add\tpc, %[tmp], #(0xffff0fc0 - 0xffff0fff)\n\t" \
  105. "bcc\t0b\n\t" \
  106. "mov\t%[tmp], %[old2]\n\t" \
  107. "1:" \
  108. : [old] "=&r" (a_oldval), [tmp] "=&r" (a_tmp) \
  109. : [new] "r" (a_newval), [ptr] "r" (a_ptr), \
  110. [old2] "r" (a_oldval2) \
  111. : "ip", "lr", "cc", "memory"); \
  112. a_tmp; })
  113. #endif
  114. #define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  115. ({ __arm_link_error (); oldval; })
  116. #endif