stdatomic.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /*
  19. * based on vlc_atomic.h from VLC
  20. * Copyright (C) 2010 Rémi Denis-Courmont
  21. */
  22. #ifndef COMPAT_ATOMICS_GCC_STDATOMIC_H
  23. #define COMPAT_ATOMICS_GCC_STDATOMIC_H
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #define ATOMIC_FLAG_INIT 0
  27. #define ATOMIC_VAR_INIT(value) (value)
  28. #define atomic_init(obj, value) \
  29. do { \
  30. *(obj) = (value); \
  31. } while(0)
  32. #define kill_dependency(y) ((void)0)
  33. #define atomic_thread_fence(order) \
  34. __sync_synchronize()
  35. #define atomic_signal_fence(order) \
  36. ((void)0)
  37. #define atomic_is_lock_free(obj) 0
  38. typedef _Bool atomic_flag;
  39. typedef _Bool atomic_bool;
  40. typedef char atomic_char;
  41. typedef signed char atomic_schar;
  42. typedef unsigned char atomic_uchar;
  43. typedef short atomic_short;
  44. typedef unsigned short atomic_ushort;
  45. typedef int atomic_int;
  46. typedef unsigned int atomic_uint;
  47. typedef long atomic_long;
  48. typedef unsigned long atomic_ulong;
  49. typedef long long atomic_llong;
  50. typedef unsigned long long atomic_ullong;
  51. typedef wchar_t atomic_wchar_t;
  52. typedef int_least8_t atomic_int_least8_t;
  53. typedef uint_least8_t atomic_uint_least8_t;
  54. typedef int_least16_t atomic_int_least16_t;
  55. typedef uint_least16_t atomic_uint_least16_t;
  56. typedef int_least32_t atomic_int_least32_t;
  57. typedef uint_least32_t atomic_uint_least32_t;
  58. typedef int_least64_t atomic_int_least64_t;
  59. typedef uint_least64_t atomic_uint_least64_t;
  60. typedef int_fast8_t atomic_int_fast8_t;
  61. typedef uint_fast8_t atomic_uint_fast8_t;
  62. typedef int_fast16_t atomic_int_fast16_t;
  63. typedef uint_fast16_t atomic_uint_fast16_t;
  64. typedef int_fast32_t atomic_int_fast32_t;
  65. typedef uint_fast32_t atomic_uint_fast32_t;
  66. typedef int_fast64_t atomic_int_fast64_t;
  67. typedef uint_fast64_t atomic_uint_fast64_t;
  68. typedef intptr_t atomic_intptr_t;
  69. typedef uintptr_t atomic_uintptr_t;
  70. typedef size_t atomic_size_t;
  71. typedef ptrdiff_t atomic_ptrdiff_t;
  72. typedef intmax_t atomic_intmax_t;
  73. typedef uintmax_t atomic_uintmax_t;
  74. #define atomic_store(object, desired) \
  75. do { \
  76. *(object) = (desired); \
  77. __sync_synchronize(); \
  78. } while (0)
  79. #define atomic_store_explicit(object, desired, order) \
  80. atomic_store(object, desired)
  81. #define atomic_load(object) \
  82. (__sync_synchronize(), *(object))
  83. #define atomic_load_explicit(object, order) \
  84. atomic_load(object)
  85. #define atomic_exchange(object, desired) \
  86. ({ \
  87. __typeof__(object) _obj = (object); \
  88. __typeof__(*object) _old; \
  89. do \
  90. _old = atomic_load(_obj); \
  91. while (!__sync_bool_compare_and_swap(_obj, _old, (desired))); \
  92. _old; \
  93. })
  94. #define atomic_exchange_explicit(object, desired, order) \
  95. atomic_exchange(object, desired)
  96. #define atomic_compare_exchange_strong(object, expected, desired) \
  97. ({ \
  98. __typeof__(object) _exp = (expected); \
  99. __typeof__(*object) _old = *_exp; \
  100. *_exp = __sync_val_compare_and_swap((object), _old, (desired)); \
  101. *_exp == _old; \
  102. })
  103. #define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) \
  104. atomic_compare_exchange_strong(object, expected, desired)
  105. #define atomic_compare_exchange_weak(object, expected, desired) \
  106. atomic_compare_exchange_strong(object, expected, desired)
  107. #define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) \
  108. atomic_compare_exchange_weak(object, expected, desired)
  109. #define atomic_fetch_add(object, operand) \
  110. __sync_fetch_and_add(object, operand)
  111. #define atomic_fetch_add_explicit(object, operand, order) \
  112. atomic_fetch_add(object, operand)
  113. #define atomic_fetch_sub(object, operand) \
  114. __sync_fetch_and_sub(object, operand)
  115. #define atomic_fetch_sub_explicit(object, operand, order) \
  116. atomic_fetch_sub(object, operand)
  117. #define atomic_fetch_or(object, operand) \
  118. __sync_fetch_and_or(object, operand)
  119. #define atomic_fetch_or_explicit(object, operand, order) \
  120. atomic_fetch_or(object, operand)
  121. #define atomic_fetch_xor(object, operand) \
  122. __sync_fetch_and_xor(object, operand)
  123. #define atomic_fetch_xor_explicit(object, operand, order) \
  124. atomic_fetch_xor(object, operand)
  125. #define atomic_fetch_and(object, operand) \
  126. __sync_fetch_and_and(object, operand)
  127. #define atomic_fetch_and_explicit(object, operand, order) \
  128. atomic_fetch_and(object, operand)
  129. #define atomic_flag_test_and_set(object) \
  130. atomic_exchange(object, 1)
  131. #define atomic_flag_test_and_set_explicit(object, order) \
  132. atomic_flag_test_and_set(object)
  133. #define atomic_flag_clear(object) \
  134. atomic_store(object, 0)
  135. #define atomic_flag_clear_explicit(object, order) \
  136. atomic_flag_clear(object)
  137. #endif /* COMPAT_ATOMICS_GCC_STDATOMIC_H */