atomicity.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Low-level functions for atomic operations. Mips version.
  2. Copyright (C) 2001, 2002 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 _MIPS_ATOMICITY_H
  17. #define _MIPS_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 result, tmp;
  24. __asm__ __volatile__
  25. ("/* Inline exchange & add */\n"
  26. "1:\n\t"
  27. ".set push\n\t"
  28. ".set mips2\n\t"
  29. "ll %0,%3\n\t"
  30. "addu %1,%4,%0\n\t"
  31. "sc %1,%2\n\t"
  32. ".set pop\n\t"
  33. "beqz %1,1b\n\t"
  34. "/* End exchange & add */"
  35. : "=&r"(result), "=&r"(tmp), "=m"(*mem)
  36. : "m" (*mem), "r"(val)
  37. : "memory");
  38. return result;
  39. }
  40. static inline void
  41. __attribute_used__
  42. atomic_add (volatile uint32_t *mem, int val)
  43. {
  44. int result;
  45. __asm__ __volatile__
  46. ("/* Inline atomic add */\n"
  47. "1:\n\t"
  48. ".set push\n\t"
  49. ".set mips2\n\t"
  50. "ll %0,%2\n\t"
  51. "addu %0,%3,%0\n\t"
  52. "sc %0,%1\n\t"
  53. ".set pop\n\t"
  54. "beqz %0,1b\n\t"
  55. "/* End atomic add */"
  56. : "=&r"(result), "=m"(*mem)
  57. : "m" (*mem), "r"(val)
  58. : "memory");
  59. }
  60. static inline int
  61. __attribute_used__
  62. compare_and_swap (volatile long int *p, long int oldval, long int newval)
  63. {
  64. long int ret, temp;
  65. __asm__ __volatile__
  66. ("/* Inline compare & swap */\n"
  67. "1:\n\t"
  68. ".set push\n\t"
  69. ".set mips2\n\t"
  70. "ll %1,%5\n\t"
  71. "move %0,$0\n\t"
  72. "bne %1,%3,2f\n\t"
  73. "move %0,%4\n\t"
  74. "sc %0,%2\n\t"
  75. ".set pop\n\t"
  76. "beqz %0,1b\n"
  77. "2:\n\t"
  78. "/* End compare & swap */"
  79. : "=&r" (ret), "=&r" (temp), "=m" (*p)
  80. : "r" (oldval), "r" (newval), "m" (*p)
  81. : "memory");
  82. return ret;
  83. }
  84. #endif /* atomicity.h */