gexch.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /* If we natively support the exchange, and if we're unconcerned with extra
  21. barriers (e.g. fully in-order cpu for which barriers are a nop), then
  22. go ahead and expand the operation inline. */
  23. #if !defined(WANT_SPECIALCASE_RELAXED) && !defined(__OPTIMIZE_SIZE__)
  24. # define EXACT_INLINE(N) \
  25. if (C2(HAVE_ATOMIC_EXCHANGE_,N)) \
  26. { \
  27. *PTR(N,rptr) = __atomic_exchange_n \
  28. (PTR(N,mptr), *PTR(N,vptr), __ATOMIC_SEQ_CST); \
  29. return; \
  30. }
  31. #else
  32. # define EXACT_INLINE(N)
  33. #endif
  34. #define EXACT(N) \
  35. do { \
  36. if (!C2(HAVE_INT,N)) break; \
  37. if ((uintptr_t)mptr & (N - 1)) break; \
  38. EXACT_INLINE (N); \
  39. *PTR(N,rptr) = C3(local_,exchange_,N) \
  40. (PTR(N,mptr), *PTR(N,vptr), smodel); \
  41. return; \
  42. } while (0)
  43. #define LARGER(N) \
  44. do { \
  45. if (!C2(HAVE_INT,N)) break; \
  46. if (!C2(MAYBE_HAVE_ATOMIC_CAS_,N)) break; \
  47. r = (uintptr_t)mptr & (N - 1); \
  48. a = (uintptr_t)mptr & -N; \
  49. if (r + n <= N) \
  50. { \
  51. pre_barrier (smodel); \
  52. u.C2(i,N) = *PTR(N,a); \
  53. do { \
  54. v = u; \
  55. memcpy (v.b + r, vptr, n); \
  56. } while (!(C2(HAVE_ATOMIC_CAS_,N) \
  57. ? __atomic_compare_exchange_n (PTR(N,a), \
  58. &u.C2(i,N), v.C2(i,N), true, \
  59. __ATOMIC_RELAXED, __ATOMIC_RELAXED) \
  60. : C3(local_,compare_exchange_,N) (PTR(N,a), \
  61. &u.C2(i,N), v.C2(i,N), \
  62. __ATOMIC_RELAXED, __ATOMIC_RELAXED))); \
  63. goto Lfinish; \
  64. } \
  65. } while (0)
  66. static void __attribute__((noinline))
  67. libat_exchange_large_inplace (size_t n, void *mptr, void *vptr)
  68. {
  69. #define BUF 1024
  70. char temp[BUF];
  71. size_t i = 0;
  72. for (i = 0; n >= BUF; i += BUF, n -= BUF)
  73. {
  74. memcpy (temp, mptr + i, BUF);
  75. memcpy (mptr + i, vptr + i, BUF);
  76. memcpy (vptr + i, temp, BUF);
  77. }
  78. if (n > 0)
  79. {
  80. memcpy (temp, mptr + i, n);
  81. memcpy (mptr + i, vptr + i, n);
  82. memcpy (vptr + i, temp, n);
  83. }
  84. #undef BUF
  85. }
  86. void
  87. libat_exchange (size_t n, void *mptr, void *vptr, void *rptr, int smodel)
  88. {
  89. union max_size_u u, v;
  90. uintptr_t r, a;
  91. switch (n)
  92. {
  93. case 0: return;
  94. case 1: EXACT(1); goto L4;
  95. case 2: EXACT(2); goto L4;
  96. case 4: EXACT(4); goto L8;
  97. case 8: EXACT(8); goto L16;
  98. case 16: EXACT(16); break;
  99. case 3: L4: LARGER(4); /* FALLTHRU */
  100. case 5 ... 7: L8: LARGER(8); /* FALLTHRU */
  101. case 9 ... 15: L16: LARGER(16); break;
  102. Lfinish:
  103. post_barrier (smodel);
  104. memcpy (rptr, u.b + r, n);
  105. return;
  106. }
  107. pre_seq_barrier (smodel);
  108. libat_lock_n (mptr, n);
  109. if (vptr != rptr)
  110. {
  111. memcpy (rptr, mptr, n);
  112. memcpy (mptr, vptr, n);
  113. }
  114. else
  115. libat_exchange_large_inplace (n, mptr, vptr);
  116. libat_unlock_n (mptr, n);
  117. post_seq_barrier (smodel);
  118. }
  119. EXPORT_ALIAS (exchange);