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
30
typedef int (action_t)(AVCodecContext *c, void *arg);
32
typedef struct ThreadContext {
40
pthread_cond_t last_job_cond;
41
pthread_cond_t current_job_cond;
42
pthread_mutex_t current_job_lock;
47
static void* worker(void *v)
49
AVCodecContext *avctx = v;
50
ThreadContext *c = avctx->thread_opaque;
51
int our_job = c->job_count;
52
int thread_count = avctx->thread_count;
55
pthread_mutex_lock(&c->current_job_lock);
56
self_id = c->current_job++;
58
while (our_job >= c->job_count) {
59
if (c->current_job == thread_count + c->job_count)
60
pthread_cond_signal(&c->last_job_cond);
62
pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
66
pthread_mutex_unlock(&c->current_job_lock);
70
pthread_mutex_unlock(&c->current_job_lock);
72
c->rets[our_job%c->rets_count] = c->func(avctx, c->args[our_job]);
74
pthread_mutex_lock(&c->current_job_lock);
75
our_job = c->current_job++;
79
static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
81
pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
82
pthread_mutex_unlock(&c->current_job_lock);
85
void avcodec_thread_free(AVCodecContext *avctx)
87
ThreadContext *c = avctx->thread_opaque;
90
pthread_mutex_lock(&c->current_job_lock);
92
pthread_cond_broadcast(&c->current_job_cond);
93
pthread_mutex_unlock(&c->current_job_lock);
95
for (i=0; i<avctx->thread_count; i++)
96
pthread_join(c->workers[i], NULL);
98
pthread_mutex_destroy(&c->current_job_lock);
99
pthread_cond_destroy(&c->current_job_cond);
100
pthread_cond_destroy(&c->last_job_cond);
105
int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, int *ret, int job_count)
107
ThreadContext *c= avctx->thread_opaque;
113
pthread_mutex_lock(&c->current_job_lock);
115
c->current_job = avctx->thread_count;
116
c->job_count = job_count;
121
c->rets_count = job_count;
123
c->rets = &dummy_ret;
126
pthread_cond_broadcast(&c->current_job_cond);
128
avcodec_thread_park_workers(c, avctx->thread_count);
133
int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
138
c = av_mallocz(sizeof(ThreadContext));
142
c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
148
avctx->thread_opaque = c;
149
avctx->thread_count = thread_count;
153
pthread_cond_init(&c->current_job_cond, NULL);
154
pthread_cond_init(&c->last_job_cond, NULL);
155
pthread_mutex_init(&c->current_job_lock, NULL);
156
pthread_mutex_lock(&c->current_job_lock);
157
for (i=0; i<thread_count; i++) {
158
if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
159
avctx->thread_count = i;
160
pthread_mutex_unlock(&c->current_job_lock);
161
avcodec_thread_free(avctx);
166
avcodec_thread_park_workers(c, thread_count);
168
avctx->execute = avcodec_thread_execute;