atomicity.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "0:\tldr\t%0,[%3]\n\t"
  28. "add\t%1,%0,%4\n\t"
  29. "swp\t%2,%1,[%3]\n\t"
  30. "cmp\t%0,%2\n\t"
  31. "swpne\t%1,%2,[%3]\n\t"
  32. "bne\t0b"
  33. : "=&r" (result), "=&r" (tmp1), "=&r" (tmp2)
  34. : "r" (mem), "r"(val)
  35. : "cc", "memory");
  36. return result;
  37. }
  38. static inline void
  39. __attribute_used__
  40. atomic_add (volatile uint32_t *mem, int val)
  41. {
  42. int tmp1;
  43. int tmp2;
  44. int tmp3;
  45. __asm__ ("\n"
  46. "0:\tldr\t%0,[%3]\n\t"
  47. "add\t%1,%0,%4\n\t"
  48. "swp\t%2,%1,[%3]\n\t"
  49. "cmp\t%0,%2\n\t"
  50. "swpne\t%1,%2,[%3]\n\t"
  51. "bne\t0b"
  52. : "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  53. : "r" (mem), "r"(val)
  54. : "cc", "memory");
  55. }
  56. static inline int
  57. __attribute_used__
  58. compare_and_swap (volatile long int *p, long int oldval, long int newval)
  59. {
  60. int result, tmp;
  61. __asm__ ("\n"
  62. "0:\tldr\t%1,[%2]\n\t"
  63. "mov\t%0,#0\n\t"
  64. "cmp\t%1,%4\n\t"
  65. "bne\t1f\n\t"
  66. "swp\t%0,%3,[%2]\n\t"
  67. "cmp\t%1,%0\n\t"
  68. "swpne\t%1,%0,[%2]\n\t"
  69. "bne\t0b\n\t"
  70. "mov\t%0,#1\n"
  71. "1:"
  72. : "=&r" (result), "=&r" (tmp)
  73. : "r" (p), "r" (newval), "r" (oldval)
  74. : "cc", "memory");
  75. return result;
  76. }
  77. #endif /* atomicity.h */