annotate libatomic/gstore.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 /* If we natively support the store, and if we're unconcerned with extra
kono
parents:
diff changeset
29 barriers (e.g. fully in-order cpu for which barriers are a nop), then
kono
parents:
diff changeset
30 go ahead and expand the operation inline. */
kono
parents:
diff changeset
31 #if !defined(WANT_SPECIALCASE_RELAXED) && !defined(__OPTIMIZE_SIZE__)
kono
parents:
diff changeset
32 # define EXACT_INLINE(N) \
kono
parents:
diff changeset
33 if (C2(HAVE_ATOMIC_LDST_,N)) \
kono
parents:
diff changeset
34 { \
kono
parents:
diff changeset
35 __atomic_store_n (PTR(N,mptr), *PTR(N,vptr), __ATOMIC_SEQ_CST); \
kono
parents:
diff changeset
36 return; \
kono
parents:
diff changeset
37 }
kono
parents:
diff changeset
38 #else
kono
parents:
diff changeset
39 # define EXACT_INLINE(N)
kono
parents:
diff changeset
40 #endif
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 #define EXACT(N) \
kono
parents:
diff changeset
44 do { \
kono
parents:
diff changeset
45 if (!C2(HAVE_INT,N)) break; \
kono
parents:
diff changeset
46 if ((uintptr_t)mptr & (N - 1)) break; \
kono
parents:
diff changeset
47 EXACT_INLINE (N); \
kono
parents:
diff changeset
48 C3(local_,store_,N) (PTR(N,mptr), *PTR(N,vptr), smodel); \
kono
parents:
diff changeset
49 return; \
kono
parents:
diff changeset
50 } while (0)
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 #define LARGER(N) \
kono
parents:
diff changeset
54 do { \
kono
parents:
diff changeset
55 union max_size_u u, v; \
kono
parents:
diff changeset
56 uintptr_t r, a; \
kono
parents:
diff changeset
57 if (!C2(HAVE_INT,N)) break; \
kono
parents:
diff changeset
58 if (!C2(MAYBE_HAVE_ATOMIC_CAS_,N)) break; \
kono
parents:
diff changeset
59 r = (uintptr_t)mptr & (N - 1); \
kono
parents:
diff changeset
60 a = (uintptr_t)mptr & -N; \
kono
parents:
diff changeset
61 if (r + n <= N) \
kono
parents:
diff changeset
62 { \
kono
parents:
diff changeset
63 pre_barrier (smodel); \
kono
parents:
diff changeset
64 /* This load need not be atomic, as the CAS \
kono
parents:
diff changeset
65 below will validate it. */ \
kono
parents:
diff changeset
66 u.C2(i,N) = *PTR(N,a); \
kono
parents:
diff changeset
67 do { \
kono
parents:
diff changeset
68 v = u; memcpy (v.b + r, vptr, n); \
kono
parents:
diff changeset
69 } while (!(C2(HAVE_ATOMIC_CAS_,N) \
kono
parents:
diff changeset
70 ? __atomic_compare_exchange_n (PTR(N,a), \
kono
parents:
diff changeset
71 &u.C2(i,N), v.C2(i,N), true, \
kono
parents:
diff changeset
72 __ATOMIC_RELAXED, __ATOMIC_RELAXED) \
kono
parents:
diff changeset
73 : C3(local_,compare_exchange_,N) (PTR(N,a), \
kono
parents:
diff changeset
74 &u.C2(i,N), v.C2(i,N), \
kono
parents:
diff changeset
75 __ATOMIC_RELAXED, __ATOMIC_RELAXED))); \
kono
parents:
diff changeset
76 post_barrier (smodel); \
kono
parents:
diff changeset
77 return; \
kono
parents:
diff changeset
78 } \
kono
parents:
diff changeset
79 } while (0)
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 void
kono
parents:
diff changeset
83 libat_store (size_t n, void *mptr, void *vptr, int smodel)
kono
parents:
diff changeset
84 {
kono
parents:
diff changeset
85 switch (n)
kono
parents:
diff changeset
86 {
kono
parents:
diff changeset
87 case 0: return;
kono
parents:
diff changeset
88 case 1: EXACT(1); goto L4;
kono
parents:
diff changeset
89 case 2: EXACT(2); goto L4;
kono
parents:
diff changeset
90 case 4: EXACT(4); goto L8;
kono
parents:
diff changeset
91 case 8: EXACT(8); goto L16;
kono
parents:
diff changeset
92 case 16: EXACT(16); break;
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 case 3: L4: LARGER(4); /* FALLTHRU */
kono
parents:
diff changeset
95 case 5 ... 7: L8: LARGER(8); /* FALLTHRU */
kono
parents:
diff changeset
96 case 9 ... 15: L16: LARGER(16); break;
kono
parents:
diff changeset
97 }
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 pre_seq_barrier (smodel);
kono
parents:
diff changeset
100 libat_lock_n (mptr, n);
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 memcpy (mptr, vptr, n);
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 libat_unlock_n (mptr, n);
kono
parents:
diff changeset
105 post_seq_barrier (smodel);
kono
parents:
diff changeset
106 }
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 EXPORT_ALIAS (store);