You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.2KB

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