atomic.h 5.3 KB

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