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.

230 lines
5.7KB

  1. /*
  2. *
  3. * This file is part of Libav.
  4. *
  5. * Libav is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * Libav is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with Libav; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file
  21. * Libavfilter multithreading support
  22. */
  23. #include "config.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/cpu.h"
  26. #include "libavutil/mem.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "thread.h"
  30. #if HAVE_PTHREADS
  31. #include <pthread.h>
  32. #elif HAVE_W32THREADS
  33. #include "compat/w32pthreads.h"
  34. #endif
  35. typedef struct ThreadContext {
  36. AVFilterGraph *graph;
  37. int nb_threads;
  38. pthread_t *workers;
  39. avfilter_action_func *func;
  40. /* per-execute perameters */
  41. AVFilterContext *ctx;
  42. void *arg;
  43. int *rets;
  44. int nb_rets;
  45. int nb_jobs;
  46. pthread_cond_t last_job_cond;
  47. pthread_cond_t current_job_cond;
  48. pthread_mutex_t current_job_lock;
  49. int current_job;
  50. int done;
  51. } ThreadContext;
  52. static void* attribute_align_arg worker(void *v)
  53. {
  54. ThreadContext *c = v;
  55. int our_job = c->nb_jobs;
  56. int nb_threads = c->nb_threads;
  57. int self_id;
  58. pthread_mutex_lock(&c->current_job_lock);
  59. self_id = c->current_job++;
  60. for (;;) {
  61. while (our_job >= c->nb_jobs) {
  62. if (c->current_job == nb_threads + c->nb_jobs)
  63. pthread_cond_signal(&c->last_job_cond);
  64. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  65. our_job = self_id;
  66. if (c->done) {
  67. pthread_mutex_unlock(&c->current_job_lock);
  68. return NULL;
  69. }
  70. }
  71. pthread_mutex_unlock(&c->current_job_lock);
  72. c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
  73. pthread_mutex_lock(&c->current_job_lock);
  74. our_job = c->current_job++;
  75. }
  76. }
  77. static void slice_thread_uninit(ThreadContext *c)
  78. {
  79. int i;
  80. pthread_mutex_lock(&c->current_job_lock);
  81. c->done = 1;
  82. pthread_cond_broadcast(&c->current_job_cond);
  83. pthread_mutex_unlock(&c->current_job_lock);
  84. for (i = 0; i < c->nb_threads; i++)
  85. pthread_join(c->workers[i], NULL);
  86. pthread_mutex_destroy(&c->current_job_lock);
  87. pthread_cond_destroy(&c->current_job_cond);
  88. pthread_cond_destroy(&c->last_job_cond);
  89. av_freep(&c->workers);
  90. }
  91. static void slice_thread_park_workers(ThreadContext *c)
  92. {
  93. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  94. pthread_mutex_unlock(&c->current_job_lock);
  95. }
  96. static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
  97. void *arg, int *ret, int nb_jobs)
  98. {
  99. ThreadContext *c = ctx->graph->internal->thread;
  100. int dummy_ret;
  101. if (nb_jobs <= 0)
  102. return 0;
  103. pthread_mutex_lock(&c->current_job_lock);
  104. c->current_job = c->nb_threads;
  105. c->nb_jobs = nb_jobs;
  106. c->ctx = ctx;
  107. c->arg = arg;
  108. c->func = func;
  109. if (ret) {
  110. c->rets = ret;
  111. c->nb_rets = nb_jobs;
  112. } else {
  113. c->rets = &dummy_ret;
  114. c->nb_rets = 1;
  115. }
  116. pthread_cond_broadcast(&c->current_job_cond);
  117. slice_thread_park_workers(c);
  118. return 0;
  119. }
  120. static int thread_init_internal(ThreadContext *c, int nb_threads)
  121. {
  122. int i, ret;
  123. if (!nb_threads) {
  124. int nb_cpus = av_cpu_count();
  125. av_log(c->graph, AV_LOG_DEBUG, "Detected %d logical cores.\n", nb_cpus);
  126. // use number of cores + 1 as thread count if there is more than one
  127. if (nb_cpus > 1)
  128. nb_threads = nb_cpus + 1;
  129. else
  130. nb_threads = 1;
  131. }
  132. if (nb_threads <= 1)
  133. return 1;
  134. c->nb_threads = nb_threads;
  135. c->workers = av_mallocz(sizeof(*c->workers) * nb_threads);
  136. if (!c->workers)
  137. return AVERROR(ENOMEM);
  138. c->current_job = 0;
  139. c->nb_jobs = 0;
  140. c->done = 0;
  141. pthread_cond_init(&c->current_job_cond, NULL);
  142. pthread_cond_init(&c->last_job_cond, NULL);
  143. pthread_mutex_init(&c->current_job_lock, NULL);
  144. pthread_mutex_lock(&c->current_job_lock);
  145. for (i = 0; i < nb_threads; i++) {
  146. ret = pthread_create(&c->workers[i], NULL, worker, c);
  147. if (ret) {
  148. pthread_mutex_unlock(&c->current_job_lock);
  149. c->nb_threads = i;
  150. slice_thread_uninit(c);
  151. return AVERROR(ret);
  152. }
  153. }
  154. slice_thread_park_workers(c);
  155. return c->nb_threads;
  156. }
  157. int ff_graph_thread_init(AVFilterGraph *graph)
  158. {
  159. int ret;
  160. #if HAVE_W32THREADS
  161. w32thread_init();
  162. #endif
  163. if (graph->nb_threads == 1) {
  164. graph->thread_type = 0;
  165. return 0;
  166. }
  167. graph->internal->thread = av_mallocz(sizeof(ThreadContext));
  168. if (!graph->internal->thread)
  169. return AVERROR(ENOMEM);
  170. ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
  171. if (ret <= 1) {
  172. av_freep(&graph->internal->thread);
  173. graph->thread_type = 0;
  174. graph->nb_threads = 1;
  175. return (ret < 0) ? ret : 0;
  176. }
  177. graph->nb_threads = ret;
  178. graph->internal->thread_execute = thread_execute;
  179. return 0;
  180. }
  181. void ff_graph_thread_free(AVFilterGraph *graph)
  182. {
  183. if (graph->internal->thread)
  184. slice_thread_uninit(graph->internal->thread);
  185. av_freep(&graph->internal->thread);
  186. }