atomicity.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Low-level functions for atomic operations. Sparc32 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 int
  20. __attribute_used__
  21. exchange_and_add (volatile uint32_t *mem, int val)
  22. {
  23. static unsigned char lock;
  24. int result, tmp;
  25. __asm__ __volatile__("1: ldstub [%1], %0\n\t"
  26. " cmp %0, 0\n\t"
  27. " bne 1b\n\t"
  28. " nop"
  29. : "=&r" (tmp)
  30. : "r" (&lock)
  31. : "memory");
  32. result = *mem;
  33. *mem += val;
  34. __asm__ __volatile__("stb %%g0, [%0]"
  35. : /* no outputs */
  36. : "r" (&lock)
  37. : "memory");
  38. return result;
  39. }
  40. static void
  41. __attribute_used__
  42. atomic_add (volatile uint32_t *mem, int val)
  43. {
  44. static unsigned char lock;
  45. int tmp;
  46. __asm__ __volatile__("1: ldstub [%1], %0\n\t"
  47. " cmp %0, 0\n\t"
  48. " bne 1b\n\t"
  49. " nop"
  50. : "=&r" (tmp)
  51. : "r" (&lock)
  52. : "memory");
  53. *mem += val;
  54. __asm__ __volatile__("stb %%g0, [%0]"
  55. : /* no outputs */
  56. : "r" (&lock)
  57. : "memory");
  58. }
  59. static int
  60. __attribute_used__
  61. compare_and_swap (volatile long int *p, long int oldval, long int newval)
  62. {
  63. static unsigned char lock;
  64. int ret, tmp;
  65. __asm__ __volatile__("1: ldstub [%1], %0\n\t"
  66. " cmp %0, 0\n\t"
  67. " bne 1b\n\t"
  68. " nop"
  69. : "=&r" (tmp)
  70. : "r" (&lock)
  71. : "memory");
  72. if (*p != oldval)
  73. ret = 0;
  74. else
  75. {
  76. *p = newval;
  77. ret = 1;
  78. }
  79. __asm__ __volatile__("stb %%g0, [%0]"
  80. : /* no outputs */
  81. : "r" (&lock)
  82. : "memory");
  83. return ret;
  84. }
  85. #endif /* atomicity.h */