annotate libatomic/fop_n.c @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1 /* Copyright (C) 2012-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
2 Contributed by Richard Henderson <rth@redhat.com>.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of the GNU Atomic Library (libatomic).
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 Libatomic is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
7 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
kono
parents:
diff changeset
9 (at your option) any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
kono
parents:
diff changeset
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
kono
parents:
diff changeset
14 more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 #include <libatomic_i.h>
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 /* This file is included multiple times with required defines:
kono
parents:
diff changeset
29 NAME the name of the operation that we're implementing;
kono
parents:
diff changeset
30 OP a two-operand functional macro the implements the operation.
kono
parents:
diff changeset
31 */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 /* If we support the builtin, just use it. */
kono
parents:
diff changeset
35 #if !DONE && SIZE(HAVE_ATOMIC_FETCH_OP)
kono
parents:
diff changeset
36 UTYPE
kono
parents:
diff changeset
37 SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
kono
parents:
diff changeset
38 {
kono
parents:
diff changeset
39 if (maybe_specialcase_relaxed(smodel))
kono
parents:
diff changeset
40 return C2(__atomic_fetch_,NAME) (mptr, opval, __ATOMIC_RELAXED);
kono
parents:
diff changeset
41 else if (maybe_specialcase_acqrel(smodel))
kono
parents:
diff changeset
42 return C2(__atomic_fetch_,NAME) (mptr, opval, __ATOMIC_ACQ_REL);
kono
parents:
diff changeset
43 else
kono
parents:
diff changeset
44 return C2(__atomic_fetch_,NAME) (mptr, opval, __ATOMIC_SEQ_CST);
kono
parents:
diff changeset
45 }
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 UTYPE
kono
parents:
diff changeset
48 SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel)
kono
parents:
diff changeset
49 {
kono
parents:
diff changeset
50 if (maybe_specialcase_relaxed(smodel))
kono
parents:
diff changeset
51 return C3(__atomic_,NAME,_fetch) (mptr, opval, __ATOMIC_RELAXED);
kono
parents:
diff changeset
52 else if (maybe_specialcase_acqrel(smodel))
kono
parents:
diff changeset
53 return C3(__atomic_,NAME,_fetch) (mptr, opval, __ATOMIC_ACQ_REL);
kono
parents:
diff changeset
54 else
kono
parents:
diff changeset
55 return C3(__atomic_,NAME,_fetch) (mptr, opval, __ATOMIC_SEQ_CST);
kono
parents:
diff changeset
56 }
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 #define DONE 1
kono
parents:
diff changeset
59 #endif /* HAVE_ATOMIC_FETCH_OP */
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 #if !DONE && defined(atomic_compare_exchange_n)
kono
parents:
diff changeset
63 UTYPE
kono
parents:
diff changeset
64 SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
kono
parents:
diff changeset
65 {
kono
parents:
diff changeset
66 UTYPE oldval, t;
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 pre_barrier (smodel);
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 oldval = *mptr;
kono
parents:
diff changeset
71 do
kono
parents:
diff changeset
72 {
kono
parents:
diff changeset
73 t = OP(oldval, opval);
kono
parents:
diff changeset
74 }
kono
parents:
diff changeset
75 while (!atomic_compare_exchange_n (mptr, &oldval, t, true,
kono
parents:
diff changeset
76 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 post_barrier (smodel);
kono
parents:
diff changeset
79 return oldval;
kono
parents:
diff changeset
80 }
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 UTYPE
kono
parents:
diff changeset
83 SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel)
kono
parents:
diff changeset
84 {
kono
parents:
diff changeset
85 UTYPE oldval, t;
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 pre_barrier (smodel);
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 oldval = *mptr;
kono
parents:
diff changeset
90 do
kono
parents:
diff changeset
91 {
kono
parents:
diff changeset
92 t = OP(oldval, opval);
kono
parents:
diff changeset
93 }
kono
parents:
diff changeset
94 while (!atomic_compare_exchange_n (mptr, &oldval, t, true,
kono
parents:
diff changeset
95 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 post_barrier (smodel);
kono
parents:
diff changeset
98 return t;
kono
parents:
diff changeset
99 }
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 #define DONE 1
kono
parents:
diff changeset
102 #endif /* atomic_compare_exchange_n */
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 /* If this type is no larger than word-sized, fall back to a word-sized
kono
parents:
diff changeset
106 compare-and-swap loop. */
kono
parents:
diff changeset
107 #if !DONE && N < WORDSIZE && defined(atomic_compare_exchange_w)
kono
parents:
diff changeset
108 UTYPE
kono
parents:
diff changeset
109 SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel)
kono
parents:
diff changeset
110 {
kono
parents:
diff changeset
111 UWORD mask, shift, woldval, wopval, t, *wptr;
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 pre_barrier (smodel);
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
kono
parents:
diff changeset
116 shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
kono
parents:
diff changeset
117 mask = SIZE(MASK) << shift;
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 wopval = (UWORD)opval << shift;
kono
parents:
diff changeset
120 woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
kono
parents:
diff changeset
121 do
kono
parents:
diff changeset
122 {
kono
parents:
diff changeset
123 t = (woldval & ~mask) | (OP(woldval, wopval) & mask);
kono
parents:
diff changeset
124 }
kono
parents:
diff changeset
125 while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
kono
parents:
diff changeset
126 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 post_barrier (smodel);
kono
parents:
diff changeset
129 return woldval >> shift;
kono
parents:
diff changeset
130 }
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 UTYPE
kono
parents:
diff changeset
133 SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel)
kono
parents:
diff changeset
134 {
kono
parents:
diff changeset
135 UWORD mask, shift, woldval, wopval, t, *wptr;
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 pre_barrier (smodel);
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
kono
parents:
diff changeset
140 shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
kono
parents:
diff changeset
141 mask = SIZE(MASK) << shift;
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 wopval = (UWORD)opval << shift;
kono
parents:
diff changeset
144 woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
kono
parents:
diff changeset
145 do
kono
parents:
diff changeset
146 {
kono
parents:
diff changeset
147 t = (woldval & ~mask) | (OP(woldval, wopval) & mask);
kono
parents:
diff changeset
148 }
kono
parents:
diff changeset
149 while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
kono
parents:
diff changeset
150 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 post_barrier (smodel);
kono
parents:
diff changeset
153 return t >> shift;
kono
parents:
diff changeset
154 }
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 #define DONE 1
kono
parents:
diff changeset
157 #endif /* atomic_compare_exchange_w */
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 /* Otherwise, fall back to some sort of protection mechanism. */
kono
parents:
diff changeset
161 #if !DONE
kono
parents:
diff changeset
162 UTYPE
kono
parents:
diff changeset
163 SIZE(C2(libat_fetch_,NAME)) (UTYPE *mptr, UTYPE opval, int smodel UNUSED)
kono
parents:
diff changeset
164 {
kono
parents:
diff changeset
165 UTYPE ret;
kono
parents:
diff changeset
166 UWORD magic;
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 pre_seq_barrier (smodel);
kono
parents:
diff changeset
169 magic = protect_start (mptr);
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 ret = *mptr;
kono
parents:
diff changeset
172 *mptr = OP(ret, opval);
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 protect_end (mptr, magic);
kono
parents:
diff changeset
175 post_seq_barrier (smodel);
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 return ret;
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 UTYPE
kono
parents:
diff changeset
181 SIZE(C3(libat_,NAME,_fetch)) (UTYPE *mptr, UTYPE opval, int smodel UNUSED)
kono
parents:
diff changeset
182 {
kono
parents:
diff changeset
183 UTYPE ret;
kono
parents:
diff changeset
184 UWORD magic;
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 pre_seq_barrier (smodel);
kono
parents:
diff changeset
187 magic = protect_start (mptr);
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 ret = OP (*mptr, opval);
kono
parents:
diff changeset
190 *mptr = ret;
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 protect_end (mptr, magic);
kono
parents:
diff changeset
193 post_seq_barrier (smodel);
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 return ret;
kono
parents:
diff changeset
196 }
kono
parents:
diff changeset
197 #endif
kono
parents:
diff changeset
198
kono
parents:
diff changeset
199 EXPORT_ALIAS (SIZE(C2(fetch_,NAME)));
kono
parents:
diff changeset
200 EXPORT_ALIAS (SIZE(C2(NAME,_fetch)));