d348ed20ae2b140aa9c06831510515a8b07cf997
[platform/upstream/glibc.git] / sysdeps / sparc / sparc64 / bits / atomic.h
1 /* Atomic operations.  sparc64 version.
2    Copyright (C) 2003-2014 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <stdint.h>
21
22 typedef int8_t atomic8_t;
23 typedef uint8_t uatomic8_t;
24 typedef int_fast8_t atomic_fast8_t;
25 typedef uint_fast8_t uatomic_fast8_t;
26
27 typedef int16_t atomic16_t;
28 typedef uint16_t uatomic16_t;
29 typedef int_fast16_t atomic_fast16_t;
30 typedef uint_fast16_t uatomic_fast16_t;
31
32 typedef int32_t atomic32_t;
33 typedef uint32_t uatomic32_t;
34 typedef int_fast32_t atomic_fast32_t;
35 typedef uint_fast32_t uatomic_fast32_t;
36
37 typedef int64_t atomic64_t;
38 typedef uint64_t uatomic64_t;
39 typedef int_fast64_t atomic_fast64_t;
40 typedef uint_fast64_t uatomic_fast64_t;
41
42 typedef intptr_t atomicptr_t;
43 typedef uintptr_t uatomicptr_t;
44 typedef intmax_t atomic_max_t;
45 typedef uintmax_t uatomic_max_t;
46
47
48 #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
49   (abort (), (__typeof (*mem)) 0)
50
51 #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
52   (abort (), (__typeof (*mem)) 0)
53
54 #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
55 ({                                                                            \
56   __typeof (*(mem)) __acev_tmp;                                               \
57   __typeof (mem) __acev_mem = (mem);                                          \
58   if (__builtin_constant_p (oldval) && (oldval) == 0)                         \
59     __asm __volatile ("cas [%3], %%g0, %0"                                    \
60                       : "=r" (__acev_tmp), "=m" (*__acev_mem)                 \
61                       : "m" (*__acev_mem), "r" (__acev_mem),                  \
62                         "0" (newval) : "memory");                             \
63   else                                                                        \
64     __asm __volatile ("cas [%4], %2, %0"                                      \
65                       : "=r" (__acev_tmp), "=m" (*__acev_mem)                 \
66                       : "r" (oldval), "m" (*__acev_mem), "r" (__acev_mem),    \
67                         "0" (newval) : "memory");                             \
68   __acev_tmp; })
69
70 #define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
71 ({                                                                            \
72   __typeof (*(mem)) __acev_tmp;                                               \
73   __typeof (mem) __acev_mem = (mem);                                          \
74   if (__builtin_constant_p (oldval) && (oldval) == 0)                         \
75     __asm __volatile ("casx [%3], %%g0, %0"                                   \
76                       : "=r" (__acev_tmp), "=m" (*__acev_mem)                 \
77                       : "m" (*__acev_mem), "r" (__acev_mem),                  \
78                         "0" ((long) (newval)) : "memory");                    \
79   else                                                                        \
80     __asm __volatile ("casx [%4], %2, %0"                                     \
81                       : "=r" (__acev_tmp), "=m" (*__acev_mem)                 \
82                       : "r" ((long) (oldval)), "m" (*__acev_mem),             \
83                         "r" (__acev_mem), "0" ((long) (newval)) : "memory");  \
84   __acev_tmp; })
85
86 #define atomic_exchange_acq(mem, newvalue) \
87   ({ __typeof (*(mem)) __oldval, __val;                                       \
88      __typeof (mem) __memp = (mem);                                           \
89      __typeof (*(mem)) __value = (newvalue);                                  \
90                                                                               \
91      if (sizeof (*(mem)) == 4)                                                \
92        __asm ("swap %0, %1"                                                   \
93               : "=m" (*__memp), "=r" (__oldval)                               \
94               : "m" (*__memp), "1" (__value) : "memory");                     \
95      else                                                                     \
96        {                                                                      \
97          __val = *__memp;                                                     \
98          do                                                                   \
99            {                                                                  \
100              __oldval = __val;                                                \
101              __val = atomic_compare_and_exchange_val_acq (__memp, __value,    \
102                                                           __oldval);          \
103            }                                                                  \
104          while (__builtin_expect (__val != __oldval, 0));                     \
105        }                                                                      \
106      __oldval; })
107
108 #define atomic_compare_and_exchange_val_24_acq(mem, newval, oldval) \
109   atomic_compare_and_exchange_val_acq (mem, newval, oldval)
110
111 #define atomic_exchange_24_rel(mem, newval) \
112   atomic_exchange_rel (mem, newval)
113
114 #define atomic_full_barrier() \
115   __asm __volatile ("membar #LoadLoad | #LoadStore"                           \
116                           " | #StoreLoad | #StoreStore" : : : "memory")
117 #define atomic_read_barrier() \
118   __asm __volatile ("membar #LoadLoad | #LoadStore" : : : "memory")
119 #define atomic_write_barrier() \
120   __asm __volatile ("membar #LoadStore | #StoreStore" : : : "memory")