gcas.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright (C) 2012-2015 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 cas, 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_CAS_,N)) \
  26. return __atomic_compare_exchange_n \
  27. (PTR(N,mptr), PTR(N,eptr), *PTR(N,dptr), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
  28. #else
  29. # define EXACT_INLINE(N)
  30. #endif
  31. /* ... and if all that fails, invoke the function we generated elsewhere.
  32. Worst case, this will *also* use locks. */
  33. #define EXACT(N) \
  34. do { \
  35. if (!C2(HAVE_INT,N)) break; \
  36. if ((uintptr_t)mptr & (N - 1)) break; \
  37. EXACT_INLINE (N); \
  38. return C3(local_,compare_exchange_,N) \
  39. (PTR(N,mptr), PTR(N,eptr), *PTR(N,dptr), smodel, fmodel); \
  40. } while (0)
  41. #define LARGER(N) \
  42. do { \
  43. if (!C2(HAVE_INT,N)) break; \
  44. if (!C2(HAVE_ATOMIC_LDST_,N)) break; \
  45. if (!C2(MAYBE_HAVE_ATOMIC_CAS_,N)) break; \
  46. r = (uintptr_t)mptr & (N - 1); \
  47. a = (uintptr_t)mptr & -N; \
  48. if (r + n <= N) \
  49. { \
  50. pre_barrier (smodel); \
  51. u.C2(i,N) = __atomic_load_n (PTR(N,a), __ATOMIC_RELAXED); \
  52. do { \
  53. if (memcmp (u.b + r, eptr, n) != 0) goto Lfail; \
  54. v = u; memcpy (v.b + r, dptr, n); \
  55. } while (!(C2(HAVE_ATOMIC_CAS_,N) \
  56. ? __atomic_compare_exchange_n (PTR(N,a), \
  57. &u.C2(i,N), v.C2(i,N), true, \
  58. __ATOMIC_RELAXED, __ATOMIC_RELAXED) \
  59. : C3(local_,compare_exchange_,N) (PTR(N,a), \
  60. &u.C2(i,N), v.C2(i,N), \
  61. __ATOMIC_RELAXED, __ATOMIC_RELAXED))); \
  62. goto Lsucc; \
  63. } \
  64. } while (0)
  65. bool
  66. libat_compare_exchange (size_t n, void *mptr, void *eptr, void *dptr,
  67. int smodel, int fmodel)
  68. {
  69. union max_size_u u, v;
  70. uintptr_t r, a;
  71. bool ret;
  72. switch (n)
  73. {
  74. case 0: return true;
  75. case 1: EXACT(1); goto L4;
  76. case 2: EXACT(2); goto L4;
  77. case 4: EXACT(4); goto L8;
  78. case 8: EXACT(8); goto L16;
  79. case 16: EXACT(16); break;
  80. case 3: L4: LARGER(4); /* FALLTHRU */
  81. case 5 ... 7: L8: LARGER(8); /* FALLTHRU */
  82. case 9 ... 15: L16: LARGER(16); break;
  83. Lsucc:
  84. post_barrier (smodel);
  85. return true;
  86. Lfail:
  87. post_barrier (fmodel);
  88. memcpy (eptr, u.b + r, n);
  89. return false;
  90. }
  91. pre_seq_barrier (smodel);
  92. libat_lock_n (mptr, n);
  93. ret = memcmp (mptr, eptr, n) == 0;
  94. memcpy ((ret ? mptr : eptr), (ret ? dptr : mptr), n);
  95. libat_unlock_n (mptr, n);
  96. post_seq_barrier (ret ? smodel : fmodel);
  97. return ret;
  98. }
  99. EXPORT_ALIAS (compare_exchange);