thread.h 5.4 KB

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