3 * This file is part of FFmpeg.
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.
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.
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
22 * Libavfilter multithreading support
27 #include "libavutil/common.h"
28 #include "libavutil/cpu.h"
29 #include "libavutil/mem.h"
38 #include "compat/os2threads.h"
40 #include "compat/w32pthreads.h"
43 typedef struct ThreadContext {
50 /* per-execute perameters */
57 pthread_cond_t last_job_cond;
58 pthread_cond_t current_job_cond;
59 pthread_mutex_t current_job_lock;
64 static void* attribute_align_arg worker(void *v)
67 int our_job = c->nb_jobs;
68 int nb_threads = c->nb_threads;
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 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
82 pthread_mutex_unlock(&c->current_job_lock);
86 pthread_mutex_unlock(&c->current_job_lock);
88 c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
90 pthread_mutex_lock(&c->current_job_lock);
91 our_job = c->current_job++;
95 static void slice_thread_uninit(ThreadContext *c)
99 pthread_mutex_lock(&c->current_job_lock);
101 pthread_cond_broadcast(&c->current_job_cond);
102 pthread_mutex_unlock(&c->current_job_lock);
104 for (i = 0; i < c->nb_threads; i++)
105 pthread_join(c->workers[i], NULL);
107 pthread_mutex_destroy(&c->current_job_lock);
108 pthread_cond_destroy(&c->current_job_cond);
109 pthread_cond_destroy(&c->last_job_cond);
110 av_freep(&c->workers);
113 static void slice_thread_park_workers(ThreadContext *c)
115 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
116 pthread_mutex_unlock(&c->current_job_lock);
119 static int thread_execute(AVFilterContext *ctx, action_func *func,
120 void *arg, int *ret, int nb_jobs)
122 ThreadContext *c = ctx->graph->internal->thread;
128 pthread_mutex_lock(&c->current_job_lock);
130 c->current_job = c->nb_threads;
131 c->nb_jobs = nb_jobs;
137 c->nb_rets = nb_jobs;
139 c->rets = &dummy_ret;
142 pthread_cond_broadcast(&c->current_job_cond);
144 slice_thread_park_workers(c);
149 static int thread_init_internal(ThreadContext *c, int nb_threads)
154 int nb_cpus = av_cpu_count();
155 // use number of cores + 1 as thread count if there is more than one
157 nb_threads = nb_cpus + 1;
165 c->nb_threads = nb_threads;
166 c->workers = av_mallocz(sizeof(*c->workers) * nb_threads);
168 return AVERROR(ENOMEM);
174 pthread_cond_init(&c->current_job_cond, NULL);
175 pthread_cond_init(&c->last_job_cond, NULL);
177 pthread_mutex_init(&c->current_job_lock, NULL);
178 pthread_mutex_lock(&c->current_job_lock);
179 for (i = 0; i < nb_threads; i++) {
180 ret = pthread_create(&c->workers[i], NULL, worker, c);
182 pthread_mutex_unlock(&c->current_job_lock);
184 slice_thread_uninit(c);
189 slice_thread_park_workers(c);
191 return c->nb_threads;
194 int ff_graph_thread_init(AVFilterGraph *graph)
202 if (graph->nb_threads == 1) {
203 graph->thread_type = 0;
207 graph->internal->thread = av_mallocz(sizeof(ThreadContext));
208 if (!graph->internal->thread)
209 return AVERROR(ENOMEM);
211 ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
213 av_freep(&graph->internal->thread);
214 graph->thread_type = 0;
215 graph->nb_threads = 1;
216 return (ret < 0) ? ret : 0;
218 graph->nb_threads = ret;
220 graph->internal->thread_execute = thread_execute;
225 void ff_graph_thread_free(AVFilterGraph *graph)
227 if (graph->internal->thread)
228 slice_thread_uninit(graph->internal->thread);
229 av_freep(&graph->internal->thread);