atomicops_internals_solaris.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2014 Google Inc. All rights reserved.
  2. // https://developers.google.com/protocol-buffers/
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // This file is an internal atomic implementation, use atomicops.h instead.
  30. #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_SPARC_GCC_H_
  31. #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_SPARC_GCC_H_
  32. #include <atomic.h>
  33. namespace google {
  34. namespace protobuf {
  35. namespace internal {
  36. inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
  37. Atomic32 old_value,
  38. Atomic32 new_value) {
  39. return (Atomic32)atomic_cas_32((volatile uint32_t*)ptr, (uint32_t)old_value, (uint32_t)new_value);
  40. }
  41. inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
  42. Atomic32 new_value) {
  43. return (Atomic32)atomic_swap_32((volatile uint32_t*)ptr, (uint32_t)new_value);
  44. }
  45. inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
  46. Atomic32 increment) {
  47. return (Atomic32)atomic_add_32_nv((volatile uint32_t*)ptr, (uint32_t)increment);
  48. }
  49. inline void MemoryBarrierInternal(void) {
  50. membar_producer();
  51. membar_consumer();
  52. }
  53. inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
  54. Atomic32 increment) {
  55. MemoryBarrierInternal();
  56. Atomic32 ret = NoBarrier_AtomicIncrement(ptr, increment);
  57. MemoryBarrierInternal();
  58. return ret;
  59. }
  60. inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
  61. Atomic32 old_value,
  62. Atomic32 new_value) {
  63. Atomic32 ret = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
  64. MemoryBarrierInternal();
  65. return ret;
  66. }
  67. inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
  68. Atomic32 old_value,
  69. Atomic32 new_value) {
  70. MemoryBarrierInternal();
  71. return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
  72. }
  73. inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
  74. *ptr = value;
  75. }
  76. inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
  77. *ptr = value;
  78. membar_producer();
  79. }
  80. inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
  81. membar_consumer();
  82. *ptr = value;
  83. }
  84. inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
  85. return *ptr;
  86. }
  87. inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
  88. Atomic32 val = *ptr;
  89. membar_consumer();
  90. return val;
  91. }
  92. inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
  93. membar_producer();
  94. return *ptr;
  95. }
  96. #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
  97. inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
  98. Atomic64 old_value,
  99. Atomic64 new_value) {
  100. return atomic_cas_64((volatile uint64_t*)ptr, (uint64_t)old_value, (uint64_t)new_value);
  101. }
  102. inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value) {
  103. return atomic_swap_64((volatile uint64_t*)ptr, (uint64_t)new_value);
  104. }
  105. inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment) {
  106. return atomic_add_64_nv((volatile uint64_t*)ptr, increment);
  107. }
  108. inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment) {
  109. MemoryBarrierInternal();
  110. Atomic64 ret = atomic_add_64_nv((volatile uint64_t*)ptr, increment);
  111. MemoryBarrierInternal();
  112. return ret;
  113. }
  114. inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
  115. Atomic64 old_value,
  116. Atomic64 new_value) {
  117. Atomic64 ret = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
  118. MemoryBarrierInternal();
  119. return ret;
  120. }
  121. inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
  122. Atomic64 old_value,
  123. Atomic64 new_value) {
  124. MemoryBarrierInternal();
  125. return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
  126. }
  127. inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) {
  128. *ptr = value;
  129. }
  130. inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) {
  131. *ptr = value;
  132. membar_producer();
  133. }
  134. inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) {
  135. membar_consumer();
  136. *ptr = value;
  137. }
  138. inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) {
  139. return *ptr;
  140. }
  141. inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) {
  142. Atomic64 ret = *ptr;
  143. membar_consumer();
  144. return ret;
  145. }
  146. inline Atomic64 Release_Load(volatile const Atomic64* ptr) {
  147. membar_producer();
  148. return *ptr;
  149. }
  150. #endif
  151. } // namespace internal
  152. } // namespace protobuf
  153. } // namespace google
  154. #endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_SPARC_GCC_H_