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.

231 lines
5.8KB

  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. if (!c->done)
  65. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  66. our_job = self_id;
  67. if (c->done) {
  68. pthread_mutex_unlock(&c->current_job_lock);
  69. return NULL;
  70. }
  71. }
  72. pthread_mutex_unlock(&c->current_job_lock);
  73. c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
  74. pthread_mutex_lock(&c->current_job_lock);
  75. our_job = c->current_job++;
  76. }
  77. }
  78. static void slice_thread_uninit(ThreadContext *c)
  79. {
  80. int i;
  81. pthread_mutex_lock(&c->current_job_lock);
  82. c->done = 1;
  83. pthread_cond_broadcast(&c->current_job_cond);
  84. pthread_mutex_unlock(&c->current_job_lock);
  85. for (i = 0; i < c->nb_threads; i++)
  86. pthread_join(c->workers[i], NULL);
  87. pthread_mutex_destroy(&c->current_job_lock);
  88. pthread_cond_destroy(&c->current_job_cond);
  89. pthread_cond_destroy(&c->last_job_cond);
  90. av_freep(&c->workers);
  91. }
  92. static void slice_thread_park_workers(ThreadContext *c)
  93. {
  94. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  95. pthread_mutex_unlock(&c->current_job_lock);
  96. }
  97. static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
  98. void *arg, int *ret, int nb_jobs)
  99. {
  100. ThreadContext *c = ctx->graph->internal->thread;
  101. int dummy_ret;
  102. if (nb_jobs <= 0)
  103. return 0;
  104. pthread_mutex_lock(&c->current_job_lock);
  105. c->current_job = c->nb_threads;
  106. c->nb_jobs = nb_jobs;
  107. c->ctx = ctx;
  108. c->arg = arg;
  109. c->func = func;
  110. if (ret) {
  111. c->rets = ret;
  112. c->nb_rets = nb_jobs;
  113. } else {
  114. c->rets = &dummy_ret;
  115. c->nb_rets = 1;
  116. }
  117. pthread_cond_broadcast(&c->current_job_cond);
  118. slice_thread_park_workers(c);
  119. return 0;
  120. }
  121. static int thread_init_internal(ThreadContext *c, int nb_threads)
  122. {
  123. int i, ret;
  124. if (!nb_threads) {
  125. int nb_cpus = av_cpu_count();
  126. av_log(c->graph, AV_LOG_DEBUG, "Detected %d logical cores.\n", nb_cpus);
  127. // use number of cores + 1 as thread count if there is more than one
  128. if (nb_cpus > 1)
  129. nb_threads = nb_cpus + 1;
  130. else
  131. nb_threads = 1;
  132. }
  133. if (nb_threads <= 1)
  134. return 1;
  135. c->nb_threads = nb_threads;
  136. c->workers = av_mallocz(sizeof(*c->workers) * nb_threads);
  137. if (!c->workers)
  138. return AVERROR(ENOMEM);
  139. c->current_job = 0;
  140. c->nb_jobs = 0;
  141. c->done = 0;
  142. pthread_cond_init(&c->current_job_cond, NULL);
  143. pthread_cond_init(&c->last_job_cond, NULL);
  144. pthread_mutex_init(&c->current_job_lock, NULL);
  145. pthread_mutex_lock(&c->current_job_lock);
  146. for (i = 0; i < nb_threads; i++) {
  147. ret = pthread_create(&c->workers[i], NULL, worker, c);
  148. if (ret) {
  149. pthread_mutex_unlock(&c->current_job_lock);
  150. c->nb_threads = i;
  151. slice_thread_uninit(c);
  152. return AVERROR(ret);
  153. }
  154. }
  155. slice_thread_park_workers(c);
  156. return c->nb_threads;
  157. }
  158. int ff_graph_thread_init(AVFilterGraph *graph)
  159. {
  160. int ret;
  161. #if HAVE_W32THREADS
  162. w32thread_init();
  163. #endif
  164. if (graph->nb_threads == 1) {
  165. graph->thread_type = 0;
  166. return 0;
  167. }
  168. graph->internal->thread = av_mallocz(sizeof(ThreadContext));
  169. if (!graph->internal->thread)
  170. return AVERROR(ENOMEM);
  171. ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
  172. if (ret <= 1) {
  173. av_freep(&graph->internal->thread);
  174. graph->thread_type = 0;
  175. graph->nb_threads = 1;
  176. return (ret < 0) ? ret : 0;
  177. }
  178. graph->nb_threads = ret;
  179. graph->internal->thread_execute = thread_execute;
  180. return 0;
  181. }
  182. void ff_graph_thread_free(AVFilterGraph *graph)
  183. {
  184. if (graph->internal->thread)
  185. slice_thread_uninit(graph->internal->thread);
  186. av_freep(&graph->internal->thread);
  187. }