2 * Copyright (c) 2004 Roman Shaposhnik.
4 * Many thanks to Steven M. Schultz for providing clever ideas and
5 * to Michael Niedermayer <michaelni@gmx.at> for writing initial
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 typedef int (action_t)(AVCodecContext *c, void *arg);
31 typedef struct ThreadContext {
39 pthread_cond_t last_job_cond;
40 pthread_cond_t current_job_cond;
41 pthread_mutex_t current_job_lock;
46 static void* worker(void *v)
48 AVCodecContext *avctx = v;
49 ThreadContext *c = avctx->thread_opaque;
50 int our_job = c->job_count;
51 int thread_count = avctx->thread_count;
54 pthread_mutex_lock(&c->current_job_lock);
55 self_id = c->current_job++;
57 while (our_job >= c->job_count) {
58 if (c->current_job == thread_count + c->job_count)
59 pthread_cond_signal(&c->last_job_cond);
61 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
65 pthread_mutex_unlock(&c->current_job_lock);
69 pthread_mutex_unlock(&c->current_job_lock);
71 c->rets[our_job%c->rets_count] = c->func(avctx, c->args[our_job]);
73 pthread_mutex_lock(&c->current_job_lock);
74 our_job = c->current_job++;
78 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
80 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
81 pthread_mutex_unlock(&c->current_job_lock);
84 void avcodec_thread_free(AVCodecContext *avctx)
86 ThreadContext *c = avctx->thread_opaque;
89 pthread_mutex_lock(&c->current_job_lock);
91 pthread_cond_broadcast(&c->current_job_cond);
92 pthread_mutex_unlock(&c->current_job_lock);
94 for (i=0; i<avctx->thread_count; i++)
95 pthread_join(c->workers[i], NULL);
97 pthread_mutex_destroy(&c->current_job_lock);
98 pthread_cond_destroy(&c->current_job_cond);
99 pthread_cond_destroy(&c->last_job_cond);
104 int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, int *ret, int job_count)
106 ThreadContext *c= avctx->thread_opaque;
112 pthread_mutex_lock(&c->current_job_lock);
114 c->current_job = avctx->thread_count;
115 c->job_count = job_count;
120 c->rets_count = job_count;
122 c->rets = &dummy_ret;
125 pthread_cond_broadcast(&c->current_job_cond);
127 avcodec_thread_park_workers(c, avctx->thread_count);
132 int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
137 c = av_mallocz(sizeof(ThreadContext));
141 c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
147 avctx->thread_opaque = c;
148 avctx->thread_count = thread_count;
152 pthread_cond_init(&c->current_job_cond, NULL);
153 pthread_cond_init(&c->last_job_cond, NULL);
154 pthread_mutex_init(&c->current_job_lock, NULL);
155 pthread_mutex_lock(&c->current_job_lock);
156 for (i=0; i<thread_count; i++) {
157 if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
158 avctx->thread_count = i;
159 pthread_mutex_unlock(&c->current_job_lock);
160 avcodec_thread_free(avctx);
165 avcodec_thread_park_workers(c, thread_count);
167 avctx->execute = avcodec_thread_execute;