atomic.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #if defined __thumb__ && !defined __thumb2__
  16. #include_next <common/bits/atomic.h>
  17. #else
  18. #include <stdint.h>
  19. #include <sysdep.h>
  20. typedef int8_t atomic8_t;
  21. typedef uint8_t uatomic8_t;
  22. typedef int_fast8_t atomic_fast8_t;
  23. typedef uint_fast8_t uatomic_fast8_t;
  24. typedef int32_t atomic32_t;
  25. typedef uint32_t uatomic32_t;
  26. typedef int_fast32_t atomic_fast32_t;
  27. typedef uint_fast32_t uatomic_fast32_t;
  28. typedef intptr_t atomicptr_t;
  29. typedef uintptr_t uatomicptr_t;
  30. typedef intmax_t atomic_max_t;
  31. typedef uintmax_t uatomic_max_t;
  32. void __arm_link_error (void);
  33. /* Use the atomic builtins provided by GCC in case the backend provides
  34. a pattern to do this efficiently. */
  35. #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
  36. #define atomic_full_barrier() __sync_synchronize ()
  37. #elif defined __thumb2__
  38. #define atomic_full_barrier() \
  39. __asm__ __volatile__ \
  40. ("movw\tip, #0x0fa0\n\t" \
  41. "movt\tip, #0xffff\n\t" \
  42. "blx\tip" \
  43. : : : "ip", "lr", "cc", "memory");
  44. #else
  45. #define atomic_full_barrier() \
  46. __asm__ __volatile__ \
  47. ("mov\tip, #0xffff0fff\n\t" \
  48. "mov\tlr, pc\n\t" \
  49. "add\tpc, ip, #(0xffff0fa0 - 0xffff0fff)" \
  50. : : : "ip", "lr", "cc", "memory");
  51. #endif
  52. /* Atomic compare and exchange. This sequence relies on the kernel to
  53. provide a compare and exchange operation which is atomic on the
  54. current architecture, either via cleverness on pre-ARMv6 or via
  55. ldrex / strex on ARMv6. */
  56. #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
  57. ({ __arm_link_error (); oldval; })
  58. #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
  59. ({ __arm_link_error (); oldval; })
  60. #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
  61. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  62. __sync_val_compare_and_swap ((mem), (oldval), (newval))
  63. /* It doesn't matter what register is used for a_oldval2, but we must
  64. specify one to work around GCC PR rtl-optimization/21223. Otherwise
  65. it may cause a_oldval or a_tmp to be moved to a different register. */
  66. #elif defined __thumb2__
  67. /* Thumb-2 has ldrex/strex. However it does not have barrier instructions,
  68. so we still need to use the kernel helper. */
  69. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  70. ({ register __typeof (oldval) a_oldval __asm__ ("r0"); \
  71. register __typeof (oldval) a_newval __asm__ ("r1") = (newval); \
  72. register __typeof (mem) a_ptr __asm__ ("r2") = (mem); \
  73. register __typeof (oldval) a_tmp __asm__ ("r3"); \
  74. register __typeof (oldval) a_oldval2 __asm__ ("r4") = (oldval); \
  75. __asm__ __volatile__ \
  76. ("0:\tldr\t%[tmp],[%[ptr]]\n\t" \
  77. "cmp\t%[tmp], %[old2]\n\t" \
  78. "bne\t1f\n\t" \
  79. "mov\t%[old], %[old2]\n\t" \
  80. "movw\t%[tmp], #0x0fc0\n\t" \
  81. "movt\t%[tmp], #0xffff\n\t" \
  82. "blx\t%[tmp]\n\t" \
  83. "bcc\t0b\n\t" \
  84. "mov\t%[tmp], %[old2]\n\t" \
  85. "1:" \
  86. : [old] "=&r" (a_oldval), [tmp] "=&r" (a_tmp) \
  87. : [new] "r" (a_newval), [ptr] "r" (a_ptr), \
  88. [old2] "r" (a_oldval2) \
  89. : "ip", "lr", "cc", "memory"); \
  90. a_tmp; })
  91. #else
  92. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  93. ({ register __typeof (oldval) a_oldval __asm__ ("r0"); \
  94. register __typeof (oldval) a_newval __asm__ ("r1") = (newval); \
  95. register __typeof (mem) a_ptr __asm__ ("r2") = (mem); \
  96. register __typeof (oldval) a_tmp __asm__ ("r3"); \
  97. register __typeof (oldval) a_oldval2 __asm__ ("r4") = (oldval); \
  98. __asm__ __volatile__ \
  99. ("0:\tldr\t%[tmp],[%[ptr]]\n\t" \
  100. "cmp\t%[tmp], %[old2]\n\t" \
  101. "bne\t1f\n\t" \
  102. "mov\t%[old], %[old2]\n\t" \
  103. "mov\t%[tmp], #0xffff0fff\n\t" \
  104. "mov\tlr, pc\n\t" \
  105. "add\tpc, %[tmp], #(0xffff0fc0 - 0xffff0fff)\n\t" \
  106. "bcc\t0b\n\t" \
  107. "mov\t%[tmp], %[old2]\n\t" \
  108. "1:" \
  109. : [old] "=&r" (a_oldval), [tmp] "=&r" (a_tmp) \
  110. : [new] "r" (a_newval), [ptr] "r" (a_ptr), \
  111. [old2] "r" (a_oldval2) \
  112. : "ip", "lr", "cc", "memory"); \
  113. a_tmp; })
  114. #endif
  115. #define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  116. ({ __arm_link_error (); oldval; })
  117. #endif