131
|
1 /* Copyright (C) 2012-2018 Free Software Foundation, Inc.
|
111
|
2 Contributed by Richard Henderson <rth@redhat.com>.
|
|
3
|
|
4 This file is part of the GNU Atomic Library (libatomic).
|
|
5
|
|
6 Libatomic is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 3 of the License, or
|
|
9 (at your option) any later version.
|
|
10
|
|
11 Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
14 more details.
|
|
15
|
|
16 Under Section 7 of GPL version 3, you are granted additional
|
|
17 permissions described in the GCC Runtime Library Exception, version
|
|
18 3.1, as published by the Free Software Foundation.
|
|
19
|
|
20 You should have received a copy of the GNU General Public License and
|
|
21 a copy of the GCC Runtime Library Exception along with this program;
|
|
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
23 <http://www.gnu.org/licenses/>. */
|
|
24
|
|
25 #include "libatomic_i.h"
|
|
26
|
|
27
|
|
28 /* If we support the builtin, just use it. */
|
|
29 #if !DONE && SIZE(HAVE_ATOMIC_LDST)
|
|
30 void
|
|
31 SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
|
|
32 {
|
|
33 if (maybe_specialcase_relaxed(smodel))
|
|
34 __atomic_store_n (mptr, newval, __ATOMIC_RELAXED);
|
|
35 else if (maybe_specialcase_acqrel(smodel))
|
|
36 /* Note that ACQ and ACQ_REL are not valid for store. */
|
|
37 __atomic_store_n (mptr, newval, __ATOMIC_RELEASE);
|
|
38 else
|
|
39 __atomic_store_n (mptr, newval, __ATOMIC_SEQ_CST);
|
|
40 }
|
|
41
|
|
42 #define DONE 1
|
|
43 #endif /* HAVE_ATOMIC_STORE */
|
|
44
|
|
45 /* If we have compare-and-swap, use it perform the store. */
|
|
46 #if !DONE && defined(atomic_compare_exchange_n)
|
|
47 void
|
|
48 SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
|
|
49 {
|
|
50 UTYPE oldval;
|
|
51
|
|
52 pre_barrier (smodel);
|
|
53
|
|
54 oldval = *mptr;
|
|
55 while (!atomic_compare_exchange_n (mptr, &oldval, newval, true,
|
|
56 __ATOMIC_RELAXED, __ATOMIC_RELAXED))
|
|
57 continue;
|
|
58
|
|
59 post_barrier (smodel);
|
|
60 }
|
|
61
|
|
62 #define DONE 1
|
|
63 #endif /* atomic_compare_exchange_n */
|
|
64
|
|
65
|
|
66 /* If this type is smaller than word-sized, fall back to a word-sized
|
|
67 compare-and-swap loop. */
|
|
68 #if !DONE && N < WORDSIZE && defined(atomic_compare_exchange_w)
|
|
69 void
|
|
70 SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
|
|
71 {
|
|
72 UWORD mask, shift, woldval, wnewval, t, *wptr;
|
|
73
|
|
74 pre_barrier (smodel);
|
|
75
|
|
76 wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
|
|
77 shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
|
|
78 mask = SIZE(MASK) << shift;
|
|
79
|
|
80 wnewval = (UWORD)newval << shift;
|
|
81 woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
|
|
82 do
|
|
83 {
|
|
84 t = (woldval & ~mask) | wnewval;
|
|
85 }
|
|
86 while (!atomic_compare_exchange_w (wptr, &woldval, t));
|
|
87
|
|
88 post_barrier (smodel);
|
|
89 }
|
|
90
|
|
91 #define DONE 1
|
|
92 #endif /* N < WORDSIZE && atomic_compare_exchange_w */
|
|
93
|
|
94
|
|
95 /* Otherwise, fall back to some sort of protection mechanism. */
|
|
96 #if !DONE
|
|
97 void
|
|
98 SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
|
|
99 {
|
|
100 UWORD magic;
|
|
101
|
|
102 pre_seq_barrier (smodel);
|
|
103 magic = protect_start (mptr);
|
|
104
|
|
105 *mptr = newval;
|
|
106
|
|
107 protect_end (mptr, magic);
|
|
108 post_seq_barrier (smodel);
|
|
109 }
|
|
110 #endif
|
|
111
|
|
112 EXPORT_ALIAS (SIZE(store));
|