thread.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #if HAVE_PTHREADS
  25. #include <pthread.h>
  26. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
  27. #include "log.h"
  28. #define ASSERT_PTHREAD_ABORT(func, ret) do { \
  29. char errbuf[AV_ERROR_MAX_STRING_SIZE] = ""; \
  30. av_log(NULL, AV_LOG_FATAL, AV_STRINGIFY(func) \
  31. " failed with error: %s\n", \
  32. av_make_error_string(errbuf, AV_ERROR_MAX_STRING_SIZE, \
  33. AVERROR(ret))); \
  34. abort(); \
  35. } while (0)
  36. #define ASSERT_PTHREAD_NORET(func, ...) do { \
  37. int ret = func(__VA_ARGS__); \
  38. if (ret) \
  39. ASSERT_PTHREAD_ABORT(func, ret); \
  40. } while (0)
  41. #define ASSERT_PTHREAD(func, ...) do { \
  42. ASSERT_PTHREAD_NORET(func, __VA_ARGS__); \
  43. return 0; \
  44. } while (0)
  45. static inline int strict_pthread_join(pthread_t thread, void **value_ptr)
  46. {
  47. ASSERT_PTHREAD(pthread_join, thread, value_ptr);
  48. }
  49. static inline int strict_pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
  50. {
  51. if (attr) {
  52. ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, attr);
  53. } else {
  54. pthread_mutexattr_t local_attr;
  55. ASSERT_PTHREAD_NORET(pthread_mutexattr_init, &local_attr);
  56. ASSERT_PTHREAD_NORET(pthread_mutexattr_settype, &local_attr, PTHREAD_MUTEX_ERRORCHECK);
  57. ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, &local_attr);
  58. ASSERT_PTHREAD_NORET(pthread_mutexattr_destroy, &local_attr);
  59. }
  60. return 0;
  61. }
  62. static inline int strict_pthread_mutex_destroy(pthread_mutex_t *mutex)
  63. {
  64. ASSERT_PTHREAD(pthread_mutex_destroy, mutex);
  65. }
  66. static inline int strict_pthread_mutex_lock(pthread_mutex_t *mutex)
  67. {
  68. ASSERT_PTHREAD(pthread_mutex_lock, mutex);
  69. }
  70. static inline int strict_pthread_mutex_unlock(pthread_mutex_t *mutex)
  71. {
  72. ASSERT_PTHREAD(pthread_mutex_unlock, mutex);
  73. }
  74. static inline int strict_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  75. {
  76. ASSERT_PTHREAD(pthread_cond_init, cond, attr);
  77. }
  78. static inline int strict_pthread_cond_destroy(pthread_cond_t *cond)
  79. {
  80. ASSERT_PTHREAD(pthread_cond_destroy, cond);
  81. }
  82. static inline int strict_pthread_cond_signal(pthread_cond_t *cond)
  83. {
  84. ASSERT_PTHREAD(pthread_cond_signal, cond);
  85. }
  86. static inline int strict_pthread_cond_broadcast(pthread_cond_t *cond)
  87. {
  88. ASSERT_PTHREAD(pthread_cond_broadcast, cond);
  89. }
  90. static inline int strict_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  91. {
  92. ASSERT_PTHREAD(pthread_cond_wait, cond, mutex);
  93. }
  94. static inline int strict_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  95. const struct timespec *abstime)
  96. {
  97. int ret = pthread_cond_timedwait(cond, mutex, abstime);
  98. if (ret && ret != ETIMEDOUT)
  99. ASSERT_PTHREAD_ABORT(pthread_cond_timedwait, ret);
  100. return ret;
  101. }
  102. static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
  103. {
  104. ASSERT_PTHREAD(pthread_once, once_control, init_routine);
  105. }
  106. #define pthread_join strict_pthread_join
  107. #define pthread_mutex_init strict_pthread_mutex_init
  108. #define pthread_mutex_destroy strict_pthread_mutex_destroy
  109. #define pthread_mutex_lock strict_pthread_mutex_lock
  110. #define pthread_mutex_unlock strict_pthread_mutex_unlock
  111. #define pthread_cond_init strict_pthread_cond_init
  112. #define pthread_cond_destroy strict_pthread_cond_destroy
  113. #define pthread_cond_signal strict_pthread_cond_signal
  114. #define pthread_cond_broadcast strict_pthread_cond_broadcast
  115. #define pthread_cond_wait strict_pthread_cond_wait
  116. #define pthread_cond_timedwait strict_pthread_cond_timedwait
  117. #define pthread_once strict_pthread_once
  118. #endif
  119. #elif HAVE_OS2THREADS
  120. #include "compat/os2threads.h"
  121. #else
  122. #include "compat/w32pthreads.h"
  123. #endif
  124. #define AVMutex pthread_mutex_t
  125. #define AV_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  126. #define ff_mutex_init pthread_mutex_init
  127. #define ff_mutex_lock pthread_mutex_lock
  128. #define ff_mutex_unlock pthread_mutex_unlock
  129. #define ff_mutex_destroy pthread_mutex_destroy
  130. #define AVOnce pthread_once_t
  131. #define AV_ONCE_INIT PTHREAD_ONCE_INIT
  132. #define ff_thread_once(control, routine) pthread_once(control, routine)
  133. #else
  134. #define AVMutex char
  135. #define AV_MUTEX_INITIALIZER 0
  136. static inline int ff_mutex_init(AVMutex *mutex, const void *attr){ return 0; }
  137. static inline int ff_mutex_lock(AVMutex *mutex){ return 0; }
  138. static inline int ff_mutex_unlock(AVMutex *mutex){ return 0; }
  139. static inline int ff_mutex_destroy(AVMutex *mutex){ return 0; }
  140. #define AVOnce char
  141. #define AV_ONCE_INIT 0
  142. static inline int ff_thread_once(char *control, void (*routine)(void))
  143. {
  144. if (!*control) {
  145. routine();
  146. *control = 1;
  147. }
  148. return 0;
  149. }
  150. #endif
  151. #endif /* AVUTIL_THREAD_H */