Debian Codegraph @

libatomic/fop_n.c

raw
1 /* Copyright (C) 2012-2025 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <[email protected]>.
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 #define LAT_FOP_N
26 #include <libatomic_i.h>
27
28
29 /* This file is included multiple times with required defines:
30 NAME the name of the operation that we're implementing;
31 OP a two-operand functional macro the implements the operation.
32 */
33
34
35 /* If we support the builtin, just use it. */
36 #if !DONE && SIZE(HAVE_ATOMIC_FETCH_OP)
37 UTYPE
38 SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
39 {
40 if (maybe_specialcase_relaxed(smodel))
41 return C2(__atomic_fetch_,NAME) (mptr, opval, __ATOMIC_RELAXED);
42 else if (maybe_specialcase_acqrel(smodel))
43 return C2(__atomic_fetch_,NAME) (mptr, opval, __ATOMIC_ACQ_REL);
44 else
45 return C2(__atomic_fetch_,NAME) (mptr, opval, __ATOMIC_SEQ_CST);
46 }
47
48 UTYPE
49 SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel)
50 {
51 if (maybe_specialcase_relaxed(smodel))
52 return C3(__atomic_,NAME,_fetch) (mptr, opval, __ATOMIC_RELAXED);
53 else if (maybe_specialcase_acqrel(smodel))
54 return C3(__atomic_,NAME,_fetch) (mptr, opval, __ATOMIC_ACQ_REL);
55 else
56 return C3(__atomic_,NAME,_fetch) (mptr, opval, __ATOMIC_SEQ_CST);
57 }
58
59 #define DONE 1
60 #endif /* HAVE_ATOMIC_FETCH_OP */
61
62
63 #if !DONE && defined(atomic_compare_exchange_n)
64 UTYPE
65 SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
66 {
67 UTYPE oldval, t;
68
69 pre_barrier (smodel);
70
71 oldval = *mptr;
72 do
73 {
74 t = OP(oldval, opval);
75 }
76 while (!atomic_compare_exchange_n (mptr, &oldval, t, true,
77 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
78
79 post_barrier (smodel);
80 return oldval;
81 }
82
83 UTYPE
84 SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel)
85 {
86 UTYPE oldval, t;
87
88 pre_barrier (smodel);
89
90 oldval = *mptr;
91 do
92 {
93 t = OP(oldval, opval);
94 }
95 while (!atomic_compare_exchange_n (mptr, &oldval, t, true,
96 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
97
98 post_barrier (smodel);
99 return t;
100 }
101
102 #define DONE 1
103 #endif /* atomic_compare_exchange_n */
104
105
106 /* If this type is no larger than word-sized, fall back to a word-sized
107 compare-and-swap loop. */
108 #if !DONE && N < WORDSIZE && defined(atomic_compare_exchange_w)
109 UTYPE
110 SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
111 {
112 UWORD mask, shift, woldval, wopval, t, *wptr;
113
114 pre_barrier (smodel);
115
116 wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
117 shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
118 mask = SIZE(MASK) << shift;
119
120 wopval = (UWORD)opval << shift;
121 woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
122 do
123 {
124 t = (woldval & ~mask) | (OP(woldval, wopval) & mask);
125 }
126 while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
127 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
128
129 post_barrier (smodel);
130 return woldval >> shift;
131 }
132
133 UTYPE
134 SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel)
135 {
136 UWORD mask, shift, woldval, wopval, t, *wptr;
137
138 pre_barrier (smodel);
139
140 wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
141 shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
142 mask = SIZE(MASK) << shift;
143
144 wopval = (UWORD)opval << shift;
145 woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
146 do
147 {
148 t = (woldval & ~mask) | (OP(woldval, wopval) & mask);
149 }
150 while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
151 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
152
153 post_barrier (smodel);
154 return t >> shift;
155 }
156
157 #define DONE 1
158 #endif /* atomic_compare_exchange_w */
159
160
161 /* Otherwise, fall back to some sort of protection mechanism. */
162 #if !DONE
163 UTYPE
164 SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel UNUSED)
165 {
166 UTYPE ret;
167 UWORD magic;
168
169 pre_seq_barrier (smodel);
170 magic = protect_start (mptr);
171
172 ret = *mptr;
173 *mptr = OP(ret, opval);
174
175 protect_end (mptr, magic);
176 post_seq_barrier (smodel);
177
178 return ret;
179 }
180
181 UTYPE
182 SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel UNUSED)
183 {
184 UTYPE ret;
185 UWORD magic;
186
187 pre_seq_barrier (smodel);
188 magic = protect_start (mptr);
189
190 ret = OP (*mptr, opval);
191 *mptr = ret;
192
193 protect_end (mptr, magic);
194 post_seq_barrier (smodel);
195
196 return ret;
197 }
198 #endif
199
200 EXPORT_ALIAS (SIZE(C2(fetch_,NAME)));
201 EXPORT_ALIAS (SIZE(C2(NAME,_fetch)));
202 #undef LAT_FOP_N