atomicity.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Low-level functions for atomic operations. PowerPC version.
  2. Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. #ifndef _ATOMICITY_H
  17. #define _ATOMICITY_H 1
  18. #include <inttypes.h>
  19. #if BROKEN_PPC_ASM_CR0
  20. # define __ATOMICITY_INLINE /* nothing */
  21. #else
  22. # define __ATOMICITY_INLINE inline
  23. #endif
  24. static __ATOMICITY_INLINE int
  25. __attribute_used__
  26. exchange_and_add (volatile uint32_t *mem, int val)
  27. {
  28. int tmp, result;
  29. __asm__ ("\n\
  30. 0: lwarx %0,0,%2 \n\
  31. add%I3 %1,%0,%3 \n\
  32. stwcx. %1,0,%2 \n\
  33. bne- 0b \n\
  34. " : "=&b"(result), "=&r"(tmp) : "r" (mem), "Ir"(val) : "cr0", "memory");
  35. return result;
  36. }
  37. static __ATOMICITY_INLINE void
  38. __attribute_used__
  39. atomic_add (volatile uint32_t *mem, int val)
  40. {
  41. int tmp;
  42. __asm__ ("\n\
  43. 0: lwarx %0,0,%1 \n\
  44. add%I2 %0,%0,%2 \n\
  45. stwcx. %0,0,%1 \n\
  46. bne- 0b \n\
  47. " : "=&b"(tmp) : "r" (mem), "Ir"(val) : "cr0", "memory");
  48. }
  49. static __ATOMICITY_INLINE int
  50. __attribute_used__
  51. compare_and_swap (volatile long int *p, long int oldval, long int newval)
  52. {
  53. int result;
  54. __asm__ ("\n\
  55. 0: lwarx %0,0,%1 \n\
  56. sub%I2c. %0,%0,%2 \n\
  57. cntlzw %0,%0 \n\
  58. bne- 1f \n\
  59. stwcx. %3,0,%1 \n\
  60. bne- 0b \n\
  61. 1: \n\
  62. " : "=&b"(result) : "r"(p), "Ir"(oldval), "r"(newval) : "cr0", "memory");
  63. return result >> 5;
  64. }
  65. static __ATOMICITY_INLINE long int
  66. __attribute_used__
  67. always_swap (volatile long int *p, long int newval)
  68. {
  69. long int result;
  70. __asm__ ("\n\
  71. 0: lwarx %0,0,%1 \n\
  72. stwcx. %2,0,%1 \n\
  73. bne- 0b \n\
  74. " : "=&r"(result) : "r"(p), "r"(newval) : "cr0", "memory");
  75. return result;
  76. }
  77. static __ATOMICITY_INLINE int
  78. __attribute_used__
  79. test_and_set (volatile long int *p, long int newval)
  80. {
  81. int result;
  82. __asm__ ("\n\
  83. 0: lwarx %0,0,%1 \n\
  84. cmpwi %0,0 \n\
  85. bne- 1f \n\
  86. stwcx. %2,0,%1 \n\
  87. bne- 0b \n\
  88. 1: \n\
  89. " : "=&r"(result) : "r"(p), "r"(newval) : "cr0", "memory");
  90. return result;
  91. }
  92. #endif /* atomicity.h */