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.7KB

  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_OS2THREADS
  33. #include "compat/os2threads.h"
  34. #elif HAVE_W32THREADS
  35. #include "compat/w32pthreads.h"
  36. #endif
  37. typedef struct ThreadContext {
  38. AVFilterGraph *graph;
  39. int nb_threads;
  40. pthread_t *workers;
  41. action_func *func;
  42. /* per-execute perameters */
  43. AVFilterContext *ctx;
  44. void *arg;
  45. int *rets;
  46. int nb_rets;
  47. int nb_jobs;
  48. pthread_cond_t last_job_cond;
  49. pthread_cond_t current_job_cond;
  50. pthread_mutex_t current_job_lock;
  51. int current_job;
  52. int done;
  53. } ThreadContext;
  54. static void* attribute_align_arg worker(void *v)
  55. {
  56. ThreadContext *c = v;
  57. int our_job = c->nb_jobs;
  58. int nb_threads = c->nb_threads;
  59. int self_id;
  60. pthread_mutex_lock(&c->current_job_lock);
  61. self_id = c->current_job++;
  62. for (;;) {
  63. while (our_job >= c->nb_jobs) {
  64. if (c->current_job == nb_threads + c->nb_jobs)
  65. pthread_cond_signal(&c->last_job_cond);
  66. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  67. our_job = self_id;
  68. if (c->done) {
  69. pthread_mutex_unlock(&c->current_job_lock);
  70. return NULL;
  71. }
  72. }
  73. pthread_mutex_unlock(&c->current_job_lock);
  74. c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
  75. pthread_mutex_lock(&c->current_job_lock);
  76. our_job = c->current_job++;
  77. }
  78. }
  79. static void slice_thread_uninit(ThreadContext *c)
  80. {
  81. int i;
  82. pthread_mutex_lock(&c->current_job_lock);
  83. c->done = 1;
  84. pthread_cond_broadcast(&c->current_job_cond);
  85. pthread_mutex_unlock(&c->current_job_lock);
  86. for (i = 0; i < c->nb_threads; i++)
  87. pthread_join(c->workers[i], NULL);
  88. pthread_mutex_destroy(&c->current_job_lock);
  89. pthread_cond_destroy(&c->current_job_cond);
  90. pthread_cond_destroy(&c->last_job_cond);
  91. av_freep(&c->workers);
  92. }
  93. static void slice_thread_park_workers(ThreadContext *c)
  94. {
  95. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  96. pthread_mutex_unlock(&c->current_job_lock);
  97. }
  98. static int thread_execute(AVFilterContext *ctx, action_func *func,
  99. void *arg, int *ret, int nb_jobs)
  100. {
  101. ThreadContext *c = ctx->graph->internal->thread;
  102. int dummy_ret;
  103. if (nb_jobs <= 0)
  104. return 0;
  105. pthread_mutex_lock(&c->current_job_lock);
  106. c->current_job = c->nb_threads;
  107. c->nb_jobs = nb_jobs;
  108. c->ctx = ctx;
  109. c->arg = arg;
  110. c->func = func;
  111. if (ret) {
  112. c->rets = ret;
  113. c->nb_rets = nb_jobs;
  114. } else {
  115. c->rets = &dummy_ret;
  116. c->nb_rets = 1;
  117. }
  118. pthread_cond_broadcast(&c->current_job_cond);
  119. slice_thread_park_workers(c);
  120. return 0;
  121. }
  122. static int thread_init_internal(ThreadContext *c, int nb_threads)
  123. {
  124. int i, ret;
  125. if (!nb_threads) {
  126. int nb_cpus = av_cpu_count();
  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. }