cas_n.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 support the builtin, just use it. */
  21. #if !DONE && defined(atomic_compare_exchange_n)
  22. bool
  23. SIZE(libat_compare_exchange) (UTYPE *mptr, UTYPE *eptr, UTYPE newval,
  24. int smodel, int fmodel UNUSED)
  25. {
  26. if (maybe_specialcase_relaxed(smodel))
  27. return atomic_compare_exchange_n (mptr, eptr, newval, false,
  28. __ATOMIC_RELAXED, __ATOMIC_RELAXED);
  29. else if (maybe_specialcase_acqrel(smodel))
  30. return atomic_compare_exchange_n (mptr, eptr, newval, false,
  31. __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
  32. else
  33. return atomic_compare_exchange_n (mptr, eptr, newval, false,
  34. __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
  35. }
  36. #define DONE 1
  37. #endif /* HAVE_ATOMIC_CAS */
  38. /* If this type is not larger than word-sized, fall back to a word-sized
  39. compare-and-swap loop, possibly assisted by the OS. */
  40. #if !DONE && N <= WORDSIZE && defined(atomic_compare_exchange_w)
  41. bool
  42. SIZE(libat_compare_exchange) (UTYPE *mptr, UTYPE *eptr, UTYPE newval,
  43. int smodel, int fmodel)
  44. {
  45. UWORD mask, shift, weval, woldval, wnewval, t, *wptr;
  46. pre_barrier (smodel);
  47. if (N < WORDSIZE)
  48. {
  49. wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
  50. shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
  51. mask = SIZE(MASK) << shift;
  52. }
  53. else
  54. {
  55. wptr = (UWORD *)mptr;
  56. shift = 0;
  57. mask = -1;
  58. }
  59. weval = (UWORD)*eptr << shift;
  60. wnewval = (UWORD)newval << shift;
  61. woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
  62. do
  63. {
  64. if ((woldval & mask) != weval)
  65. goto failure;
  66. t = (woldval & ~mask) | wnewval;
  67. }
  68. while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
  69. __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  70. post_barrier (smodel);
  71. return true;
  72. failure:
  73. *eptr = woldval >> shift;
  74. post_barrier (fmodel);
  75. return false;
  76. }
  77. #define DONE 1
  78. #endif /* HAVE_ATOMIC_CAS && N <= WORDSIZE */
  79. /* Otherwise, fall back to some sort of protection mechanism. */
  80. #if !DONE
  81. bool
  82. SIZE(libat_compare_exchange) (UTYPE *mptr, UTYPE *eptr, UTYPE newval,
  83. int smodel, int fmodel UNUSED)
  84. {
  85. UTYPE oldval;
  86. UWORD magic;
  87. bool ret;
  88. pre_seq_barrier (smodel);
  89. magic = protect_start (mptr);
  90. oldval = *mptr;
  91. ret = (oldval == *eptr);
  92. if (ret)
  93. *mptr = newval;
  94. else
  95. *eptr = oldval;
  96. protect_end (mptr, magic);
  97. post_seq_barrier (smodel);
  98. return ret;
  99. }
  100. #endif
  101. EXPORT_ALIAS (SIZE(compare_exchange));