/tools/simulator/libsimulator/lib/protobuf-lite/google/protobuf/stubs/atomicops_internals_arm_gcc.h

https://github.com/dumganhar/cocos2d-x · C Header · 151 lines · 90 code · 21 blank · 40 comment · 7 complexity · b0c7e44eefe09435614b26ade2b43781 MD5 · raw file

  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. // This file is an internal atomic implementation, use atomicops.h instead.
  31. //
  32. // LinuxKernelCmpxchg and Barrier_AtomicIncrement are from Google Gears.
  33. #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_GCC_H_
  34. #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_GCC_H_
  35. namespace google {
  36. namespace protobuf {
  37. namespace internal {
  38. // 0xffff0fc0 is the hard coded address of a function provided by
  39. // the kernel which implements an atomic compare-exchange. On older
  40. // ARM architecture revisions (pre-v6) this may be implemented using
  41. // a syscall. This address is stable, and in active use (hard coded)
  42. // by at least glibc-2.7 and the Android C library.
  43. typedef Atomic32 (*LinuxKernelCmpxchgFunc)(Atomic32 old_value,
  44. Atomic32 new_value,
  45. volatile Atomic32* ptr);
  46. LinuxKernelCmpxchgFunc pLinuxKernelCmpxchg __attribute__((weak)) =
  47. (LinuxKernelCmpxchgFunc) 0xffff0fc0;
  48. typedef void (*LinuxKernelMemoryBarrierFunc)(void);
  49. LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier __attribute__((weak)) =
  50. (LinuxKernelMemoryBarrierFunc) 0xffff0fa0;
  51. inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
  52. Atomic32 old_value,
  53. Atomic32 new_value) {
  54. Atomic32 prev_value = *ptr;
  55. do {
  56. if (!pLinuxKernelCmpxchg(old_value, new_value,
  57. const_cast<Atomic32*>(ptr))) {
  58. return old_value;
  59. }
  60. prev_value = *ptr;
  61. } while (prev_value == old_value);
  62. return prev_value;
  63. }
  64. inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
  65. Atomic32 new_value) {
  66. Atomic32 old_value;
  67. do {
  68. old_value = *ptr;
  69. } while (pLinuxKernelCmpxchg(old_value, new_value,
  70. const_cast<Atomic32*>(ptr)));
  71. return old_value;
  72. }
  73. inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
  74. Atomic32 increment) {
  75. return Barrier_AtomicIncrement(ptr, increment);
  76. }
  77. inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
  78. Atomic32 increment) {
  79. for (;;) {
  80. // Atomic exchange the old value with an incremented one.
  81. Atomic32 old_value = *ptr;
  82. Atomic32 new_value = old_value + increment;
  83. if (pLinuxKernelCmpxchg(old_value, new_value,
  84. const_cast<Atomic32*>(ptr)) == 0) {
  85. // The exchange took place as expected.
  86. return new_value;
  87. }
  88. // Otherwise, *ptr changed mid-loop and we need to retry.
  89. }
  90. }
  91. inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
  92. Atomic32 old_value,
  93. Atomic32 new_value) {
  94. return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
  95. }
  96. inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
  97. Atomic32 old_value,
  98. Atomic32 new_value) {
  99. return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
  100. }
  101. inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
  102. *ptr = value;
  103. }
  104. inline void MemoryBarrier() {
  105. pLinuxKernelMemoryBarrier();
  106. }
  107. inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
  108. *ptr = value;
  109. MemoryBarrier();
  110. }
  111. inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
  112. MemoryBarrier();
  113. *ptr = value;
  114. }
  115. inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
  116. return *ptr;
  117. }
  118. inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
  119. Atomic32 value = *ptr;
  120. MemoryBarrier();
  121. return value;
  122. }
  123. inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
  124. MemoryBarrier();
  125. return *ptr;
  126. }
  127. } // namespace internal
  128. } // namespace protobuf
  129. } // namespace google
  130. #endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_GCC_H_