atomic.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* NVPTX 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. /* Implement __sync_val_compare_and_swap and __sync_bool_compare_and_swap
  21. for 1 and 2-byte values (which are not natively supported) in terms of
  22. __sync_val_compare_and_swap for 4-byte values (which is supported).
  23. This assumes that the contents of the word surrounding the subword
  24. value that we are interested in are accessible as well (which should
  25. normally be the case). Note that if the contents of the word surrounding
  26. the subword changes between the __sync_val_compare_and_swap_4 and the
  27. preceeding load of oldword, while the subword does not, the implementation
  28. loops, which may manifest worst-case as a hang. */
  29. #define __SYNC_SUBWORD_COMPARE_AND_SWAP(TYPE, SIZE) \
  30. \
  31. TYPE \
  32. __sync_val_compare_and_swap_##SIZE (volatile void *vptr, TYPE oldval, \
  33. TYPE newval) \
  34. { \
  35. volatile TYPE *ptr = vptr; \
  36. volatile unsigned int *wordptr \
  37. = (volatile unsigned int *)((__UINTPTR_TYPE__) ptr & ~3UL); \
  38. int shift = ((__UINTPTR_TYPE__) ptr & 3UL) * 8; \
  39. unsigned int valmask = (1 << (SIZE * 8)) - 1; \
  40. unsigned int wordmask = ~(valmask << shift); \
  41. unsigned int oldword = *wordptr; \
  42. for (;;) \
  43. { \
  44. TYPE prevval = (oldword >> shift) & valmask; \
  45. /* Exit if the subword value previously read from memory is not */ \
  46. /* equal to the expected value OLDVAL. */ \
  47. if (__builtin_expect (prevval != oldval, 0)) \
  48. return prevval; \
  49. unsigned int newword = oldword & wordmask; \
  50. newword |= ((unsigned int) newval) << shift; \
  51. unsigned int prevword \
  52. = __sync_val_compare_and_swap_4 (wordptr, oldword, newword); \
  53. /* Exit only if the compare-and-swap succeeds on the whole word */ \
  54. /* (i.e. the contents of *WORDPTR have not changed since the last */ \
  55. /* memory read). */ \
  56. if (__builtin_expect (prevword == oldword, 1)) \
  57. return oldval; \
  58. oldword = prevword; \
  59. } \
  60. } \
  61. \
  62. bool \
  63. __sync_bool_compare_and_swap_##SIZE (volatile void *ptr, TYPE oldval, \
  64. TYPE newval) \
  65. { \
  66. return __sync_val_compare_and_swap_##SIZE (ptr, oldval, newval) == oldval; \
  67. }
  68. __SYNC_SUBWORD_COMPARE_AND_SWAP (unsigned char, 1)
  69. __SYNC_SUBWORD_COMPARE_AND_SWAP (unsigned short, 2)