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.

225 lines
6.4KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Slice multithreading support functions
  21. * @see doc/multithreading.txt
  22. */
  23. #include "config.h"
  24. #if HAVE_PTHREADS
  25. #include <pthread.h>
  26. #elif HAVE_W32THREADS
  27. #include "compat/w32pthreads.h"
  28. #endif
  29. #include "avcodec.h"
  30. #include "internal.h"
  31. #include "pthread_internal.h"
  32. #include "thread.h"
  33. #include "libavutil/common.h"
  34. #include "libavutil/cpu.h"
  35. #include "libavutil/mem.h"
  36. typedef int (action_func)(AVCodecContext *c, void *arg);
  37. typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
  38. typedef struct SliceThreadContext {
  39. pthread_t *workers;
  40. action_func *func;
  41. action_func2 *func2;
  42. void *args;
  43. int *rets;
  44. int rets_count;
  45. int job_count;
  46. int job_size;
  47. pthread_cond_t last_job_cond;
  48. pthread_cond_t current_job_cond;
  49. pthread_mutex_t current_job_lock;
  50. unsigned current_execute;
  51. int current_job;
  52. int done;
  53. } SliceThreadContext;
  54. static void* attribute_align_arg worker(void *v)
  55. {
  56. AVCodecContext *avctx = v;
  57. SliceThreadContext *c = avctx->internal->thread_ctx;
  58. unsigned last_execute = 0;
  59. int our_job = c->job_count;
  60. int thread_count = avctx->thread_count;
  61. int self_id;
  62. pthread_mutex_lock(&c->current_job_lock);
  63. self_id = c->current_job++;
  64. for (;;){
  65. while (our_job >= c->job_count) {
  66. if (c->current_job == thread_count + c->job_count)
  67. pthread_cond_signal(&c->last_job_cond);
  68. while (last_execute == c->current_execute && !c->done)
  69. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  70. last_execute = c->current_execute;
  71. our_job = self_id;
  72. if (c->done) {
  73. pthread_mutex_unlock(&c->current_job_lock);
  74. return NULL;
  75. }
  76. }
  77. pthread_mutex_unlock(&c->current_job_lock);
  78. c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
  79. c->func2(avctx, c->args, our_job, self_id);
  80. pthread_mutex_lock(&c->current_job_lock);
  81. our_job = c->current_job++;
  82. }
  83. }
  84. void ff_slice_thread_free(AVCodecContext *avctx)
  85. {
  86. SliceThreadContext *c = avctx->internal->thread_ctx;
  87. int i;
  88. pthread_mutex_lock(&c->current_job_lock);
  89. c->done = 1;
  90. pthread_cond_broadcast(&c->current_job_cond);
  91. pthread_mutex_unlock(&c->current_job_lock);
  92. for (i=0; i<avctx->thread_count; i++)
  93. pthread_join(c->workers[i], NULL);
  94. pthread_mutex_destroy(&c->current_job_lock);
  95. pthread_cond_destroy(&c->current_job_cond);
  96. pthread_cond_destroy(&c->last_job_cond);
  97. av_free(c->workers);
  98. av_freep(&avctx->internal->thread_ctx);
  99. }
  100. static av_always_inline void thread_park_workers(SliceThreadContext *c, int thread_count)
  101. {
  102. while (c->current_job != thread_count + c->job_count)
  103. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  104. pthread_mutex_unlock(&c->current_job_lock);
  105. }
  106. static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
  107. {
  108. SliceThreadContext *c = avctx->internal->thread_ctx;
  109. int dummy_ret;
  110. if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
  111. return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
  112. if (job_count <= 0)
  113. return 0;
  114. pthread_mutex_lock(&c->current_job_lock);
  115. c->current_job = avctx->thread_count;
  116. c->job_count = job_count;
  117. c->job_size = job_size;
  118. c->args = arg;
  119. c->func = func;
  120. if (ret) {
  121. c->rets = ret;
  122. c->rets_count = job_count;
  123. } else {
  124. c->rets = &dummy_ret;
  125. c->rets_count = 1;
  126. }
  127. c->current_execute++;
  128. pthread_cond_broadcast(&c->current_job_cond);
  129. thread_park_workers(c, avctx->thread_count);
  130. return 0;
  131. }
  132. static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
  133. {
  134. SliceThreadContext *c = avctx->internal->thread_ctx;
  135. c->func2 = func2;
  136. return thread_execute(avctx, NULL, arg, ret, job_count, 0);
  137. }
  138. int ff_slice_thread_init(AVCodecContext *avctx)
  139. {
  140. int i;
  141. SliceThreadContext *c;
  142. int thread_count = avctx->thread_count;
  143. #if HAVE_W32THREADS
  144. w32thread_init();
  145. #endif
  146. if (!thread_count) {
  147. int nb_cpus = av_cpu_count();
  148. av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
  149. // use number of cores + 1 as thread count if there is more than one
  150. if (nb_cpus > 1)
  151. thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
  152. else
  153. thread_count = avctx->thread_count = 1;
  154. }
  155. if (thread_count <= 1) {
  156. avctx->active_thread_type = 0;
  157. return 0;
  158. }
  159. c = av_mallocz(sizeof(SliceThreadContext));
  160. if (!c)
  161. return -1;
  162. c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
  163. if (!c->workers) {
  164. av_free(c);
  165. return -1;
  166. }
  167. avctx->internal->thread_ctx = c;
  168. c->current_job = 0;
  169. c->job_count = 0;
  170. c->job_size = 0;
  171. c->done = 0;
  172. pthread_cond_init(&c->current_job_cond, NULL);
  173. pthread_cond_init(&c->last_job_cond, NULL);
  174. pthread_mutex_init(&c->current_job_lock, NULL);
  175. pthread_mutex_lock(&c->current_job_lock);
  176. for (i=0; i<thread_count; i++) {
  177. if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
  178. avctx->thread_count = i;
  179. pthread_mutex_unlock(&c->current_job_lock);
  180. ff_thread_free(avctx);
  181. return -1;
  182. }
  183. }
  184. thread_park_workers(c, thread_count);
  185. avctx->execute = thread_execute;
  186. avctx->execute2 = thread_execute2;
  187. return 0;
  188. }