1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2012 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // This file is an internal atomic implementation, use atomicops.h instead.
33 // LinuxKernelCmpxchg and Barrier_AtomicIncrement are from Google Gears.
35 #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_GCC_H_
36 #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_GCC_H_
42 // 0xffff0fc0 is the hard coded address of a function provided by
43 // the kernel which implements an atomic compare-exchange. On older
44 // ARM architecture revisions (pre-v6) this may be implemented using
45 // a syscall. This address is stable, and in active use (hard coded)
46 // by at least glibc-2.7 and the Android C library.
47 typedef Atomic32 (*LinuxKernelCmpxchgFunc)(Atomic32 old_value,
49 volatile Atomic32* ptr);
50 LinuxKernelCmpxchgFunc pLinuxKernelCmpxchg __attribute__((weak)) =
51 (LinuxKernelCmpxchgFunc) 0xffff0fc0;
53 typedef void (*LinuxKernelMemoryBarrierFunc)(void);
54 LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier __attribute__((weak)) =
55 (LinuxKernelMemoryBarrierFunc) 0xffff0fa0;
58 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
61 Atomic32 prev_value = *ptr;
63 if (!pLinuxKernelCmpxchg(old_value, new_value,
64 const_cast<Atomic32*>(ptr))) {
68 } while (prev_value == old_value);
72 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
77 } while (pLinuxKernelCmpxchg(old_value, new_value,
78 const_cast<Atomic32*>(ptr)));
82 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
84 return Barrier_AtomicIncrement(ptr, increment);
87 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
90 // Atomic exchange the old value with an incremented one.
91 Atomic32 old_value = *ptr;
92 Atomic32 new_value = old_value + increment;
93 if (pLinuxKernelCmpxchg(old_value, new_value,
94 const_cast<Atomic32*>(ptr)) == 0) {
95 // The exchange took place as expected.
98 // Otherwise, *ptr changed mid-loop and we need to retry.
102 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
104 Atomic32 new_value) {
105 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
108 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
110 Atomic32 new_value) {
111 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
114 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
118 inline void MemoryBarrier() {
119 pLinuxKernelMemoryBarrier();
122 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
127 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
132 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
136 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
137 Atomic32 value = *ptr;
142 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
147 } // namespace internal
148 } // namespace protobuf
149 } // namespace google
151 #endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_GCC_H_