pthread.c 6.0 KB

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