atomicity.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Low-level functions for atomic operations. ARM 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. static inline int
  20. __attribute_used__
  21. exchange_and_add (volatile uint32_t *mem, int val)
  22. {
  23. int tmp1;
  24. int tmp2;
  25. int result;
  26. __asm__ ("\n"
  27. #if defined(__thumb__)
  28. "\t.align 0\n"
  29. "\tbx pc\n"
  30. "\tnop\n"
  31. "\t.arm\n"
  32. #endif
  33. "0:\tldr\t%0,[%3]\n\t"
  34. "add\t%1,%0,%4\n\t"
  35. "swp\t%2,%1,[%3]\n\t"
  36. "cmp\t%0,%2\n\t"
  37. "swpne\t%1,%2,[%3]\n\t"
  38. "bne\t0b"
  39. #if defined(__thumb__)
  40. "\torr %1, pc, #1\n"
  41. "\tbx %1\n"
  42. "\t.force_thumb"
  43. #endif
  44. : "=&r" (result), "=&r" (tmp1), "=&r" (tmp2)
  45. : "r" (mem), "r"(val)
  46. : "cc", "memory");
  47. return result;
  48. }
  49. static inline void
  50. __attribute_used__
  51. atomic_add (volatile uint32_t *mem, int val)
  52. {
  53. int tmp1;
  54. int tmp2;
  55. int tmp3;
  56. __asm__ ("\n"
  57. #if defined(__thumb__)
  58. "\t.align 0\n"
  59. "\tbx pc\n"
  60. "\tnop\n"
  61. "\t.arm\n"
  62. #endif
  63. "0:\tldr\t%0,[%3]\n\t"
  64. "add\t%1,%0,%4\n\t"
  65. "swp\t%2,%1,[%3]\n\t"
  66. "cmp\t%0,%2\n\t"
  67. "swpne\t%1,%2,[%3]\n\t"
  68. "bne\t0b"
  69. #if defined(__thumb__)
  70. "\torr %1, pc, #1\n"
  71. "\tbx %1\n"
  72. "\t.force_thumb"
  73. #endif
  74. : "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  75. : "r" (mem), "r"(val)
  76. : "cc", "memory");
  77. }
  78. static inline int
  79. __attribute_used__
  80. compare_and_swap (volatile long int *p, long int oldval, long int newval)
  81. {
  82. int result, tmp;
  83. __asm__ ("\n"
  84. #if defined(__thumb__)
  85. "\t.align 0\n"
  86. "\tbx pc\n"
  87. "\tnop\n"
  88. "\t.arm\n"
  89. #endif
  90. "0:\tldr\t%1,[%2]\n\t"
  91. "mov\t%0,#0\n\t"
  92. "cmp\t%1,%4\n\t"
  93. "bne\t1f\n\t"
  94. "swp\t%0,%3,[%2]\n\t"
  95. "cmp\t%1,%0\n\t"
  96. "swpne\t%1,%0,[%2]\n\t"
  97. "bne\t0b\n\t"
  98. "mov\t%0,#1\n"
  99. "1:"
  100. #if defined(__thumb__)
  101. "\torr %1, pc, #1\n"
  102. "\tbx %1\n"
  103. "\t.force_thumb"
  104. #endif
  105. : "=&r" (result), "=&r" (tmp)
  106. : "r" (p), "r" (newval), "r" (oldval)
  107. : "cc", "memory");
  108. return result;
  109. }
  110. #endif /* atomicity.h */