3 * This file is part of Libav.
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.
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.
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
22 * Libavfilter multithreading support
27 #include "libavutil/common.h"
28 #include "libavutil/cpu.h"
29 #include "libavutil/mem.h"
38 #include "compat/w32pthreads.h"
41 typedef struct ThreadContext {
46 avfilter_action_func *func;
48 /* per-execute perameters */
55 pthread_cond_t last_job_cond;
56 pthread_cond_t current_job_cond;
57 pthread_mutex_t current_job_lock;
59 unsigned int current_execute;
63 static void* attribute_align_arg worker(void *v)
66 int our_job = c->nb_jobs;
67 int nb_threads = c->nb_threads;
68 unsigned int last_execute = 0;
71 pthread_mutex_lock(&c->current_job_lock);
72 self_id = c->current_job++;
74 while (our_job >= c->nb_jobs) {
75 if (c->current_job == nb_threads + c->nb_jobs)
76 pthread_cond_signal(&c->last_job_cond);
78 while (last_execute == c->current_execute && !c->done)
79 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
80 last_execute = c->current_execute;
84 pthread_mutex_unlock(&c->current_job_lock);
88 pthread_mutex_unlock(&c->current_job_lock);
90 c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
92 pthread_mutex_lock(&c->current_job_lock);
93 our_job = c->current_job++;
97 static void slice_thread_uninit(ThreadContext *c)
101 pthread_mutex_lock(&c->current_job_lock);
103 pthread_cond_broadcast(&c->current_job_cond);
104 pthread_mutex_unlock(&c->current_job_lock);
106 for (i = 0; i < c->nb_threads; i++)
107 pthread_join(c->workers[i], NULL);
109 pthread_mutex_destroy(&c->current_job_lock);
110 pthread_cond_destroy(&c->current_job_cond);
111 pthread_cond_destroy(&c->last_job_cond);
112 av_freep(&c->workers);
115 static void slice_thread_park_workers(ThreadContext *c)
117 while (c->current_job != c->nb_threads + c->nb_jobs)
118 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
119 pthread_mutex_unlock(&c->current_job_lock);
122 static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
123 void *arg, int *ret, int nb_jobs)
125 ThreadContext *c = ctx->graph->internal->thread;
131 pthread_mutex_lock(&c->current_job_lock);
133 c->current_job = c->nb_threads;
134 c->nb_jobs = nb_jobs;
140 c->nb_rets = nb_jobs;
142 c->rets = &dummy_ret;
145 c->current_execute++;
147 pthread_cond_broadcast(&c->current_job_cond);
149 slice_thread_park_workers(c);
154 static int thread_init_internal(ThreadContext *c, int nb_threads)
159 int nb_cpus = av_cpu_count();
160 av_log(c->graph, AV_LOG_DEBUG, "Detected %d logical cores.\n", nb_cpus);
161 // use number of cores + 1 as thread count if there is more than one
163 nb_threads = nb_cpus + 1;
171 c->nb_threads = nb_threads;
172 c->workers = av_mallocz(sizeof(*c->workers) * nb_threads);
174 return AVERROR(ENOMEM);
180 pthread_cond_init(&c->current_job_cond, NULL);
181 pthread_cond_init(&c->last_job_cond, NULL);
183 pthread_mutex_init(&c->current_job_lock, NULL);
184 pthread_mutex_lock(&c->current_job_lock);
185 for (i = 0; i < nb_threads; i++) {
186 ret = pthread_create(&c->workers[i], NULL, worker, c);
188 pthread_mutex_unlock(&c->current_job_lock);
190 slice_thread_uninit(c);
195 slice_thread_park_workers(c);
197 return c->nb_threads;
200 int ff_graph_thread_init(AVFilterGraph *graph)
208 if (graph->nb_threads == 1) {
209 graph->thread_type = 0;
213 graph->internal->thread = av_mallocz(sizeof(ThreadContext));
214 if (!graph->internal->thread)
215 return AVERROR(ENOMEM);
217 ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
219 av_freep(&graph->internal->thread);
220 graph->thread_type = 0;
221 graph->nb_threads = 1;
222 return (ret < 0) ? ret : 0;
224 graph->nb_threads = ret;
226 graph->internal->thread_execute = thread_execute;
231 void ff_graph_thread_free(AVFilterGraph *graph)
233 if (graph->internal->thread)
234 slice_thread_uninit(graph->internal->thread);
235 av_freep(&graph->internal->thread);