store_n.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_LDST)
  22. void
  23. SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
  24. {
  25. if (maybe_specialcase_relaxed(smodel))
  26. __atomic_store_n (mptr, newval, __ATOMIC_RELAXED);
  27. else if (maybe_specialcase_acqrel(smodel))
  28. /* Note that ACQ and ACQ_REL are not valid for store. */
  29. __atomic_store_n (mptr, newval, __ATOMIC_RELEASE);
  30. else
  31. __atomic_store_n (mptr, newval, __ATOMIC_SEQ_CST);
  32. }
  33. #define DONE 1
  34. #endif /* HAVE_ATOMIC_STORE */
  35. /* If we have compare-and-swap, use it perform the store. */
  36. #if !DONE && defined(atomic_compare_exchange_n)
  37. void
  38. SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
  39. {
  40. UTYPE oldval;
  41. pre_barrier (smodel);
  42. oldval = *mptr;
  43. while (!atomic_compare_exchange_n (mptr, &oldval, newval, true,
  44. __ATOMIC_RELAXED, __ATOMIC_RELAXED))
  45. continue;
  46. post_barrier (smodel);
  47. }
  48. #define DONE 1
  49. #endif /* atomic_compare_exchange_n */
  50. /* If this type is smaller than word-sized, fall back to a word-sized
  51. compare-and-swap loop. */
  52. #if !DONE && N < WORDSIZE && defined(atomic_compare_exchange_w)
  53. void
  54. SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
  55. {
  56. UWORD mask, shift, woldval, wnewval, t, *wptr;
  57. pre_barrier (smodel);
  58. wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
  59. shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
  60. mask = SIZE(MASK) << shift;
  61. wnewval = (UWORD)newval << shift;
  62. woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
  63. do
  64. {
  65. t = (woldval & ~mask) | wnewval;
  66. }
  67. while (!atomic_compare_exchange_w (wptr, &woldval, t));
  68. post_barrier (smodel);
  69. }
  70. #define DONE 1
  71. #endif /* N < WORDSIZE && atomic_compare_exchange_w */
  72. /* Otherwise, fall back to some sort of protection mechanism. */
  73. #if !DONE
  74. void
  75. SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
  76. {
  77. UWORD magic;
  78. pre_seq_barrier (smodel);
  79. magic = protect_start (mptr);
  80. *mptr = newval;
  81. protect_end (mptr, magic);
  82. post_seq_barrier (smodel);
  83. }
  84. #endif
  85. EXPORT_ALIAS (SIZE(store));