atomic.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <stdint.h>
  17. #include <bits/kernel-features.h>
  18. #define ABORT_INSTRUCTION __asm__(__UCLIBC_ABORT_INSTRUCTION__)
  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. #if __ASSUME_LWS_CAS
  49. /* The only basic operation needed is compare and exchange. */
  50. # define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
  51. ({ \
  52. volatile int lws_errno = EFAULT; \
  53. volatile int lws_ret = 0xdeadbeef; \
  54. __asm__ volatile( \
  55. "0: \n\t" \
  56. "copy %3, %%r26 \n\t" \
  57. "copy %4, %%r25 \n\t" \
  58. "copy %5, %%r24 \n\t" \
  59. "ble " LWS "(%%sr2, %%r0) \n\t" \
  60. "ldi " LWS_CAS ", %%r20 \n\t" \
  61. "cmpib,=,n " ASM_EAGAIN ",%%r21,0b \n\t" \
  62. "nop \n\t" \
  63. "stw %%r28, %0 \n\t" \
  64. "sub %%r0, %%r21, %%r21 \n\t" \
  65. "stw %%r21, %1 \n\t" \
  66. : "=m" (lws_ret), "=m" (lws_errno), "=m" (*mem) \
  67. : "r" (mem), "r" (oldval), "r" (newval) \
  68. : LWS_CLOBBER \
  69. ); \
  70. \
  71. if(lws_errno == EFAULT || lws_errno == ENOSYS) \
  72. ABORT_INSTRUCTION; \
  73. \
  74. lws_ret; \
  75. })
  76. # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
  77. ({ \
  78. int ret; \
  79. ret = atomic_compare_and_exchange_val_acq(mem, newval, oldval); \
  80. /* Return 1 if it was already acquired */ \
  81. (ret != oldval); \
  82. })
  83. #else
  84. # error __ASSUME_LWS_CAS is required to build uClibc.
  85. #endif
  86. /* __ASSUME_LWS_CAS */
  87. #endif
  88. /* _BITS_ATOMIC_H */