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