pthread.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Libavfilter multithreading support
  21. */
  22. #include "config.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/cpu.h"
  25. #include "libavutil/mem.h"
  26. #include "libavutil/thread.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "thread.h"
  30. typedef struct ThreadContext {
  31. AVFilterGraph *graph;
  32. int nb_threads;
  33. pthread_t *workers;
  34. avfilter_action_func *func;
  35. /* per-execute parameters */
  36. AVFilterContext *ctx;
  37. void *arg;
  38. int *rets;
  39. int nb_rets;
  40. int nb_jobs;
  41. pthread_cond_t last_job_cond;
  42. pthread_cond_t current_job_cond;
  43. pthread_mutex_t current_job_lock;
  44. int current_job;
  45. unsigned int current_execute;
  46. int done;
  47. } ThreadContext;
  48. static void* attribute_align_arg worker(void *v)
  49. {
  50. ThreadContext *c = v;
  51. int our_job = c->nb_jobs;
  52. int nb_threads = c->nb_threads;
  53. unsigned int last_execute = 0;
  54. int self_id;
  55. pthread_mutex_lock(&c->current_job_lock);
  56. self_id = c->current_job++;
  57. for (;;) {
  58. while (our_job >= c->nb_jobs) {
  59. if (c->current_job == nb_threads + c->nb_jobs)
  60. pthread_cond_signal(&c->last_job_cond);
  61. while (last_execute == c->current_execute && !c->done)
  62. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  63. last_execute = c->current_execute;
  64. our_job = self_id;
  65. if (c->done) {
  66. pthread_mutex_unlock(&c->current_job_lock);
  67. return NULL;
  68. }
  69. }
  70. pthread_mutex_unlock(&c->current_job_lock);
  71. c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
  72. pthread_mutex_lock(&c->current_job_lock);
  73. our_job = c->current_job++;
  74. }
  75. }
  76. static void slice_thread_uninit(ThreadContext *c)
  77. {
  78. int i;
  79. pthread_mutex_lock(&c->current_job_lock);
  80. c->done = 1;
  81. pthread_cond_broadcast(&c->current_job_cond);
  82. pthread_mutex_unlock(&c->current_job_lock);
  83. for (i = 0; i < c->nb_threads; i++)
  84. pthread_join(c->workers[i], NULL);
  85. pthread_mutex_destroy(&c->current_job_lock);
  86. pthread_cond_destroy(&c->current_job_cond);
  87. pthread_cond_destroy(&c->last_job_cond);
  88. av_freep(&c->workers);
  89. }
  90. static void slice_thread_park_workers(ThreadContext *c)
  91. {
  92. while (c->current_job != c->nb_threads + c->nb_jobs)
  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. c->current_execute++;
  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. // 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_array(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. }