atomic.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "atomic.h"
  21. #if !HAVE_ATOMICS_NATIVE
  22. #if HAVE_PTHREADS
  23. #include <pthread.h>
  24. static pthread_mutex_t atomic_lock = PTHREAD_MUTEX_INITIALIZER;
  25. int avpriv_atomic_int_get(volatile int *ptr)
  26. {
  27. int res;
  28. pthread_mutex_lock(&atomic_lock);
  29. res = *ptr;
  30. pthread_mutex_unlock(&atomic_lock);
  31. return res;
  32. }
  33. void avpriv_atomic_int_set(volatile int *ptr, int val)
  34. {
  35. pthread_mutex_lock(&atomic_lock);
  36. *ptr = val;
  37. pthread_mutex_unlock(&atomic_lock);
  38. }
  39. int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
  40. {
  41. int res;
  42. pthread_mutex_lock(&atomic_lock);
  43. *ptr += inc;
  44. res = *ptr;
  45. pthread_mutex_unlock(&atomic_lock);
  46. return res;
  47. }
  48. void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
  49. {
  50. void *ret;
  51. pthread_mutex_lock(&atomic_lock);
  52. ret = *ptr;
  53. if (*ptr == oldval)
  54. *ptr = newval;
  55. pthread_mutex_unlock(&atomic_lock);
  56. return ret;
  57. }
  58. #elif !HAVE_THREADS
  59. int avpriv_atomic_int_get(volatile int *ptr)
  60. {
  61. return *ptr;
  62. }
  63. void avpriv_atomic_int_set(volatile int *ptr, int val)
  64. {
  65. *ptr = val;
  66. }
  67. int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
  68. {
  69. *ptr += inc;
  70. return *ptr;
  71. }
  72. void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
  73. {
  74. if (*ptr == oldval) {
  75. *ptr = newval;
  76. return oldval;
  77. }
  78. return *ptr;
  79. }
  80. #else
  81. #error "Threading is enabled, but there is no implementation of atomic operations available"
  82. #endif /* HAVE_PTHREADS */
  83. #endif /* !HAVE_MEMORYBARRIER && !HAVE_SYNC_VAL_COMPARE_AND_SWAP && !HAVE_MACHINE_RW_BARRIER */
  84. #ifdef TEST
  85. #include "avassert.h"
  86. int main(void)
  87. {
  88. volatile int val = 1;
  89. int res;
  90. res = avpriv_atomic_int_add_and_fetch(&val, 1);
  91. av_assert0(res == 2);
  92. avpriv_atomic_int_set(&val, 3);
  93. res = avpriv_atomic_int_get(&val);
  94. av_assert0(res == 3);
  95. return 0;
  96. }
  97. #endif