fop_n.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright (C) 2012-2022 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Atomic Library (libatomic).
  4. Libatomic is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include <libatomic_i.h>
  20. /* This file is included multiple times with required defines:
  21. NAME the name of the operation that we're implementing;
  22. OP a two-operand functional macro the implements the operation.
  23. */
  24. /* If we support the builtin, just use it. */
  25. #if !DONE && SIZE(HAVE_ATOMIC_FETCH_OP)
  26. UTYPE
  27. SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
  28. {
  29. if (maybe_specialcase_relaxed(smodel))
  30. return C2(__atomic_fetch_,NAME) (mptr, opval, __ATOMIC_RELAXED);
  31. else if (maybe_specialcase_acqrel(smodel))
  32. return C2(__atomic_fetch_,NAME) (mptr, opval, __ATOMIC_ACQ_REL);
  33. else
  34. return C2(__atomic_fetch_,NAME) (mptr, opval, __ATOMIC_SEQ_CST);
  35. }
  36. UTYPE
  37. SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel)
  38. {
  39. if (maybe_specialcase_relaxed(smodel))
  40. return C3(__atomic_,NAME,_fetch) (mptr, opval, __ATOMIC_RELAXED);
  41. else if (maybe_specialcase_acqrel(smodel))
  42. return C3(__atomic_,NAME,_fetch) (mptr, opval, __ATOMIC_ACQ_REL);
  43. else
  44. return C3(__atomic_,NAME,_fetch) (mptr, opval, __ATOMIC_SEQ_CST);
  45. }
  46. #define DONE 1
  47. #endif /* HAVE_ATOMIC_FETCH_OP */
  48. #if !DONE && defined(atomic_compare_exchange_n)
  49. UTYPE
  50. SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
  51. {
  52. UTYPE oldval, t;
  53. pre_barrier (smodel);
  54. oldval = *mptr;
  55. do
  56. {
  57. t = OP(oldval, opval);
  58. }
  59. while (!atomic_compare_exchange_n (mptr, &oldval, t, true,
  60. __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  61. post_barrier (smodel);
  62. return oldval;
  63. }
  64. UTYPE
  65. SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel)
  66. {
  67. UTYPE oldval, t;
  68. pre_barrier (smodel);
  69. oldval = *mptr;
  70. do
  71. {
  72. t = OP(oldval, opval);
  73. }
  74. while (!atomic_compare_exchange_n (mptr, &oldval, t, true,
  75. __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  76. post_barrier (smodel);
  77. return t;
  78. }
  79. #define DONE 1
  80. #endif /* atomic_compare_exchange_n */
  81. /* If this type is no larger than word-sized, fall back to a word-sized
  82. compare-and-swap loop. */
  83. #if !DONE && N < WORDSIZE && defined(atomic_compare_exchange_w)
  84. UTYPE
  85. SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
  86. {
  87. UWORD mask, shift, woldval, wopval, t, *wptr;
  88. pre_barrier (smodel);
  89. wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
  90. shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
  91. mask = SIZE(MASK) << shift;
  92. wopval = (UWORD)opval << shift;
  93. woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
  94. do
  95. {
  96. t = (woldval & ~mask) | (OP(woldval, wopval) & mask);
  97. }
  98. while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
  99. __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  100. post_barrier (smodel);
  101. return woldval >> shift;
  102. }
  103. UTYPE
  104. SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel)
  105. {
  106. UWORD mask, shift, woldval, wopval, t, *wptr;
  107. pre_barrier (smodel);
  108. wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
  109. shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
  110. mask = SIZE(MASK) << shift;
  111. wopval = (UWORD)opval << shift;
  112. woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
  113. do
  114. {
  115. t = (woldval & ~mask) | (OP(woldval, wopval) & mask);
  116. }
  117. while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
  118. __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  119. post_barrier (smodel);
  120. return t >> shift;
  121. }
  122. #define DONE 1
  123. #endif /* atomic_compare_exchange_w */
  124. /* Otherwise, fall back to some sort of protection mechanism. */
  125. #if !DONE
  126. UTYPE
  127. SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel UNUSED)
  128. {
  129. UTYPE ret;
  130. UWORD magic;
  131. pre_seq_barrier (smodel);
  132. magic = protect_start (mptr);
  133. ret = *mptr;
  134. *mptr = OP(ret, opval);
  135. protect_end (mptr, magic);
  136. post_seq_barrier (smodel);
  137. return ret;
  138. }
  139. UTYPE
  140. SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel UNUSED)
  141. {
  142. UTYPE ret;
  143. UWORD magic;
  144. pre_seq_barrier (smodel);
  145. magic = protect_start (mptr);
  146. ret = OP (*mptr, opval);
  147. *mptr = ret;
  148. protect_end (mptr, magic);
  149. post_seq_barrier (smodel);
  150. return ret;
  151. }
  152. #endif
  153. EXPORT_ALIAS (SIZE(C2(fetch_,NAME)));
  154. EXPORT_ALIAS (SIZE(C2(NAME,_fetch)));