atomic.c 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* AMD GCN atomic operations
  2. Copyright (C) 2020-2022 Free Software Foundation, Inc.
  3. Contributed by Mentor Graphics.
  4. This file is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. This file is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include <stdbool.h>
  20. #define __SYNC_SUBWORD_COMPARE_AND_SWAP(TYPE, SIZE) \
  21. \
  22. TYPE \
  23. __sync_val_compare_and_swap_##SIZE (TYPE *ptr, TYPE oldval, TYPE newval) \
  24. { \
  25. unsigned int valmask = (1 << (SIZE * 8)) - 1; \
  26. unsigned int *wordptr = (unsigned int *)((__UINTPTR_TYPE__ ) ptr & ~3UL); \
  27. int shift = ((__UINTPTR_TYPE__ ) ptr & 3UL) * 8; \
  28. unsigned int wordmask = ~(valmask << shift); \
  29. unsigned int oldword = *wordptr; \
  30. for (;;) \
  31. { \
  32. TYPE prevval = (oldword >> shift) & valmask; \
  33. if (__builtin_expect (prevval != oldval, 0)) \
  34. return prevval; \
  35. unsigned int newword = oldword & wordmask; \
  36. newword |= ((unsigned int) newval) << shift; \
  37. unsigned int prevword \
  38. = __sync_val_compare_and_swap_4 (wordptr, oldword, newword); \
  39. if (__builtin_expect (prevword == oldword, 1)) \
  40. return oldval; \
  41. oldword = prevword; \
  42. } \
  43. } \
  44. \
  45. bool \
  46. __sync_bool_compare_and_swap_##SIZE (TYPE *ptr, TYPE oldval, TYPE newval) \
  47. { \
  48. return __sync_val_compare_and_swap_##SIZE (ptr, oldval, newval) == oldval; \
  49. }
  50. __SYNC_SUBWORD_COMPARE_AND_SWAP (unsigned char, 1)
  51. __SYNC_SUBWORD_COMPARE_AND_SWAP (unsigned short, 2)
  52. #define __ATOMIC_COMPARE_EXCHANGE(TYPE,SIZE) \
  53. bool \
  54. __atomic_compare_exchange_##SIZE (TYPE *ptr, TYPE *expected, \
  55. TYPE desired, bool weak, \
  56. int success_memorder, int failure_memorder) \
  57. { \
  58. unsigned int valmask = (1 << (SIZE * 8)) - 1; \
  59. \
  60. unsigned int *wordptr = (unsigned int *)((__UINTPTR_TYPE__ ) ptr & ~3UL); \
  61. int ptrshift = ((__UINTPTR_TYPE__ ) ptr & 3UL) * 8; \
  62. unsigned int wordmask = ~(valmask << ptrshift); \
  63. \
  64. unsigned int ptrword = *wordptr; \
  65. unsigned int exptword = ptrword & wordmask; \
  66. unsigned int newword = ptrword & wordmask; \
  67. exptword |= ((unsigned int) *expected) << ptrshift; \
  68. newword |= ((unsigned int) desired) << ptrshift; \
  69. if (__atomic_compare_exchange_4 (wordptr, &exptword, newword, weak, \
  70. success_memorder, failure_memorder)) \
  71. return true; \
  72. *expected = (TYPE) ((exptword >> ptrshift) & valmask); \
  73. return false; \
  74. }
  75. __ATOMIC_COMPARE_EXCHANGE (unsigned char, 1)
  76. __ATOMIC_COMPARE_EXCHANGE (unsigned short, 2)