1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2012 Google Inc. All rights reserved. 3 // http://code.google.com/p/protobuf/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 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 14 // distribution. 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. 18 // 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. 30 31 // This file is an internal atomic implementation, use atomicops.h instead. 32 // 33 // LinuxKernelCmpxchg and Barrier_AtomicIncrement are from Google Gears. 34 35 #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_GCC_H_ 36 #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_GCC_H_ 37 38 namespace google { 39 namespace protobuf { 40 namespace internal { 41 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, 48 Atomic32 new_value, 49 volatile Atomic32* ptr); 50 LinuxKernelCmpxchgFunc pLinuxKernelCmpxchg __attribute__((weak)) = 51 (LinuxKernelCmpxchgFunc) 0xffff0fc0; 52 53 typedef void (*LinuxKernelMemoryBarrierFunc)(void); 54 LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier __attribute__((weak)) = 55 (LinuxKernelMemoryBarrierFunc) 0xffff0fa0; 56 57 58 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, 59 Atomic32 old_value, 60 Atomic32 new_value) { 61 Atomic32 prev_value = *ptr; 62 do { 63 if (!pLinuxKernelCmpxchg(old_value, new_value, 64 const_cast<Atomic32*>(ptr))) { 65 return old_value; 66 } 67 prev_value = *ptr; 68 } while (prev_value == old_value); 69 return prev_value; 70 } 71 72 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, 73 Atomic32 new_value) { 74 Atomic32 old_value; 75 do { 76 old_value = *ptr; 77 } while (pLinuxKernelCmpxchg(old_value, new_value, 78 const_cast<Atomic32*>(ptr))); 79 return old_value; 80 } 81 82 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, 83 Atomic32 increment) { 84 return Barrier_AtomicIncrement(ptr, increment); 85 } 86 87 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, 88 Atomic32 increment) { 89 for (;;) { 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. 96 return new_value; 97 } 98 // Otherwise, *ptr changed mid-loop and we need to retry. 99 } 100 } 101 102 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, 103 Atomic32 old_value, 104 Atomic32 new_value) { 105 return NoBarrier_CompareAndSwap(ptr, old_value, new_value); 106 } 107 108 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, 109 Atomic32 old_value, 110 Atomic32 new_value) { 111 return NoBarrier_CompareAndSwap(ptr, old_value, new_value); 112 } 113 114 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { 115 *ptr = value; 116 } 117 118 inline void MemoryBarrier() { 119 pLinuxKernelMemoryBarrier(); 120 } 121 122 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { 123 *ptr = value; 124 MemoryBarrier(); 125 } 126 127 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { 128 MemoryBarrier(); 129 *ptr = value; 130 } 131 132 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { 133 return *ptr; 134 } 135 136 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { 137 Atomic32 value = *ptr; 138 MemoryBarrier(); 139 return value; 140 } 141 142 inline Atomic32 Release_Load(volatile const Atomic32* ptr) { 143 MemoryBarrier(); 144 return *ptr; 145 } 146 147 } // namespace internal 148 } // namespace protobuf 149 } // namespace google 150 151 #endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_GCC_H_ 152