thread.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // This header should only be used to simplify code where
  19. // threading is optional, not as a generic threading abstraction.
  20. #ifndef AVUTIL_THREAD_H
  21. #define AVUTIL_THREAD_H
  22. #include "config.h"
  23. #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
  24. #define USE_ATOMICS 0
  25. #if HAVE_PTHREADS
  26. #include <pthread.h>
  27. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
  28. #include "log.h"
  29. #define ASSERT_PTHREAD_NORET(func, ...) do { \
  30. int ret = func(__VA_ARGS__); \
  31. if (ret) { \
  32. av_log(NULL, AV_LOG_FATAL, AV_STRINGIFY(func) \
  33. " failed with error: %s\n", av_err2str(AVERROR(ret))); \
  34. abort(); \
  35. } \
  36. } while (0)
  37. #define ASSERT_PTHREAD(func, ...) do { \
  38. ASSERT_PTHREAD_NORET(func, __VA_ARGS__); \
  39. return 0; \
  40. } while (0)
  41. static inline int strict_pthread_join(pthread_t thread, void **value_ptr)
  42. {
  43. ASSERT_PTHREAD(pthread_join, thread, value_ptr);
  44. }
  45. static inline int strict_pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
  46. {
  47. if (attr) {
  48. ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, attr);
  49. } else {
  50. pthread_mutexattr_t local_attr;
  51. ASSERT_PTHREAD_NORET(pthread_mutexattr_init, &local_attr);
  52. ASSERT_PTHREAD_NORET(pthread_mutexattr_settype, &local_attr, PTHREAD_MUTEX_ERRORCHECK);
  53. ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, &local_attr);
  54. ASSERT_PTHREAD_NORET(pthread_mutexattr_destroy, &local_attr);
  55. }
  56. return 0;
  57. }
  58. static inline int strict_pthread_mutex_destroy(pthread_mutex_t *mutex)
  59. {
  60. ASSERT_PTHREAD(pthread_mutex_destroy, mutex);
  61. }
  62. static inline int strict_pthread_mutex_lock(pthread_mutex_t *mutex)
  63. {
  64. ASSERT_PTHREAD(pthread_mutex_lock, mutex);
  65. }
  66. static inline int strict_pthread_mutex_unlock(pthread_mutex_t *mutex)
  67. {
  68. ASSERT_PTHREAD(pthread_mutex_unlock, mutex);
  69. }
  70. static inline int strict_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  71. {
  72. ASSERT_PTHREAD(pthread_cond_init, cond, attr);
  73. }
  74. static inline int strict_pthread_cond_destroy(pthread_cond_t *cond)
  75. {
  76. ASSERT_PTHREAD(pthread_cond_destroy, cond);
  77. }
  78. static inline int strict_pthread_cond_signal(pthread_cond_t *cond)
  79. {
  80. ASSERT_PTHREAD(pthread_cond_signal, cond);
  81. }
  82. static inline int strict_pthread_cond_broadcast(pthread_cond_t *cond)
  83. {
  84. ASSERT_PTHREAD(pthread_cond_broadcast, cond);
  85. }
  86. static inline int strict_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  87. {
  88. ASSERT_PTHREAD(pthread_cond_wait, cond, mutex);
  89. }
  90. static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
  91. {
  92. ASSERT_PTHREAD(pthread_once, once_control, init_routine);
  93. }
  94. #define pthread_join strict_pthread_join
  95. #define pthread_mutex_init strict_pthread_mutex_init
  96. #define pthread_mutex_destroy strict_pthread_mutex_destroy
  97. #define pthread_mutex_lock strict_pthread_mutex_lock
  98. #define pthread_mutex_unlock strict_pthread_mutex_unlock
  99. #define pthread_cond_init strict_pthread_cond_init
  100. #define pthread_cond_destroy strict_pthread_cond_destroy
  101. #define pthread_cond_signal strict_pthread_cond_signal
  102. #define pthread_cond_broadcast strict_pthread_cond_broadcast
  103. #define pthread_cond_wait strict_pthread_cond_wait
  104. #define pthread_once strict_pthread_once
  105. #endif
  106. #elif HAVE_OS2THREADS
  107. #include "compat/os2threads.h"
  108. #else
  109. #include "compat/w32pthreads.h"
  110. #endif
  111. #define AVMutex pthread_mutex_t
  112. #define ff_mutex_init pthread_mutex_init
  113. #define ff_mutex_lock pthread_mutex_lock
  114. #define ff_mutex_unlock pthread_mutex_unlock
  115. #define ff_mutex_destroy pthread_mutex_destroy
  116. #define AVOnce pthread_once_t
  117. #define AV_ONCE_INIT PTHREAD_ONCE_INIT
  118. #define ff_thread_once(control, routine) pthread_once(control, routine)
  119. #else
  120. #define USE_ATOMICS 1
  121. #define AVMutex char
  122. #define ff_mutex_init(mutex, attr) (0)
  123. #define ff_mutex_lock(mutex) (0)
  124. #define ff_mutex_unlock(mutex) (0)
  125. #define ff_mutex_destroy(mutex) (0)
  126. #define AVOnce char
  127. #define AV_ONCE_INIT 0
  128. static inline int ff_thread_once(char *control, void (*routine)(void))
  129. {
  130. if (!*control) {
  131. routine();
  132. *control = 1;
  133. }
  134. return 0;
  135. }
  136. #endif
  137. #endif /* AVUTIL_THREAD_H */