atomic.c 2.8 KB

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