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.

237 lines
6.0KB

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