atomicops_internals_mips_gcc.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This file is an internal atomic implementation, use base/atomicops.h instead.
  5. //
  6. // LinuxKernelCmpxchg and Barrier_AtomicIncrement are from Google Gears.
  7. #ifndef BASE_ATOMICOPS_INTERNALS_MIPS_GCC_H_
  8. #define BASE_ATOMICOPS_INTERNALS_MIPS_GCC_H_
  9. namespace base {
  10. namespace subtle {
  11. // Atomically execute:
  12. // result = *ptr;
  13. // if (*ptr == old_value)
  14. // *ptr = new_value;
  15. // return result;
  16. //
  17. // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
  18. // Always return the old value of "*ptr"
  19. //
  20. // This routine implies no memory barriers.
  21. inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
  22. Atomic32 old_value,
  23. Atomic32 new_value) {
  24. Atomic32 prev, tmp;
  25. __asm__ __volatile__(".set push\n"
  26. ".set noreorder\n"
  27. "1:\n"
  28. "ll %0, %5\n" // prev = *ptr
  29. "bne %0, %3, 2f\n" // if (prev != old_value) goto 2
  30. "move %2, %4\n" // tmp = new_value
  31. "sc %2, %1\n" // *ptr = tmp (with atomic check)
  32. "beqz %2, 1b\n" // start again on atomic error
  33. "nop\n" // delay slot nop
  34. "2:\n"
  35. ".set pop\n"
  36. : "=&r" (prev), "=m" (*ptr), "=&r" (tmp)
  37. : "Ir" (old_value), "r" (new_value), "m" (*ptr)
  38. : "memory");
  39. return prev;
  40. }
  41. // Atomically store new_value into *ptr, returning the previous value held in
  42. // *ptr. This routine implies no memory barriers.
  43. inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
  44. Atomic32 new_value) {
  45. Atomic32 temp, old;
  46. __asm__ __volatile__(".set push\n"
  47. ".set noreorder\n"
  48. "1:\n"
  49. "ll %1, %2\n" // old = *ptr
  50. "move %0, %3\n" // temp = new_value
  51. "sc %0, %2\n" // *ptr = temp (with atomic check)
  52. "beqz %0, 1b\n" // start again on atomic error
  53. "nop\n" // delay slot nop
  54. ".set pop\n"
  55. : "=&r" (temp), "=&r" (old), "=m" (*ptr)
  56. : "r" (new_value), "m" (*ptr)
  57. : "memory");
  58. return old;
  59. }
  60. // Atomically increment *ptr by "increment". Returns the new value of
  61. // *ptr with the increment applied. This routine implies no memory barriers.
  62. inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
  63. Atomic32 increment) {
  64. Atomic32 temp, temp2;
  65. __asm__ __volatile__(".set push\n"
  66. ".set noreorder\n"
  67. "1:\n"
  68. "ll %0, %2\n" // temp = *ptr
  69. "addu %1, %0, %3\n" // temp2 = temp + increment
  70. "sc %1, %2\n" // *ptr = temp2 (with atomic check)
  71. "beqz %1, 1b\n" // start again on atomic error
  72. "addu %1, %0, %3\n" // temp2 = temp + increment
  73. ".set pop\n"
  74. : "=&r" (temp), "=&r" (temp2), "=m" (*ptr)
  75. : "Ir" (increment), "m" (*ptr)
  76. : "memory");
  77. // temp2 now holds the final value.
  78. return temp2;
  79. }
  80. inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
  81. Atomic32 increment) {
  82. MemoryBarrier();
  83. Atomic32 res = NoBarrier_AtomicIncrement(ptr, increment);
  84. MemoryBarrier();
  85. return res;
  86. }
  87. // "Acquire" operations
  88. // ensure that no later memory access can be reordered ahead of the operation.
  89. // "Release" operations ensure that no previous memory access can be reordered
  90. // after the operation. "Barrier" operations have both "Acquire" and "Release"
  91. // semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory
  92. // access.
  93. inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
  94. Atomic32 old_value,
  95. Atomic32 new_value) {
  96. Atomic32 res = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
  97. MemoryBarrier();
  98. return res;
  99. }
  100. inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
  101. Atomic32 old_value,
  102. Atomic32 new_value) {
  103. MemoryBarrier();
  104. return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
  105. }
  106. inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
  107. *ptr = value;
  108. }
  109. inline void MemoryBarrier() {
  110. __asm__ __volatile__("sync" : : : "memory");
  111. }
  112. inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
  113. *ptr = value;
  114. MemoryBarrier();
  115. }
  116. inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
  117. MemoryBarrier();
  118. *ptr = value;
  119. }
  120. inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
  121. return *ptr;
  122. }
  123. inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
  124. Atomic32 value = *ptr;
  125. MemoryBarrier();
  126. return value;
  127. }
  128. inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
  129. MemoryBarrier();
  130. return *ptr;
  131. }
  132. } // namespace base::subtle
  133. } // namespace base
  134. #endif // BASE_ATOMICOPS_INTERNALS_MIPS_GCC_H_