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

https://bitbucket.org/freebsd/freebsd-head/ · C Header · 126 lines · 46 code · 19 blank · 61 comment · 6 complexity · 8b086b52cb3475581e90904c108541cc MD5 · raw file

  1. /*
  2. * Copyright (C) 2005, 2007 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.5 2007/06/19 23:47:18 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. * Copyright (c) 2001 Jake Burkholder.
  24. * All rights reserved.
  25. *
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the following conditions
  28. * are met:
  29. * 1. Redistributions of source code must retain the above copyright
  30. * notice, this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright
  32. * notice, this list of conditions and the following disclaimer in the
  33. * documentation and/or other materials provided with the distribution.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  36. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  41. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  42. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  45. * SUCH DAMAGE.
  46. *
  47. * from: FreeBSD: src/sys/i386/include/atomic.h,v 1.20 2001/02/11
  48. * $FreeBSD$
  49. */
  50. #ifndef ISC_ATOMIC_H
  51. #define ISC_ATOMIC_H 1
  52. #include <isc/platform.h>
  53. #include <isc/types.h>
  54. #define ASI_P 0x80 /* Primary Address Space Identifier */
  55. #ifdef ISC_PLATFORM_USEGCCASM
  56. /*
  57. * This routine atomically increments the value stored in 'p' by 'val', and
  58. * returns the previous value.
  59. */
  60. static inline isc_int32_t
  61. isc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
  62. isc_int32_t prev, swapped;
  63. for (prev = *(volatile isc_int32_t *)p; ; prev = swapped) {
  64. swapped = prev + val;
  65. __asm__ volatile(
  66. "casa [%2] %3, %4, %0"
  67. : "+r"(swapped), "=m"(*p)
  68. : "r"(p), "n"(ASI_P), "r"(prev), "m"(*p));
  69. if (swapped == prev)
  70. break;
  71. }
  72. return (prev);
  73. }
  74. /*
  75. * This routine atomically stores the value 'val' in 'p'.
  76. */
  77. static inline void
  78. isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
  79. isc_int32_t prev, swapped;
  80. for (prev = *(volatile isc_int32_t *)p; ; prev = swapped) {
  81. swapped = val;
  82. __asm__ volatile(
  83. "casa [%2] %3, %4, %0"
  84. : "+r"(swapped), "=m"(*p)
  85. : "r"(p), "n"(ASI_P), "r"(prev), "m"(*p));
  86. if (swapped == prev)
  87. break;
  88. }
  89. }
  90. /*
  91. * This routine atomically replaces the value in 'p' with 'val', if the
  92. * original value is equal to 'cmpval'. The original value is returned in any
  93. * case.
  94. */
  95. static inline isc_int32_t
  96. isc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
  97. isc_int32_t temp = val;
  98. __asm__ volatile(
  99. "casa [%2] %3, %4, %0"
  100. : "+r"(temp), "=m"(*p)
  101. : "r"(p), "n"(ASI_P), "r"(cmpval), "m"(*p));
  102. return (temp);
  103. }
  104. #else /* ISC_PLATFORM_USEGCCASM */
  105. #error "unsupported compiler. disable atomic ops by --disable-atomic"
  106. #endif /* ISC_PLATFORM_USEGCCASM */
  107. #endif /* ISC_ATOMIC_H */