annotate libatomic/load_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 /* If we support the builtin, just use it. */
kono
parents:
diff changeset
29 #if !DONE && SIZE(HAVE_ATOMIC_LDST)
kono
parents:
diff changeset
30 UTYPE
kono
parents:
diff changeset
31 SIZE(libat_load) (UTYPE *mptr, int smodel)
kono
parents:
diff changeset
32 {
kono
parents:
diff changeset
33 if (maybe_specialcase_relaxed(smodel))
kono
parents:
diff changeset
34 return __atomic_load_n (mptr, __ATOMIC_RELAXED);
kono
parents:
diff changeset
35 else if (maybe_specialcase_acqrel(smodel))
kono
parents:
diff changeset
36 /* Note that REL and ACQ_REL are not valid for loads. */
kono
parents:
diff changeset
37 return __atomic_load_n (mptr, __ATOMIC_ACQUIRE);
kono
parents:
diff changeset
38 else
kono
parents:
diff changeset
39 return __atomic_load_n (mptr, __ATOMIC_SEQ_CST);
kono
parents:
diff changeset
40 }
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 #define DONE 1
kono
parents:
diff changeset
43 #endif /* HAVE_ATOMIC_LOAD */
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 /* If we have compare-and-swap, use it to swap 0 with 0 and as a side
kono
parents:
diff changeset
47 effect load the original value. */
kono
parents:
diff changeset
48 #if !DONE && defined(atomic_compare_exchange_n)
kono
parents:
diff changeset
49 UTYPE
kono
parents:
diff changeset
50 SIZE(libat_load) (UTYPE *mptr, int smodel)
kono
parents:
diff changeset
51 {
kono
parents:
diff changeset
52 UTYPE t = 0;
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 if (maybe_specialcase_relaxed(smodel))
kono
parents:
diff changeset
55 atomic_compare_exchange_n (mptr, &t, 0, true,
kono
parents:
diff changeset
56 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
kono
parents:
diff changeset
57 else if (maybe_specialcase_acqrel(smodel))
kono
parents:
diff changeset
58 atomic_compare_exchange_n (mptr, &t, 0, true,
kono
parents:
diff changeset
59 __ATOMIC_ACQ_REL, __ATOMIC_ACQ_REL);
kono
parents:
diff changeset
60 else
kono
parents:
diff changeset
61 atomic_compare_exchange_n (mptr, &t, 0, true,
kono
parents:
diff changeset
62 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 return t;
kono
parents:
diff changeset
65 }
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 #define DONE 1
kono
parents:
diff changeset
68 #endif /* atomic_compare_exchange_n */
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 /* Similar, but only assume a word-sized compare-and-swap. */
kono
parents:
diff changeset
72 #if !DONE && N < WORDSIZE && defined(atomic_compare_exchange_w)
kono
parents:
diff changeset
73 UTYPE
kono
parents:
diff changeset
74 SIZE(libat_load) (UTYPE *mptr, int smodel)
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 UWORD shift, t, *wptr;
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 pre_barrier (smodel);
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
kono
parents:
diff changeset
81 shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 /* Exchange 0 with 0, placing the old value of *WPTR in T. */
kono
parents:
diff changeset
84 t = 0;
kono
parents:
diff changeset
85 atomic_compare_exchange_w (wptr, &t, 0);
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 post_barrier (smodel);
kono
parents:
diff changeset
88 return t >> shift;
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 #define DONE 1
kono
parents:
diff changeset
92 #endif /* HAVE_ATOMIC_CAS && N < WORDSIZE */
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 /* Otherwise, fall back to some sort of protection mechanism. */
kono
parents:
diff changeset
96 #if !DONE
kono
parents:
diff changeset
97 UTYPE
kono
parents:
diff changeset
98 SIZE(libat_load) (UTYPE *mptr, int smodel)
kono
parents:
diff changeset
99 {
kono
parents:
diff changeset
100 UTYPE ret;
kono
parents:
diff changeset
101 UWORD magic;
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 pre_seq_barrier (smodel);
kono
parents:
diff changeset
104 magic = protect_start (mptr);
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 ret = *mptr;
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 protect_end (mptr, magic);
kono
parents:
diff changeset
109 post_seq_barrier (smodel);
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 return ret;
kono
parents:
diff changeset
112 }
kono
parents:
diff changeset
113 #endif
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 EXPORT_ALIAS (SIZE(load));