atomic.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifdef __thumb2__
  31. #define atomic_full_barrier() \
  32. __asm__ __volatile__ \
  33. ("movw\tip, #0x0fa0\n\t" \
  34. "movt\tip, #0xffff\n\t" \
  35. "blx\tip" \
  36. : : : "ip", "lr", "cc", "memory");
  37. #else
  38. #define atomic_full_barrier() \
  39. __asm__ __volatile__ \
  40. ("mov\tip, #0xffff0fff\n\t" \
  41. "mov\tlr, pc\n\t" \
  42. "add\tpc, ip, #(0xffff0fa0 - 0xffff0fff)" \
  43. : : : "ip", "lr", "cc", "memory");
  44. #endif
  45. /* Atomic compare and exchange. This sequence relies on the kernel to
  46. provide a compare and exchange operation which is atomic on the
  47. current architecture, either via cleverness on pre-ARMv6 or via
  48. ldrex / strex on ARMv6. */
  49. #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
  50. ({ __arm_link_error (); oldval; })
  51. #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
  52. ({ __arm_link_error (); oldval; })
  53. /* It doesn't matter what register is used for a_oldval2, but we must
  54. specify one to work around GCC PR rtl-optimization/21223. Otherwise
  55. it may cause a_oldval or a_tmp to be moved to a different register. */
  56. #ifdef __thumb2__
  57. /* Thumb-2 has ldrex/strex. However it does not have barrier instructions,
  58. so we still need to use the kernel helper. */
  59. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  60. ({ register __typeof (oldval) a_oldval __asm__ ("r0"); \
  61. register __typeof (oldval) a_newval __asm__ ("r1") = (newval); \
  62. register __typeof (mem) a_ptr __asm__ ("r2") = (mem); \
  63. register __typeof (oldval) a_tmp __asm__ ("r3"); \
  64. register __typeof (oldval) a_oldval2 __asm__ ("r4") = (oldval); \
  65. __asm__ __volatile__ \
  66. ("0:\tldr\t%[tmp],[%[ptr]]\n\t" \
  67. "cmp\t%[tmp], %[old2]\n\t" \
  68. "bne\t1f\n\t" \
  69. "mov\t%[old], %[old2]\n\t" \
  70. "movw\t%[tmp], #0x0fc0\n\t" \
  71. "movt\t%[tmp], #0xffff\n\t" \
  72. "blx\t%[tmp]\n\t" \
  73. "bcc\t0b\n\t" \
  74. "mov\t%[tmp], %[old2]\n\t" \
  75. "1:" \
  76. : [old] "=&r" (a_oldval), [tmp] "=&r" (a_tmp) \
  77. : [new] "r" (a_newval), [ptr] "r" (a_ptr), \
  78. [old2] "r" (a_oldval2) \
  79. : "ip", "lr", "cc", "memory"); \
  80. a_tmp; })
  81. #else
  82. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  83. ({ register __typeof (oldval) a_oldval __asm__ ("r0"); \
  84. register __typeof (oldval) a_newval __asm__ ("r1") = (newval); \
  85. register __typeof (mem) a_ptr __asm__ ("r2") = (mem); \
  86. register __typeof (oldval) a_tmp __asm__ ("r3"); \
  87. register __typeof (oldval) a_oldval2 __asm__ ("r4") = (oldval); \
  88. __asm__ __volatile__ \
  89. ("0:\tldr\t%[tmp],[%[ptr]]\n\t" \
  90. "cmp\t%[tmp], %[old2]\n\t" \
  91. "bne\t1f\n\t" \
  92. "mov\t%[old], %[old2]\n\t" \
  93. "mov\t%[tmp], #0xffff0fff\n\t" \
  94. "mov\tlr, pc\n\t" \
  95. "add\tpc, %[tmp], #(0xffff0fc0 - 0xffff0fff)\n\t" \
  96. "bcc\t0b\n\t" \
  97. "mov\t%[tmp], %[old2]\n\t" \
  98. "1:" \
  99. : [old] "=&r" (a_oldval), [tmp] "=&r" (a_tmp) \
  100. : [new] "r" (a_newval), [ptr] "r" (a_ptr), \
  101. [old2] "r" (a_oldval2) \
  102. : "ip", "lr", "cc", "memory"); \
  103. a_tmp; })
  104. #endif
  105. #define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  106. ({ __arm_link_error (); oldval; })