atomic_wide_counter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Monotonically increasing wide counters (at least 62 bits).
  2. Copyright (C) 2016-2026 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, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _ATOMIC_WIDE_COUNTER_H
  16. #define _ATOMIC_WIDE_COUNTER_H
  17. #include <atomic.h>
  18. #include <bits/atomic_wide_counter.h>
  19. #if HAVE_64B_ATOMICS
  20. static inline uint64_t
  21. __atomic_wide_counter_load_relaxed (__atomic_wide_counter *c)
  22. {
  23. return atomic_load_relaxed (&c->__value64);
  24. }
  25. static inline uint64_t
  26. __atomic_wide_counter_load_acquire (__atomic_wide_counter *c)
  27. {
  28. return atomic_load_acquire (&c->__value64);
  29. }
  30. static inline uint64_t
  31. __atomic_wide_counter_fetch_add_relaxed (__atomic_wide_counter *c,
  32. unsigned int val)
  33. {
  34. return atomic_fetch_add_relaxed (&c->__value64, val);
  35. }
  36. static inline uint64_t
  37. __atomic_wide_counter_fetch_add_acquire (__atomic_wide_counter *c,
  38. unsigned int val)
  39. {
  40. return atomic_fetch_add_acquire (&c->__value64, val);
  41. }
  42. static inline void
  43. __atomic_wide_counter_add_relaxed (__atomic_wide_counter *c,
  44. unsigned int val)
  45. {
  46. atomic_store_relaxed (&c->__value64,
  47. atomic_load_relaxed (&c->__value64) + val);
  48. }
  49. static uint64_t __attribute__ ((unused))
  50. __atomic_wide_counter_fetch_xor_release (__atomic_wide_counter *c,
  51. unsigned int val)
  52. {
  53. return atomic_fetch_xor_release (&c->__value64, val);
  54. }
  55. #else /* !HAVE_64B_ATOMICS */
  56. uint64_t __atomic_wide_counter_load_relaxed (__atomic_wide_counter *c)
  57. attribute_hidden;
  58. static inline uint64_t
  59. __atomic_wide_counter_load_acquire (__atomic_wide_counter *c)
  60. {
  61. uint64_t r = __atomic_wide_counter_load_relaxed (c);
  62. atomic_thread_fence_acquire ();
  63. return r;
  64. }
  65. uint64_t __atomic_wide_counter_fetch_add_relaxed (__atomic_wide_counter *c,
  66. unsigned int op)
  67. attribute_hidden;
  68. static inline uint64_t
  69. __atomic_wide_counter_fetch_add_acquire (__atomic_wide_counter *c,
  70. unsigned int val)
  71. {
  72. uint64_t r = __atomic_wide_counter_fetch_add_relaxed (c, val);
  73. atomic_thread_fence_acquire ();
  74. return r;
  75. }
  76. static inline void
  77. __atomic_wide_counter_add_relaxed (__atomic_wide_counter *c,
  78. unsigned int val)
  79. {
  80. __atomic_wide_counter_fetch_add_relaxed (c, val);
  81. }
  82. #endif /* !HAVE_64B_ATOMICS */
  83. #endif /* _ATOMIC_WIDE_COUNTER_H */