load_n.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. UTYPE
  23. SIZE(libat_load) (UTYPE *mptr, int smodel)
  24. {
  25. if (maybe_specialcase_relaxed(smodel))
  26. return __atomic_load_n (mptr, __ATOMIC_RELAXED);
  27. else if (maybe_specialcase_acqrel(smodel))
  28. /* Note that REL and ACQ_REL are not valid for loads. */
  29. return __atomic_load_n (mptr, __ATOMIC_ACQUIRE);
  30. else
  31. return __atomic_load_n (mptr, __ATOMIC_SEQ_CST);
  32. }
  33. #define DONE 1
  34. #endif /* HAVE_ATOMIC_LOAD */
  35. /* If we have compare-and-swap, use it to swap 0 with 0 and as a side
  36. effect load the original value. */
  37. #if !DONE && defined(atomic_compare_exchange_n)
  38. UTYPE
  39. SIZE(libat_load) (UTYPE *mptr, int smodel)
  40. {
  41. UTYPE t = 0;
  42. if (maybe_specialcase_relaxed(smodel))
  43. atomic_compare_exchange_n (mptr, &t, 0, true,
  44. __ATOMIC_RELAXED, __ATOMIC_RELAXED);
  45. else if (maybe_specialcase_acqrel(smodel))
  46. atomic_compare_exchange_n (mptr, &t, 0, true,
  47. __ATOMIC_ACQ_REL, __ATOMIC_ACQ_REL);
  48. else
  49. atomic_compare_exchange_n (mptr, &t, 0, true,
  50. __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
  51. return t;
  52. }
  53. #define DONE 1
  54. #endif /* atomic_compare_exchange_n */
  55. /* Similar, but only assume a word-sized compare-and-swap. */
  56. #if !DONE && N < WORDSIZE && defined(atomic_compare_exchange_w)
  57. UTYPE
  58. SIZE(libat_load) (UTYPE *mptr, int smodel)
  59. {
  60. UWORD shift, t, *wptr;
  61. pre_barrier (smodel);
  62. wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
  63. shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
  64. /* Exchange 0 with 0, placing the old value of *WPTR in T. */
  65. t = 0;
  66. atomic_compare_exchange_w (wptr, &t, 0);
  67. post_barrier (smodel);
  68. return t >> shift;
  69. }
  70. #define DONE 1
  71. #endif /* HAVE_ATOMIC_CAS && N < WORDSIZE */
  72. /* Otherwise, fall back to some sort of protection mechanism. */
  73. #if !DONE
  74. UTYPE
  75. SIZE(libat_load) (UTYPE *mptr, int smodel)
  76. {
  77. UTYPE ret;
  78. UWORD magic;
  79. pre_seq_barrier (smodel);
  80. magic = protect_start (mptr);
  81. ret = *mptr;
  82. protect_end (mptr, magic);
  83. post_seq_barrier (smodel);
  84. return ret;
  85. }
  86. #endif
  87. EXPORT_ALIAS (SIZE(load));