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.

229 lines
5.6KB

  1. /*
  2. *
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. 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, 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(ThreadContext *c, int nb_threads)
  121. {
  122. int i, ret;
  123. if (!nb_threads) {
  124. int nb_cpus = av_cpu_count();
  125. // use number of cores + 1 as thread count if there is more than one
  126. if (nb_cpus > 1)
  127. nb_threads = nb_cpus + 1;
  128. else
  129. nb_threads = 1;
  130. }
  131. if (nb_threads <= 1)
  132. return 1;
  133. c->nb_threads = nb_threads;
  134. c->workers = av_mallocz(sizeof(*c->workers) * nb_threads);
  135. if (!c->workers)
  136. return AVERROR(ENOMEM);
  137. c->current_job = 0;
  138. c->nb_jobs = 0;
  139. c->done = 0;
  140. pthread_cond_init(&c->current_job_cond, NULL);
  141. pthread_cond_init(&c->last_job_cond, NULL);
  142. pthread_mutex_init(&c->current_job_lock, NULL);
  143. pthread_mutex_lock(&c->current_job_lock);
  144. for (i = 0; i < nb_threads; i++) {
  145. ret = pthread_create(&c->workers[i], NULL, worker, c);
  146. if (ret) {
  147. pthread_mutex_unlock(&c->current_job_lock);
  148. c->nb_threads = i;
  149. slice_thread_uninit(c);
  150. return AVERROR(ret);
  151. }
  152. }
  153. slice_thread_park_workers(c);
  154. return c->nb_threads;
  155. }
  156. int ff_graph_thread_init(AVFilterGraph *graph)
  157. {
  158. int ret;
  159. #if HAVE_W32THREADS
  160. w32thread_init();
  161. #endif
  162. if (graph->nb_threads == 1) {
  163. graph->thread_type = 0;
  164. return 0;
  165. }
  166. graph->internal->thread = av_mallocz(sizeof(ThreadContext));
  167. if (!graph->internal->thread)
  168. return AVERROR(ENOMEM);
  169. ret = thread_init(graph->internal->thread, graph->nb_threads);
  170. if (ret <= 1) {
  171. av_freep(&graph->internal->thread);
  172. graph->thread_type = 0;
  173. graph->nb_threads = 1;
  174. return (ret < 0) ? ret : 0;
  175. }
  176. graph->nb_threads = ret;
  177. graph->internal->thread_execute = thread_execute;
  178. return 0;
  179. }
  180. void ff_graph_thread_free(AVFilterGraph *graph)
  181. {
  182. if (graph->internal->thread)
  183. slice_thread_uninit(graph->internal->thread);
  184. av_freep(&graph->internal->thread);
  185. }