]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/s390/atomic-machine.h
4932edb45dfdef139a887e09f8d720fd4c81d99d
[thirdparty/glibc.git] / sysdeps / s390 / atomic-machine.h
1 /* Copyright (C) 2003-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <stdint.h>
20
21 typedef int8_t atomic8_t;
22 typedef uint8_t uatomic8_t;
23 typedef int_fast8_t atomic_fast8_t;
24 typedef uint_fast8_t uatomic_fast8_t;
25
26 typedef int16_t atomic16_t;
27 typedef uint16_t uatomic16_t;
28 typedef int_fast16_t atomic_fast16_t;
29 typedef uint_fast16_t uatomic_fast16_t;
30
31 typedef int32_t atomic32_t;
32 typedef uint32_t uatomic32_t;
33 typedef int_fast32_t atomic_fast32_t;
34 typedef uint_fast32_t uatomic_fast32_t;
35
36 typedef int64_t atomic64_t;
37 typedef uint64_t uatomic64_t;
38 typedef int_fast64_t atomic_fast64_t;
39 typedef uint_fast64_t uatomic_fast64_t;
40
41 typedef intptr_t atomicptr_t;
42 typedef uintptr_t uatomicptr_t;
43 typedef intmax_t atomic_max_t;
44 typedef uintmax_t uatomic_max_t;
45
46 /* Activate all C11 atomic builtins.
47
48 Note:
49 E.g. in nptl/pthread_key_delete.c if compiled with GCCs 6 and before,
50 an extra stack-frame is generated and the old value is stored on stack
51 before cs instruction but it never loads this value from stack.
52 An unreleased GCC 7 omit those stack operations.
53
54 E.g. in nptl/pthread_once.c the condition code of cs instruction is
55 evaluated by a sequence of ipm, sra, compare and jump instructions instead
56 of one conditional jump instruction. This also occurs with an unreleased
57 GCC 7.
58
59 The atomic_fetch_abc_def C11 builtins are now using load-and-abc instructions
60 on z196 zarch and higher cpus instead of a loop with compare-and-swap
61 instruction. */
62 #define USE_ATOMIC_COMPILER_BUILTINS 1
63
64 #ifdef __s390x__
65 # define __HAVE_64B_ATOMICS 1
66 #else
67 # define __HAVE_64B_ATOMICS 0
68 #endif
69
70 #define ATOMIC_EXCHANGE_USES_CAS 1
71
72 /* Implement some of the non-C11 atomic macros from include/atomic.h
73 with help of the C11 atomic builtins. The other non-C11 atomic macros
74 are using the macros defined here. */
75
76 /* Atomically store NEWVAL in *MEM if *MEM is equal to OLDVAL.
77 Return the old *MEM value. */
78 #define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
79 ({ __atomic_check_size((mem)); \
80 typeof ((__typeof (*(mem))) *(mem)) __atg1_oldval = (oldval); \
81 __atomic_compare_exchange_n (mem, (void *) &__atg1_oldval, \
82 newval, 1, __ATOMIC_ACQUIRE, \
83 __ATOMIC_RELAXED); \
84 __atg1_oldval; })
85 #define atomic_compare_and_exchange_val_rel(mem, newval, oldval) \
86 ({ __atomic_check_size((mem)); \
87 typeof ((__typeof (*(mem))) *(mem)) __atg1_2_oldval = (oldval); \
88 __atomic_compare_exchange_n (mem, (void *) &__atg1_2_oldval, \
89 newval, 1, __ATOMIC_RELEASE, \
90 __ATOMIC_RELAXED); \
91 __atg1_2_oldval; })
92
93 /* Atomically store NEWVAL in *MEM if *MEM is equal to OLDVAL.
94 Return zero if *MEM was changed or non-zero if no exchange happened. */
95 #define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
96 ({ __atomic_check_size((mem)); \
97 typeof ((__typeof (*(mem))) *(mem)) __atg2_oldval = (oldval); \
98 !__atomic_compare_exchange_n (mem, (void *) &__atg2_oldval, newval, \
99 1, __ATOMIC_ACQUIRE, \
100 __ATOMIC_RELAXED); })
101 #define catomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
102 atomic_compare_and_exchange_bool_acq (mem, newval, oldval)
103
104 /* Store NEWVALUE in *MEM and return the old value. */
105 #define atomic_exchange_acq(mem, newvalue) \
106 ({ __atomic_check_size((mem)); \
107 __atomic_exchange_n (mem, newvalue, __ATOMIC_ACQUIRE); })
108 #define atomic_exchange_rel(mem, newvalue) \
109 ({ __atomic_check_size((mem)); \
110 __atomic_exchange_n (mem, newvalue, __ATOMIC_RELEASE); })
111
112 /* Add VALUE to *MEM and return the old value of *MEM. */
113 /* The gcc builtin uses load-and-add instruction on z196 zarch and higher cpus
114 instead of a loop with compare-and-swap instruction. */
115 # define atomic_exchange_and_add_acq(mem, operand) \
116 ({ __atomic_check_size((mem)); \
117 __atomic_fetch_add ((mem), (operand), __ATOMIC_ACQUIRE); })
118 # define atomic_exchange_and_add_rel(mem, operand) \
119 ({ __atomic_check_size((mem)); \
120 __atomic_fetch_add ((mem), (operand), __ATOMIC_RELEASE); })
121 #define catomic_exchange_and_add(mem, value) \
122 atomic_exchange_and_add (mem, value)
123
124 /* Atomically *mem |= mask and return the old value of *mem. */
125 /* The gcc builtin uses load-and-or instruction on z196 zarch and higher cpus
126 instead of a loop with compare-and-swap instruction. */
127 #define atomic_or_val(mem, operand) \
128 ({ __atomic_check_size((mem)); \
129 __atomic_fetch_or ((mem), (operand), __ATOMIC_ACQUIRE); })
130 /* Atomically *mem |= mask. */
131 #define atomic_or(mem, mask) \
132 do { \
133 atomic_or_val (mem, mask); \
134 } while (0)
135 #define catomic_or(mem, mask) \
136 atomic_or (mem, mask)
137
138 /* Atomically *mem |= 1 << bit and return true if the bit was set in old value
139 of *mem. */
140 /* The load-and-or instruction is used on z196 zarch and higher cpus
141 instead of a loop with compare-and-swap instruction. */
142 #define atomic_bit_test_set(mem, bit) \
143 ({ __typeof (*(mem)) __atg14_old; \
144 __typeof (mem) __atg14_memp = (mem); \
145 __typeof (*(mem)) __atg14_mask = ((__typeof (*(mem))) 1 << (bit)); \
146 __atg14_old = atomic_or_val (__atg14_memp, __atg14_mask); \
147 __atg14_old & __atg14_mask; })
148
149 /* Atomically *mem &= mask and return the old value of *mem. */
150 /* The gcc builtin uses load-and-and instruction on z196 zarch and higher cpus
151 instead of a loop with compare-and-swap instruction. */
152 #define atomic_and_val(mem, operand) \
153 ({ __atomic_check_size((mem)); \
154 __atomic_fetch_and ((mem), (operand), __ATOMIC_ACQUIRE); })
155 /* Atomically *mem &= mask. */
156 #define atomic_and(mem, mask) \
157 do { \
158 atomic_and_val (mem, mask); \
159 } while (0)
160 #define catomic_and(mem, mask) \
161 atomic_and(mem, mask)