Re-apply: Drop excess size used for run time allocated stack variables.
[official-gcc.git] / libatomic / exch_n.c
blobba0c7af93329accd443c29ecff6f499bc631e1e4
1 /* Copyright (C) 2012-2016 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Atomic Library (libatomic).
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.
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.
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.
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/>. */
25 #include "libatomic_i.h"
28 /* If we support the builtin, just use it. */
29 #if !DONE && SIZE(HAVE_ATOMIC_EXCHANGE)
30 UTYPE
31 SIZE(libat_exchange) (UTYPE *mptr, UTYPE newval, int smodel)
33 if (maybe_specialcase_relaxed(smodel))
34 return __atomic_exchange_n (mptr, newval, __ATOMIC_RELAXED);
35 else if (maybe_specialcase_acqrel(smodel))
36 return __atomic_exchange_n (mptr, newval, __ATOMIC_ACQ_REL);
37 else
38 return __atomic_exchange_n (mptr, newval, __ATOMIC_SEQ_CST);
41 #define DONE 1
42 #endif /* HAVE_ATOMIC_EXCHANGE */
45 #if !DONE && defined(atomic_compare_exchange_n)
46 UTYPE
47 SIZE(libat_exchange) (UTYPE *mptr, UTYPE newval, int smodel)
49 UTYPE oldval;
51 pre_barrier (smodel);
53 oldval = *mptr;
54 while (!atomic_compare_exchange_n (mptr, &oldval, newval, true,
55 __ATOMIC_RELAXED, __ATOMIC_RELAXED))
56 continue;
58 post_barrier (smodel);
60 return oldval;
63 #define DONE 1
64 #endif /* atomic_compare_exchange_n */
67 /* If this type is smaller than word-sized, fall back to a word-sized
68 compare-and-swap loop. */
69 #if !DONE && N <= WORDSIZE && defined(atomic_compare_exchange_w)
70 UTYPE
71 SIZE(libat_exchange) (UTYPE *mptr, UTYPE newval, int smodel)
73 UWORD mask, shift, woldval, wnewval, t, *wptr;
75 pre_barrier (smodel);
77 if (N < WORDSIZE)
79 wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
80 shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
81 mask = SIZE(MASK) << shift;
83 else
85 wptr = (UWORD *)mptr;
86 shift = 0;
87 mask = -1;
90 wnewval = (UWORD)newval << shift;
91 woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
94 t = (woldval & ~mask) | wnewval;
96 while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
97 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
99 post_barrier (smodel);
100 return woldval >> shift;
103 #define DONE 1
104 #endif /* HAVE_ATOMIC_CAS && N < WORDSIZE */
107 /* Otherwise, fall back to some sort of protection mechanism. */
108 #if !DONE
109 UTYPE
110 SIZE(libat_exchange) (UTYPE *mptr, UTYPE newval, int smodel UNUSED)
112 UTYPE oldval;
113 UWORD magic;
115 pre_seq_barrier (smodel);
116 magic = protect_start (mptr);
118 oldval = *mptr;
119 *mptr = newval;
121 protect_end (mptr, magic);
122 post_seq_barrier (smodel);
124 return oldval;
126 #endif
128 EXPORT_ALIAS (SIZE(exchange));