atomic_gcc.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVUTIL_ATOMIC_GCC_H
  21. #define AVUTIL_ATOMIC_GCC_H
  22. #include <stdint.h>
  23. #include "atomic.h"
  24. #define avpriv_atomic_int_get atomic_int_get_gcc
  25. static inline int atomic_int_get_gcc(volatile int *ptr)
  26. {
  27. #if HAVE_ATOMIC_COMPARE_EXCHANGE
  28. return __atomic_load_n(ptr, __ATOMIC_SEQ_CST);
  29. #else
  30. __sync_synchronize();
  31. return *ptr;
  32. #endif
  33. }
  34. #define avpriv_atomic_int_set atomic_int_set_gcc
  35. static inline void atomic_int_set_gcc(volatile int *ptr, int val)
  36. {
  37. #if HAVE_ATOMIC_COMPARE_EXCHANGE
  38. __atomic_store_n(ptr, val, __ATOMIC_SEQ_CST);
  39. #else
  40. *ptr = val;
  41. __sync_synchronize();
  42. #endif
  43. }
  44. #define avpriv_atomic_int_add_and_fetch atomic_int_add_and_fetch_gcc
  45. static inline int atomic_int_add_and_fetch_gcc(volatile int *ptr, int inc)
  46. {
  47. #if HAVE_ATOMIC_COMPARE_EXCHANGE
  48. return __atomic_add_fetch(ptr, inc, __ATOMIC_SEQ_CST);
  49. #else
  50. return __sync_add_and_fetch(ptr, inc);
  51. #endif
  52. }
  53. #define avpriv_atomic_ptr_cas atomic_ptr_cas_gcc
  54. static inline void *atomic_ptr_cas_gcc(void * volatile *ptr,
  55. void *oldval, void *newval)
  56. {
  57. #if HAVE_SYNC_VAL_COMPARE_AND_SWAP
  58. #ifdef __ARMCC_VERSION
  59. // armcc will throw an error if ptr is not an integer type
  60. volatile uintptr_t *tmp = (volatile uintptr_t*)ptr;
  61. return (void*)__sync_val_compare_and_swap(tmp, oldval, newval);
  62. #else
  63. return __sync_val_compare_and_swap(ptr, oldval, newval);
  64. #endif
  65. #else
  66. __atomic_compare_exchange_n(ptr, &oldval, newval, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
  67. return oldval;
  68. #endif
  69. }
  70. #endif /* AVUTIL_ATOMIC_GCC_H */