--- The idea of this patch is that for all platforms that are not supported by explicit implementations of atomic operations but happen to use gcc 4.7 or later we can use the generic builtin functions in there.
Someone might want to do a quick review if my understanding of the semantics of the interface was correct in all cases. When implementing this I wondered though whether it probably would make sense to limit the interface to be implemented to only those functions that are actually used. Currently this would be Acquire_CompareAndSwap, Release_Store, and Acquire_Load. I definitely would consider removing at least those functions that seem to have quite questionable semantics like Acquire_Store and Release_Load. I completely fail to see what those could be useful for and in fact even in the v8 project where this stuff is taken from there seems to be absolutely no user of those specific functions. It seems that implementors of those functions tried to find some kind of useful semantics by just adding full memory barriers before or after the actual operation but I really don't see the point of having them at all. Thus I decided to just implement them with relaxed semantics for now and not providing any special memory order semantics in my implementation. Robert src/google/protobuf/stubs/atomicops.h | 2 + .../stubs/atomicops_internals_builtin_gcc.h | 184 +++++++++++++++++++++ src/google/protobuf/stubs/platform_macros.h | 7 + 3 files changed, 193 insertions(+) create mode 100644 src/google/protobuf/stubs/atomicops_internals_builtin_gcc.h diff --git a/src/google/protobuf/stubs/atomicops.h b/src/google/protobuf/stubs/atomicops.h index 8ece86b..4e8eb33 100644 --- a/src/google/protobuf/stubs/atomicops.h +++ b/src/google/protobuf/stubs/atomicops.h @@ -184,6 +184,8 @@ GOOGLE_PROTOBUF_ATOMICOPS_ERROR #include <google/protobuf/stubs/atomicops_internals_mips_gcc.h> #elif defined(__pnacl__) #include <google/protobuf/stubs/atomicops_internals_pnacl.h> +#elif defined(GOOGLE_PROTOBUF_ARCH_BUILTIN_GCC) +#include <google/protobuf/stubs/atomicops_internals_builtin_gcc.h> #else GOOGLE_PROTOBUF_ATOMICOPS_ERROR #endif diff --git a/src/google/protobuf/stubs/atomicops_internals_builtin_gcc.h b/src/google/protobuf/stubs/atomicops_internals_builtin_gcc.h new file mode 100644 index 0000000..155e189 --- /dev/null +++ b/src/google/protobuf/stubs/atomicops_internals_builtin_gcc.h @@ -0,0 +1,184 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2012 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// This file is an internal atomic implementation, use atomicops.h instead. + +#ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_BUILTIN_GCC_H_ +#define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_BUILTIN_GCC_H_ + +namespace google { +namespace protobuf { +namespace internal { + +inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, + Atomic32 old_value, + Atomic32 new_value) { + Atomic32 x = old_value; + __atomic_compare_exchange_n(ptr, &x, new_value, false, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); + return x; +} + +inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, + Atomic32 new_value) { + return __atomic_exchange_n(ptr, new_value, __ATOMIC_RELAXED); +} + +inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, + Atomic32 increment) { + return __atomic_add_fetch(ptr, increment, __ATOMIC_RELAXED); +} + +inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, + Atomic32 increment) { + return __atomic_add_fetch(ptr, increment, __ATOMIC_ACQ_REL); +} + +inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, + Atomic32 old_value, + Atomic32 new_value) { + Atomic32 x = old_value; + __atomic_compare_exchange_n(ptr, &x, new_value, false, + __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE); + return x; +} + +inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, + Atomic32 old_value, + Atomic32 new_value) { + Atomic32 x = old_value; + __atomic_compare_exchange_n(ptr, &x, new_value, false, + __ATOMIC_RELEASE, __ATOMIC_RELAXED); + return x; +} + +inline void MemoryBarrier() { + __atomic_thread_fence(__ATOMIC_ACQ_REL); +} + +inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { + __atomic_store_n(ptr, value, __ATOMIC_RELAXED); +} + +// What is the memory order semantics of Acquire_Store? +inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { + __atomic_store_n(ptr, value, __ATOMIC_RELAXED); +} + +inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { + __atomic_store_n(ptr, value, __ATOMIC_RELEASE); +} + +inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { + return __atomic_load_n(ptr, __ATOMIC_RELAXED); +} + +inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { + return __atomic_load_n(ptr, __ATOMIC_ACQUIRE); +} + +// What is the memory order semantics of Release_Load? +inline Atomic32 Release_Load(volatile const Atomic32* ptr) { + return __atomic_load_n(ptr, __ATOMIC_RELAXED); +} + +#ifdef GOOGLE_PROTOBUF_ARCH_64_BIT +inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, + Atomic64 old_value, + Atomic64 new_value) { + Atomic64 x = old_value; + __atomic_compare_exchange_n(ptr, &x, new_value, false, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); + return x; +} + +inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value) { + return __atomic_exchange_n(ptr, new_value, __ATOMIC_RELAXED); +} + +inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment) { + return __atomic_add_fetch(ptr, increment, __ATOMIC_RELAXED); +} + +inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment) { + return __atomic_add_fetch(ptr, increment, __ATOMIC_ACQ_REL); +} + +inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, + Atomic64 old_value, + Atomic64 new_value) { + Atomic64 x = old_value; + __atomic_compare_exchange_n(ptr, &x, new_value, false, + __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE); + return x; +} + +inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr, + Atomic64 old_value, + Atomic64 new_value) { + Atomic64 x = old_value; + __atomic_compare_exchange_n(ptr, &x, new_value, false, + __ATOMIC_RELEASE, __ATOMIC_RELAXED); + return x; +} + +inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { + __atomic_store_n(ptr, value, __ATOMIC_RELAXED); +} + +// What is the memory order semantics of Acquire_Store? +inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { + __atomic_store_n(ptr, value, __ATOMIC_RELAXED); +} + +inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { + __atomic_store_n(ptr, value, __ATOMIC_RELAXED); +} + +inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { + return __atomic_load_n(ptr, __ATOMIC_RELAXED); +} + +inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { + return __atomic_load_n(ptr, __ATOMIC_ACQUIRE); +} + +// What is the memory order semantics of Release_Load? +inline Atomic64 Release_Load(volatile const Atomic64* ptr) { + return __atomic_load_n(ptr, __ATOMIC_RELAXED); +} + +#endif // GOOGLE_PROTOBUF_ARCH_64_BIT + +} // namespace internal +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_BUILTIN_GCC_H_ diff --git a/src/google/protobuf/stubs/platform_macros.h b/src/google/protobuf/stubs/platform_macros.h index b1df60e..5491704 100644 --- a/src/google/protobuf/stubs/platform_macros.h +++ b/src/google/protobuf/stubs/platform_macros.h @@ -57,6 +57,13 @@ #elif defined(__ppc__) #define GOOGLE_PROTOBUF_ARCH_PPC 1 #define GOOGLE_PROTOBUF_ARCH_32_BIT 1 +#elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) +#define GOOGLE_PROTOBUF_ARCH_BUILTIN_GCC 1 +#if defined(__LP64__) +#define GOOGLE_PROTOBUF_ARCH_64_BIT 1 +#else +#define GOOGLE_PROTOBUF_ARCH_32_BIT 1 +#endif #else #error Host architecture was not detected as supported by protobuf #endif -- 1.8.1.4 -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/protobuf?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
