Debian Codegraph @

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