exch_n.c 3.3 KB

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