atomicity.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Low-level functions for atomic operations. Alpha version.
  2. Copyright (C) 1999 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__ ((unused))
  21. exchange_and_add (volatile uint32_t *mem, int val)
  22. {
  23. register int result, tmp;
  24. __asm__ __volatile__ (
  25. "/* Inline exchange & add */\n"
  26. "1:\t"
  27. "ldl_l %0,%3\n\t"
  28. "addl %0,%4,%1\n\t"
  29. "stl_c %1,%2\n\t"
  30. "beq %1,2f\n"
  31. ".subsection 1\n"
  32. "2:\t"
  33. "br 1b\n"
  34. ".previous\n\t"
  35. "mb\n\t"
  36. "/* End exchange & add */"
  37. : "=&r"(result), "=&r"(tmp), "=m"(*mem)
  38. : "m" (*mem), "r"(val));
  39. return result;
  40. }
  41. static inline void
  42. __attribute__ ((unused))
  43. atomic_add (volatile uint32_t *mem, int val)
  44. {
  45. register int result;
  46. __asm__ __volatile__ (
  47. "/* Inline exchange & add */\n"
  48. "1:\t"
  49. "ldl_l %0,%2\n\t"
  50. "addl %0,%3,%0\n\t"
  51. "stl_c %0,%1\n\t"
  52. "beq %0,2f\n\t"
  53. ".subsection 1\n"
  54. "2:\t"
  55. "br 1b\n"
  56. ".previous\n\t"
  57. "mb\n\t"
  58. "/* End exchange & add */"
  59. : "=&r"(result), "=m"(*mem)
  60. : "m" (*mem), "r"(val));
  61. }
  62. static inline long
  63. __attribute__ ((unused))
  64. compare_and_swap (volatile long int *p, long int oldval, long int newval)
  65. {
  66. long int ret;
  67. __asm__ __volatile__ (
  68. "/* Inline compare & swap */\n"
  69. "1:\t"
  70. "ldq_l %0,%4\n\t"
  71. "cmpeq %0,%2,%0\n\t"
  72. "beq %0,3f\n\t"
  73. "mov %3,%0\n\t"
  74. "stq_c %0,%1\n\t"
  75. "beq %0,2f\n\t"
  76. ".subsection 1\n"
  77. "2:\t"
  78. "br 1b\n"
  79. ".previous\n\t"
  80. "3:\t"
  81. "mb\n\t"
  82. "/* End compare & swap */"
  83. : "=&r"(ret), "=m"(*p)
  84. : "r"(oldval), "r"(newval), "m"(*p));
  85. return ret;
  86. }
  87. #endif /* atomicity.h */