1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2015 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 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 // Author: ogabbay (at) advaoptical.com (Oded Gabbay) 32 // Cleaned up by: bsilver16384 (at) gmail.com (Brian Silverman) 33 // 34 // This file is an internal atomic implementation, use atomicops.h instead. 35 36 #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_PPC_GCC_H_ 37 #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_PPC_GCC_H_ 38 39 #define ATOMICOPS_COMPILER_BARRIER() __asm__ __volatile__("" : : : "memory") 40 41 namespace google { 42 namespace protobuf { 43 namespace internal { 44 45 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32 *ptr, 46 Atomic32 old_value, 47 Atomic32 new_value) { 48 Atomic32 prev; 49 50 __asm__ __volatile__( 51 "0: \n\t" 52 "lwarx %[prev],0,%[ptr] \n\t" 53 "cmpw 0,%[prev],%[old_value] \n\t" 54 "bne- 1f \n\t" 55 "stwcx. %[new_value],0,%[ptr] \n\t" 56 "bne- 0b \n\t" 57 "1: \n\t" 58 : [prev] "=&r"(prev), "+m"(*ptr) 59 : [ptr] "r"(ptr), [old_value] "r"(old_value), [new_value] "r"(new_value) 60 : "cc", "memory"); 61 62 return prev; 63 } 64 65 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32 *ptr, 66 Atomic32 new_value) { 67 Atomic32 old; 68 69 __asm__ __volatile__( 70 "0: \n\t" 71 "lwarx %[old],0,%[ptr] \n\t" 72 "stwcx. %[new_value],0,%[ptr] \n\t" 73 "bne- 0b \n\t" 74 : [old] "=&r"(old), "+m"(*ptr) 75 : [ptr] "r"(ptr), [new_value] "r"(new_value) 76 : "cc", "memory"); 77 78 return old; 79 } 80 81 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32 *ptr, 82 Atomic32 increment) { 83 Atomic32 temp; 84 85 __asm__ __volatile__( 86 "0: \n\t" 87 "lwarx %[temp],0,%[ptr] \n\t" 88 "add %[temp],%[increment],%[temp] \n\t" 89 "stwcx. %[temp],0,%[ptr] \n\t" 90 "bne- 0b \n\t" 91 : [temp] "=&r"(temp) 92 : [increment] "r"(increment), [ptr] "r"(ptr) 93 : "cc", "memory"); 94 95 return temp; 96 } 97 98 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32 *ptr, 99 Atomic32 increment) { 100 MemoryBarrier(); 101 Atomic32 res = NoBarrier_AtomicIncrement(ptr, increment); 102 MemoryBarrier(); 103 return res; 104 } 105 106 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32 *ptr, 107 Atomic32 old_value, Atomic32 new_value) { 108 Atomic32 res = NoBarrier_CompareAndSwap(ptr, old_value, new_value); 109 MemoryBarrier(); 110 return res; 111 } 112 113 inline Atomic32 Release_CompareAndSwap(volatile Atomic32 *ptr, 114 Atomic32 old_value, Atomic32 new_value) { 115 MemoryBarrier(); 116 Atomic32 res = NoBarrier_CompareAndSwap(ptr, old_value, new_value); 117 return res; 118 } 119 120 inline void NoBarrier_Store(volatile Atomic32 *ptr, Atomic32 value) { 121 *ptr = value; 122 } 123 124 inline void MemoryBarrier() { __asm__ __volatile__("sync" : : : "memory"); } 125 126 inline void Acquire_Store(volatile Atomic32 *ptr, Atomic32 value) { 127 *ptr = value; 128 MemoryBarrier(); 129 } 130 131 inline void Release_Store(volatile Atomic32 *ptr, Atomic32 value) { 132 MemoryBarrier(); 133 *ptr = value; 134 } 135 136 inline Atomic32 NoBarrier_Load(volatile const Atomic32 *ptr) { return *ptr; } 137 138 inline Atomic32 Acquire_Load(volatile const Atomic32 *ptr) { 139 Atomic32 value = *ptr; 140 MemoryBarrier(); 141 return value; 142 } 143 144 inline Atomic32 Release_Load(volatile const Atomic32 *ptr) { 145 MemoryBarrier(); 146 return *ptr; 147 } 148 149 } // namespace internal 150 } // namespace protobuf 151 } // namespace google 152 153 #undef ATOMICOPS_COMPILER_BARRIER 154 155 #endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_PPC_GCC_H_ 156