atomic.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Carlos O'Donell <carlos@baldric.uwo.ca>, 2005.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdint.h>
  16. #include <errno.h>
  17. #include <bits/kernel-features.h>
  18. #define ABORT_INSTRUCTION __asm__("iitlbp %r0,(%sr0, %r0)")
  19. /* We need EFAULT, ENOSYS */
  20. #if !defined EFAULT && !defined ENOSYS
  21. #define EFAULT 14
  22. #define ENOSYS 251
  23. #endif
  24. #ifndef _BITS_ATOMIC_H
  25. #define _BITS_ATOMIC_H 1
  26. typedef int8_t atomic8_t;
  27. typedef uint8_t uatomic8_t;
  28. typedef int_fast8_t atomic_fast8_t;
  29. typedef uint_fast8_t uatomic_fast8_t;
  30. typedef int32_t atomic32_t;
  31. typedef uint32_t uatomic32_t;
  32. typedef int_fast32_t atomic_fast32_t;
  33. typedef uint_fast32_t uatomic_fast32_t;
  34. typedef intptr_t atomicptr_t;
  35. typedef uintptr_t uatomicptr_t;
  36. typedef intmax_t atomic_max_t;
  37. typedef uintmax_t uatomic_max_t;
  38. /* prev = *addr;
  39. if (prev == old)
  40. *addr = new;
  41. return prev; */
  42. /* Use the kernel atomic light weight syscalls on hppa */
  43. #define LWS "0xb0"
  44. #define LWS_CAS "0"
  45. /* Note r31 is the link register */
  46. #define LWS_CLOBBER "r1", "r26", "r25", "r24", "r23", "r22", "r21", "r20", "r28", "r31", "memory"
  47. #define ASM_EAGAIN "11"
  48. /* The only basic operation needed is compare and exchange. */
  49. # define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
  50. ({ \
  51. volatile int lws_errno = EFAULT; \
  52. volatile int lws_ret = 0xdeadbeef; \
  53. __asm__ volatile( \
  54. "0: \n\t" \
  55. "copy %3, %%r26 \n\t" \
  56. "copy %4, %%r25 \n\t" \
  57. "copy %5, %%r24 \n\t" \
  58. "ble " LWS "(%%sr2, %%r0) \n\t" \
  59. "ldi " LWS_CAS ", %%r20 \n\t" \
  60. "cmpib,=,n " ASM_EAGAIN ",%%r21,0b \n\t" \
  61. "nop \n\t" \
  62. "stw %%r28, %0 \n\t" \
  63. "sub %%r0, %%r21, %%r21 \n\t" \
  64. "stw %%r21, %1 \n\t" \
  65. : "=m" (lws_ret), "=m" (lws_errno), "=m" (*mem) \
  66. : "r" (mem), "r" (oldval), "r" (newval) \
  67. : LWS_CLOBBER \
  68. ); \
  69. \
  70. if(lws_errno == EFAULT || lws_errno == ENOSYS) \
  71. ABORT_INSTRUCTION; \
  72. \
  73. lws_ret; \
  74. })
  75. # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
  76. ({ \
  77. int ret; \
  78. ret = atomic_compare_and_exchange_val_acq(mem, newval, oldval); \
  79. /* Return 1 if it was already acquired */ \
  80. (ret != oldval); \
  81. })
  82. #endif
  83. /* _BITS_ATOMIC_H */