]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/hppa/atomic-machine.h
bf61b66b70532998c379750ac409835e3d241e0f
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / hppa / atomic-machine.h
1 /* Copyright (C) 2003-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Carlos O'Donell <carlos@baldric.uwo.ca>, 2005.
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 <https://www.gnu.org/licenses/>. */
18
19 #include <stdint.h> /* Required for type definitions e.g. uint8_t. */
20
21 #ifndef _ATOMIC_MACHINE_H
22 #define _ATOMIC_MACHINE_H 1
23
24 typedef int8_t atomic8_t;
25 typedef uint8_t uatomic8_t;
26 typedef int_fast8_t atomic_fast8_t;
27 typedef uint_fast8_t uatomic_fast8_t;
28
29 typedef int32_t atomic32_t;
30 typedef uint32_t uatomic32_t;
31 typedef int_fast32_t atomic_fast32_t;
32 typedef uint_fast32_t uatomic_fast32_t;
33
34 typedef intptr_t atomicptr_t;
35 typedef uintptr_t uatomicptr_t;
36 typedef intmax_t atomic_max_t;
37 typedef uintmax_t uatomic_max_t;
38
39 #define atomic_full_barrier() __sync_synchronize ()
40
41 #define __HAVE_64B_ATOMICS 0
42 #define USE_ATOMIC_COMPILER_BUILTINS 0
43
44 /* We use the compiler atomic load and store builtins as the generic
45 defines are not atomic. In particular, we need to use compare and
46 exchange for stores as the implementation is synthesized. */
47 void __atomic_link_error (void);
48 #define __atomic_check_size_ls(mem) \
49 if ((sizeof (*mem) != 1) && (sizeof (*mem) != 2) && sizeof (*mem) != 4) \
50 __atomic_link_error ();
51
52 #define atomic_load_relaxed(mem) \
53 ({ __atomic_check_size_ls((mem)); \
54 __atomic_load_n ((mem), __ATOMIC_RELAXED); })
55 #define atomic_load_acquire(mem) \
56 ({ __atomic_check_size_ls((mem)); \
57 __atomic_load_n ((mem), __ATOMIC_ACQUIRE); })
58
59 #define atomic_store_relaxed(mem, val) \
60 do { \
61 __atomic_check_size_ls((mem)); \
62 __atomic_store_n ((mem), (val), __ATOMIC_RELAXED); \
63 } while (0)
64 #define atomic_store_release(mem, val) \
65 do { \
66 __atomic_check_size_ls((mem)); \
67 __atomic_store_n ((mem), (val), __ATOMIC_RELEASE); \
68 } while (0)
69
70 /* XXX Is this actually correct? */
71 #define ATOMIC_EXCHANGE_USES_CAS 1
72
73 /* prev = *addr;
74 if (prev == old)
75 *addr = new;
76 return prev; */
77
78 /* Use the kernel atomic light weight syscalls on hppa. */
79 #define _LWS "0xb0"
80 #define _LWS_CAS "0"
81 /* Note r31 is the link register. */
82 #define _LWS_CLOBBER "r1", "r23", "r22", "r20", "r31", "memory"
83 /* String constant for -EAGAIN. */
84 #define _ASM_EAGAIN "-11"
85 /* String constant for -EDEADLOCK. */
86 #define _ASM_EDEADLOCK "-45"
87
88 /* The only basic operation needed is compare and exchange. The mem
89 pointer must be word aligned. We no longer loop on deadlock. */
90 #define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
91 ({ \
92 register long lws_errno asm("r21"); \
93 register unsigned long lws_ret asm("r28"); \
94 register unsigned long lws_mem asm("r26") = (unsigned long)(mem); \
95 register unsigned long lws_old asm("r25") = (unsigned long)(oldval);\
96 register unsigned long lws_new asm("r24") = (unsigned long)(newval);\
97 __asm__ __volatile__( \
98 "0: \n\t" \
99 "ble " _LWS "(%%sr2, %%r0) \n\t" \
100 "ldi " _LWS_CAS ", %%r20 \n\t" \
101 "cmpiclr,<> " _ASM_EAGAIN ", %%r21, %%r0\n\t" \
102 "b,n 0b \n\t" \
103 "cmpclr,= %%r0, %%r21, %%r0 \n\t" \
104 "iitlbp %%r0,(%%sr0, %%r0) \n\t" \
105 : "=r" (lws_ret), "=r" (lws_errno) \
106 : "r" (lws_mem), "r" (lws_old), "r" (lws_new) \
107 : _LWS_CLOBBER \
108 ); \
109 \
110 (__typeof (oldval)) lws_ret; \
111 })
112
113 #define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
114 ({ \
115 __typeof__ (*mem) ret; \
116 ret = atomic_compare_and_exchange_val_acq(mem, newval, oldval); \
117 /* Return 1 if it was already acquired. */ \
118 (ret != oldval); \
119 })
120
121 #endif
122 /* _ATOMIC_MACHINE_H */