1 // Copyright 2014 Google Inc. All rights reserved.
2 // https://developers.google.com/protocol-buffers/
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 // This file is an internal atomic implementation, use atomicops.h instead.
31
32 #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_SPARC_GCC_H_
33 #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_SPARC_GCC_H_
34
35 #include <atomic.h>
36
37 namespace google {
38 namespace protobuf {
39 namespace internal {
40
NoBarrier_CompareAndSwap(volatile Atomic32 * ptr,Atomic32 old_value,Atomic32 new_value)41 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
42 Atomic32 old_value,
43 Atomic32 new_value) {
44 return (Atomic32)atomic_cas_32((volatile uint32_t*)ptr, (uint32_t)old_value, (uint32_t)new_value);
45 }
46
NoBarrier_AtomicExchange(volatile Atomic32 * ptr,Atomic32 new_value)47 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
48 Atomic32 new_value) {
49 return (Atomic32)atomic_swap_32((volatile uint32_t*)ptr, (uint32_t)new_value);
50 }
51
NoBarrier_AtomicIncrement(volatile Atomic32 * ptr,Atomic32 increment)52 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
53 Atomic32 increment) {
54 return (Atomic32)atomic_add_32_nv((volatile uint32_t*)ptr, (uint32_t)increment);
55 }
56
MemoryBarrier(void)57 inline void MemoryBarrier(void) {
58 membar_producer();
59 membar_consumer();
60 }
61
Barrier_AtomicIncrement(volatile Atomic32 * ptr,Atomic32 increment)62 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
63 Atomic32 increment) {
64 MemoryBarrier();
65 Atomic32 ret = NoBarrier_AtomicIncrement(ptr, increment);
66 MemoryBarrier();
67
68 return ret;
69 }
70
Acquire_CompareAndSwap(volatile Atomic32 * ptr,Atomic32 old_value,Atomic32 new_value)71 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
72 Atomic32 old_value,
73 Atomic32 new_value) {
74 Atomic32 ret = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
75 MemoryBarrier();
76
77 return ret;
78 }
79
Release_CompareAndSwap(volatile Atomic32 * ptr,Atomic32 old_value,Atomic32 new_value)80 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
81 Atomic32 old_value,
82 Atomic32 new_value) {
83 MemoryBarrier();
84 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
85 }
86
NoBarrier_Store(volatile Atomic32 * ptr,Atomic32 value)87 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
88 *ptr = value;
89 }
90
Acquire_Store(volatile Atomic32 * ptr,Atomic32 value)91 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
92 *ptr = value;
93 membar_producer();
94 }
95
Release_Store(volatile Atomic32 * ptr,Atomic32 value)96 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
97 membar_consumer();
98 *ptr = value;
99 }
100
NoBarrier_Load(volatile const Atomic32 * ptr)101 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
102 return *ptr;
103 }
104
Acquire_Load(volatile const Atomic32 * ptr)105 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
106 Atomic32 val = *ptr;
107 membar_consumer();
108 return val;
109 }
110
Release_Load(volatile const Atomic32 * ptr)111 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
112 membar_producer();
113 return *ptr;
114 }
115
116 #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
NoBarrier_CompareAndSwap(volatile Atomic64 * ptr,Atomic64 old_value,Atomic64 new_value)117 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
118 Atomic64 old_value,
119 Atomic64 new_value) {
120 return atomic_cas_64((volatile uint64_t*)ptr, (uint64_t)old_value, (uint64_t)new_value);
121 }
122
NoBarrier_AtomicExchange(volatile Atomic64 * ptr,Atomic64 new_value)123 inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value) {
124 return atomic_swap_64((volatile uint64_t*)ptr, (uint64_t)new_value);
125 }
126
NoBarrier_AtomicIncrement(volatile Atomic64 * ptr,Atomic64 increment)127 inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment) {
128 return atomic_add_64_nv((volatile uint64_t*)ptr, increment);
129 }
130
Barrier_AtomicIncrement(volatile Atomic64 * ptr,Atomic64 increment)131 inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment) {
132 MemoryBarrier();
133 Atomic64 ret = atomic_add_64_nv((volatile uint64_t*)ptr, increment);
134 MemoryBarrier();
135 return ret;
136 }
137
Acquire_CompareAndSwap(volatile Atomic64 * ptr,Atomic64 old_value,Atomic64 new_value)138 inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
139 Atomic64 old_value,
140 Atomic64 new_value) {
141 Atomic64 ret = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
142 MemoryBarrier();
143 return ret;
144 }
145
Release_CompareAndSwap(volatile Atomic64 * ptr,Atomic64 old_value,Atomic64 new_value)146 inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
147 Atomic64 old_value,
148 Atomic64 new_value) {
149 MemoryBarrier();
150 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
151 }
152
NoBarrier_Store(volatile Atomic64 * ptr,Atomic64 value)153 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) {
154 *ptr = value;
155 }
156
Acquire_Store(volatile Atomic64 * ptr,Atomic64 value)157 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) {
158 *ptr = value;
159 membar_producer();
160 }
161
Release_Store(volatile Atomic64 * ptr,Atomic64 value)162 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) {
163 membar_consumer();
164 *ptr = value;
165 }
166
NoBarrier_Load(volatile const Atomic64 * ptr)167 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) {
168 return *ptr;
169 }
170
Acquire_Load(volatile const Atomic64 * ptr)171 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) {
172 Atomic64 ret = *ptr;
173 membar_consumer();
174 return ret;
175 }
176
Release_Load(volatile const Atomic64 * ptr)177 inline Atomic64 Release_Load(volatile const Atomic64* ptr) {
178 membar_producer();
179 return *ptr;
180 }
181 #endif
182
183 } // namespace internal
184 } // namespace protobuf
185 } // namespace google
186
187 #endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_SPARC_GCC_H_
188
189