2 * This file is part of FFmpeg.
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.
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.
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
21 * Slice multithreading support functions
22 * @see doc/multithreading.txt
30 #include "compat/w32pthreads.h"
32 #include "compat/os2threads.h"
37 #include "pthread_internal.h"
40 #include "libavutil/avassert.h"
41 #include "libavutil/common.h"
42 #include "libavutil/cpu.h"
43 #include "libavutil/mem.h"
45 typedef int (action_func)(AVCodecContext *c, void *arg);
46 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
48 typedef struct SliceThreadContext {
57 pthread_cond_t last_job_cond;
58 pthread_cond_t current_job_cond;
59 pthread_mutex_t current_job_lock;
60 unsigned current_execute;
67 pthread_cond_t *progress_cond;
68 pthread_mutex_t *progress_mutex;
71 static void* attribute_align_arg worker(void *v)
73 AVCodecContext *avctx = v;
74 SliceThreadContext *c = avctx->internal->thread_ctx;
75 unsigned last_execute = 0;
76 int our_job = c->job_count;
77 int thread_count = avctx->thread_count;
80 pthread_mutex_lock(&c->current_job_lock);
81 self_id = c->current_job++;
84 while (our_job >= c->job_count) {
85 if (c->current_job == thread_count + c->job_count)
86 pthread_cond_signal(&c->last_job_cond);
88 while (last_execute == c->current_execute && !c->done)
89 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
90 last_execute = c->current_execute;
94 pthread_mutex_unlock(&c->current_job_lock);
98 pthread_mutex_unlock(&c->current_job_lock);
100 ret = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
101 c->func2(avctx, c->args, our_job, self_id);
103 c->rets[our_job%c->job_count] = ret;
105 pthread_mutex_lock(&c->current_job_lock);
106 our_job = c->current_job++;
110 void ff_slice_thread_free(AVCodecContext *avctx)
112 SliceThreadContext *c = avctx->internal->thread_ctx;
115 pthread_mutex_lock(&c->current_job_lock);
117 pthread_cond_broadcast(&c->current_job_cond);
118 for (i = 0; i < c->thread_count; i++)
119 pthread_cond_broadcast(&c->progress_cond[i]);
120 pthread_mutex_unlock(&c->current_job_lock);
122 for (i=0; i<avctx->thread_count; i++)
123 pthread_join(c->workers[i], NULL);
125 for (i = 0; i < c->thread_count; i++) {
126 pthread_mutex_destroy(&c->progress_mutex[i]);
127 pthread_cond_destroy(&c->progress_cond[i]);
130 pthread_mutex_destroy(&c->current_job_lock);
131 pthread_cond_destroy(&c->current_job_cond);
132 pthread_cond_destroy(&c->last_job_cond);
134 av_freep(&c->entries);
135 av_freep(&c->progress_mutex);
136 av_freep(&c->progress_cond);
138 av_freep(&c->workers);
139 av_freep(&avctx->internal->thread_ctx);
142 static av_always_inline void thread_park_workers(SliceThreadContext *c, int thread_count)
144 while (c->current_job != thread_count + c->job_count)
145 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
146 pthread_mutex_unlock(&c->current_job_lock);
149 static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
151 SliceThreadContext *c = avctx->internal->thread_ctx;
153 if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
154 return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
159 pthread_mutex_lock(&c->current_job_lock);
161 c->current_job = avctx->thread_count;
162 c->job_count = job_count;
163 c->job_size = job_size;
171 c->current_execute++;
172 pthread_cond_broadcast(&c->current_job_cond);
174 thread_park_workers(c, avctx->thread_count);
179 static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
181 SliceThreadContext *c = avctx->internal->thread_ctx;
183 return thread_execute(avctx, NULL, arg, ret, job_count, 0);
186 int ff_slice_thread_init(AVCodecContext *avctx)
189 SliceThreadContext *c;
190 int thread_count = avctx->thread_count;
197 int nb_cpus = av_cpu_count();
199 nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
200 // use number of cores + 1 as thread count if there is more than one
202 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
204 thread_count = avctx->thread_count = 1;
207 if (thread_count <= 1) {
208 avctx->active_thread_type = 0;
212 c = av_mallocz(sizeof(SliceThreadContext));
216 c->workers = av_mallocz_array(thread_count, sizeof(pthread_t));
222 avctx->internal->thread_ctx = c;
227 pthread_cond_init(&c->current_job_cond, NULL);
228 pthread_cond_init(&c->last_job_cond, NULL);
229 pthread_mutex_init(&c->current_job_lock, NULL);
230 pthread_mutex_lock(&c->current_job_lock);
231 for (i=0; i<thread_count; i++) {
232 if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
233 avctx->thread_count = i;
234 pthread_mutex_unlock(&c->current_job_lock);
235 ff_thread_free(avctx);
240 thread_park_workers(c, thread_count);
242 avctx->execute = thread_execute;
243 avctx->execute2 = thread_execute2;
247 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
249 SliceThreadContext *p = avctx->internal->thread_ctx;
250 int *entries = p->entries;
252 pthread_mutex_lock(&p->progress_mutex[thread]);
254 pthread_cond_signal(&p->progress_cond[thread]);
255 pthread_mutex_unlock(&p->progress_mutex[thread]);
258 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
260 SliceThreadContext *p = avctx->internal->thread_ctx;
261 int *entries = p->entries;
263 if (!entries || !field) return;
265 thread = thread ? thread - 1 : p->thread_count - 1;
267 pthread_mutex_lock(&p->progress_mutex[thread]);
268 while ((entries[field - 1] - entries[field]) < shift){
269 pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]);
271 pthread_mutex_unlock(&p->progress_mutex[thread]);
274 int ff_alloc_entries(AVCodecContext *avctx, int count)
278 if (avctx->active_thread_type & FF_THREAD_SLICE) {
279 SliceThreadContext *p = avctx->internal->thread_ctx;
282 av_assert0(p->thread_count == avctx->thread_count);
283 av_freep(&p->entries);
286 p->thread_count = avctx->thread_count;
287 p->entries = av_mallocz_array(count, sizeof(int));
289 if (!p->progress_mutex) {
290 p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t));
291 p->progress_cond = av_malloc_array(p->thread_count, sizeof(pthread_cond_t));
294 if (!p->entries || !p->progress_mutex || !p->progress_cond) {
295 av_freep(&p->entries);
296 av_freep(&p->progress_mutex);
297 av_freep(&p->progress_cond);
298 return AVERROR(ENOMEM);
300 p->entries_count = count;
302 for (i = 0; i < p->thread_count; i++) {
303 pthread_mutex_init(&p->progress_mutex[i], NULL);
304 pthread_cond_init(&p->progress_cond[i], NULL);
311 void ff_reset_entries(AVCodecContext *avctx)
313 SliceThreadContext *p = avctx->internal->thread_ctx;
314 memset(p->entries, 0, p->entries_count * sizeof(int));