pthread.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 "avfilter.h"
  27. #include "internal.h"
  28. #include "thread.h"
  29. #if HAVE_PTHREADS
  30. #include <pthread.h>
  31. #elif HAVE_W32THREADS
  32. #include "compat/w32pthreads.h"
  33. #endif
  34. typedef struct ThreadContext {
  35. AVFilterGraph *graph;
  36. int nb_threads;
  37. pthread_t *workers;
  38. avfilter_action_func *func;
  39. /* per-execute parameters */
  40. AVFilterContext *ctx;
  41. void *arg;
  42. int *rets;
  43. int nb_rets;
  44. int nb_jobs;
  45. pthread_cond_t last_job_cond;
  46. pthread_cond_t current_job_cond;
  47. pthread_mutex_t current_job_lock;
  48. int current_job;
  49. unsigned int current_execute;
  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. unsigned int last_execute = 0;
  58. int self_id;
  59. pthread_mutex_lock(&c->current_job_lock);
  60. self_id = c->current_job++;
  61. for (;;) {
  62. while (our_job >= c->nb_jobs) {
  63. if (c->current_job == nb_threads + c->nb_jobs)
  64. pthread_cond_signal(&c->last_job_cond);
  65. while (last_execute == c->current_execute && !c->done)
  66. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  67. last_execute = c->current_execute;
  68. our_job = self_id;
  69. if (c->done) {
  70. pthread_mutex_unlock(&c->current_job_lock);
  71. return NULL;
  72. }
  73. }
  74. pthread_mutex_unlock(&c->current_job_lock);
  75. c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
  76. pthread_mutex_lock(&c->current_job_lock);
  77. our_job = c->current_job++;
  78. }
  79. }
  80. static void slice_thread_uninit(ThreadContext *c)
  81. {
  82. int i;
  83. pthread_mutex_lock(&c->current_job_lock);
  84. c->done = 1;
  85. pthread_cond_broadcast(&c->current_job_cond);
  86. pthread_mutex_unlock(&c->current_job_lock);
  87. for (i = 0; i < c->nb_threads; i++)
  88. pthread_join(c->workers[i], NULL);
  89. pthread_mutex_destroy(&c->current_job_lock);
  90. pthread_cond_destroy(&c->current_job_cond);
  91. pthread_cond_destroy(&c->last_job_cond);
  92. av_freep(&c->workers);
  93. }
  94. static void slice_thread_park_workers(ThreadContext *c)
  95. {
  96. while (c->current_job != c->nb_threads + c->nb_jobs)
  97. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  98. pthread_mutex_unlock(&c->current_job_lock);
  99. }
  100. static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
  101. void *arg, int *ret, int nb_jobs)
  102. {
  103. ThreadContext *c = ctx->graph->internal->thread;
  104. int dummy_ret;
  105. if (nb_jobs <= 0)
  106. return 0;
  107. pthread_mutex_lock(&c->current_job_lock);
  108. c->current_job = c->nb_threads;
  109. c->nb_jobs = nb_jobs;
  110. c->ctx = ctx;
  111. c->arg = arg;
  112. c->func = func;
  113. if (ret) {
  114. c->rets = ret;
  115. c->nb_rets = nb_jobs;
  116. } else {
  117. c->rets = &dummy_ret;
  118. c->nb_rets = 1;
  119. }
  120. c->current_execute++;
  121. pthread_cond_broadcast(&c->current_job_cond);
  122. slice_thread_park_workers(c);
  123. return 0;
  124. }
  125. static int thread_init_internal(ThreadContext *c, int nb_threads)
  126. {
  127. int i, ret;
  128. if (!nb_threads) {
  129. int nb_cpus = av_cpu_count();
  130. av_log(c->graph, AV_LOG_DEBUG, "Detected %d logical cores.\n", nb_cpus);
  131. // use number of cores + 1 as thread count if there is more than one
  132. if (nb_cpus > 1)
  133. nb_threads = nb_cpus + 1;
  134. else
  135. nb_threads = 1;
  136. }
  137. if (nb_threads <= 1)
  138. return 1;
  139. c->nb_threads = nb_threads;
  140. c->workers = av_mallocz(sizeof(*c->workers) * nb_threads);
  141. if (!c->workers)
  142. return AVERROR(ENOMEM);
  143. c->current_job = 0;
  144. c->nb_jobs = 0;
  145. c->done = 0;
  146. pthread_cond_init(&c->current_job_cond, NULL);
  147. pthread_cond_init(&c->last_job_cond, NULL);
  148. pthread_mutex_init(&c->current_job_lock, NULL);
  149. pthread_mutex_lock(&c->current_job_lock);
  150. for (i = 0; i < nb_threads; i++) {
  151. ret = pthread_create(&c->workers[i], NULL, worker, c);
  152. if (ret) {
  153. pthread_mutex_unlock(&c->current_job_lock);
  154. c->nb_threads = i;
  155. slice_thread_uninit(c);
  156. return AVERROR(ret);
  157. }
  158. }
  159. slice_thread_park_workers(c);
  160. return c->nb_threads;
  161. }
  162. int ff_graph_thread_init(AVFilterGraph *graph)
  163. {
  164. int ret;
  165. #if HAVE_W32THREADS
  166. w32thread_init();
  167. #endif
  168. if (graph->nb_threads == 1) {
  169. graph->thread_type = 0;
  170. return 0;
  171. }
  172. graph->internal->thread = av_mallocz(sizeof(ThreadContext));
  173. if (!graph->internal->thread)
  174. return AVERROR(ENOMEM);
  175. ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
  176. if (ret <= 1) {
  177. av_freep(&graph->internal->thread);
  178. graph->thread_type = 0;
  179. graph->nb_threads = 1;
  180. return (ret < 0) ? ret : 0;
  181. }
  182. graph->nb_threads = ret;
  183. graph->internal->thread_execute = thread_execute;
  184. return 0;
  185. }
  186. void ff_graph_thread_free(AVFilterGraph *graph)
  187. {
  188. if (graph->internal->thread)
  189. slice_thread_uninit(graph->internal->thread);
  190. av_freep(&graph->internal->thread);
  191. }