/contrib/bind9/lib/isc/alpha/include/isc/atomic.h

https://bitbucket.org/freebsd/freebsd-head/ · C Header · 184 lines · 102 code · 21 blank · 61 comment · 0 complexity · c4ccfcbefa57b38bb23c03be9932e3f8 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* $Id: atomic.h,v 1.7 2009/04/08 06:48:23 tbox Exp $ */
  17. /*
  18. * This code was written based on FreeBSD's kernel source whose copyright
  19. * follows:
  20. */
  21. /*-
  22. * Copyright (c) 1998 Doug Rabson
  23. * All rights reserved.
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. * modification, are permitted provided that the following conditions
  27. * are met:
  28. * 1. Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in the
  32. * documentation and/or other materials provided with the distribution.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  35. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  38. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  40. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  43. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  44. * SUCH DAMAGE.
  45. *
  46. * $FreeBSD$
  47. */
  48. #ifndef ISC_ATOMIC_H
  49. #define ISC_ATOMIC_H 1
  50. #include <isc/platform.h>
  51. #include <isc/types.h>
  52. #ifdef ISC_PLATFORM_USEOSFASM
  53. #include <c_asm.h>
  54. #pragma intrinsic(asm)
  55. /*
  56. * This routine atomically increments the value stored in 'p' by 'val', and
  57. * returns the previous value. Memory access ordering around this function
  58. * can be critical, so we add explicit memory block instructions at the
  59. * beginning and the end of it (same for other functions).
  60. */
  61. static inline isc_int32_t
  62. isc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
  63. return (asm("mb;"
  64. "1:"
  65. "ldl_l %t0, 0(%a0);" /* load old value */
  66. "mov %t0, %v0;" /* copy the old value */
  67. "addl %t0, %a1, %t0;" /* calculate new value */
  68. "stl_c %t0, 0(%a0);" /* attempt to store */
  69. "beq %t0, 1b;" /* spin if failed */
  70. "mb;",
  71. p, val));
  72. }
  73. /*
  74. * This routine atomically stores the value 'val' in 'p'.
  75. */
  76. static inline void
  77. isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
  78. (void)asm("mb;"
  79. "1:"
  80. "ldl_l %t0, 0(%a0);" /* load old value */
  81. "mov %a1, %t0;" /* value to store */
  82. "stl_c %t0, 0(%a0);" /* attempt to store */
  83. "beq %t0, 1b;" /* spin if failed */
  84. "mb;",
  85. p, val);
  86. }
  87. /*
  88. * This routine atomically replaces the value in 'p' with 'val', if the
  89. * original value is equal to 'cmpval'. The original value is returned in any
  90. * case.
  91. */
  92. static inline isc_int32_t
  93. isc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
  94. return(asm("mb;"
  95. "1:"
  96. "ldl_l %t0, 0(%a0);" /* load old value */
  97. "mov %t0, %v0;" /* copy the old value */
  98. "cmpeq %t0, %a1, %t0;" /* compare */
  99. "beq %t0, 2f;" /* exit if not equal */
  100. "mov %a2, %t0;" /* value to store */
  101. "stl_c %t0, 0(%a0);" /* attempt to store */
  102. "beq %t0, 1b;" /* if it failed, spin */
  103. "2:"
  104. "mb;",
  105. p, cmpval, val));
  106. }
  107. #elif defined (ISC_PLATFORM_USEGCCASM)
  108. static inline isc_int32_t
  109. isc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
  110. isc_int32_t temp, prev;
  111. __asm__ volatile(
  112. "mb;"
  113. "1:"
  114. "ldl_l %0, %1;" /* load old value */
  115. "mov %0, %2;" /* copy the old value */
  116. "addl %0, %3, %0;" /* calculate new value */
  117. "stl_c %0, %1;" /* attempt to store */
  118. "beq %0, 1b;" /* spin if failed */
  119. "mb;"
  120. : "=&r"(temp), "+m"(*p), "=&r"(prev)
  121. : "r"(val)
  122. : "memory");
  123. return (prev);
  124. }
  125. static inline void
  126. isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
  127. isc_int32_t temp;
  128. __asm__ volatile(
  129. "mb;"
  130. "1:"
  131. "ldl_l %0, %1;" /* load old value */
  132. "mov %2, %0;" /* value to store */
  133. "stl_c %0, %1;" /* attempt to store */
  134. "beq %0, 1b;" /* if it failed, spin */
  135. "mb;"
  136. : "=&r"(temp), "+m"(*p)
  137. : "r"(val)
  138. : "memory");
  139. }
  140. static inline isc_int32_t
  141. isc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
  142. isc_int32_t temp, prev;
  143. __asm__ volatile(
  144. "mb;"
  145. "1:"
  146. "ldl_l %0, %1;" /* load old value */
  147. "mov %0, %2;" /* copy the old value */
  148. "cmpeq %0, %3, %0;" /* compare */
  149. "beq %0, 2f;" /* exit if not equal */
  150. "mov %4, %0;" /* value to store */
  151. "stl_c %0, %1;" /* attempt to store */
  152. "beq %0, 1b;" /* if it failed, spin */
  153. "2:"
  154. "mb;"
  155. : "=&r"(temp), "+m"(*p), "=&r"(prev)
  156. : "r"(cmpval), "r"(val)
  157. : "memory");
  158. return (prev);
  159. }
  160. #else
  161. #error "unsupported compiler. disable atomic ops by --disable-atomic"
  162. #endif
  163. #endif /* ISC_ATOMIC_H */