annotate pthread.c @ 4962:f99e40a7155b libavcodec

Remove redundant #inclusion of common.h, avcodec.h already #includes it.
author diego
date Thu, 10 May 2007 09:00:44 +0000
parents d6f83e2f8804
children 2b72f9bc4f06
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
1 /*
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
2 * Copyright (c) 2004 Roman Shaposhnik.
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
3 *
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
4 * Many thanks to Steven M. Schultz for providing clever ideas and
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
5 * to Michael Niedermayer <michaelni@gmx.at> for writing initial
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
6 * implementation.
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
7 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
8 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
9 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
10 * FFmpeg is free software; you can redistribute it and/or
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
11 * modify it under the terms of the GNU Lesser General Public
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
12 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
13 * version 2.1 of the License, or (at your option) any later version.
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
14 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
15 * FFmpeg is distributed in the hope that it will be useful,
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
18 * Lesser General Public License for more details.
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
19 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
20 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
21 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2979
diff changeset
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
23 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
24 */
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
25 #include <pthread.h>
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
26
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
27 #include "avcodec.h"
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
28
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
29 typedef int (action_t)(AVCodecContext *c, void *arg);
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
30
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
31 typedef struct ThreadContext {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
32 pthread_t *workers;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
33 action_t *func;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
34 void **args;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
35 int *rets;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
36 int rets_count;
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
37 int job_count;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
38
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
39 pthread_cond_t last_job_cond;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
40 pthread_cond_t current_job_cond;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
41 pthread_mutex_t current_job_lock;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
42 int current_job;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
43 int done;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
44 } ThreadContext;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
45
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
46 static void* worker(void *v)
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
47 {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
48 AVCodecContext *avctx = v;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
49 ThreadContext *c = avctx->thread_opaque;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
50 int our_job = c->job_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
51 int thread_count = avctx->thread_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
52 int self_id;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
53
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
54 pthread_mutex_lock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
55 self_id = c->current_job++;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
56 for (;;){
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
57 while (our_job >= c->job_count) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
58 if (c->current_job == thread_count + c->job_count)
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
59 pthread_cond_signal(&c->last_job_cond);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
60
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
61 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
62 our_job = self_id;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
63
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
64 if (c->done) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
65 pthread_mutex_unlock(&c->current_job_lock);
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
66 return NULL;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
67 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
68 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
69 pthread_mutex_unlock(&c->current_job_lock);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
70
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
71 c->rets[our_job%c->rets_count] = c->func(avctx, c->args[our_job]);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
72
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
73 pthread_mutex_lock(&c->current_job_lock);
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
74 our_job = c->current_job++;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
75 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
76 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
77
4283
d6f83e2f8804 rename always_inline to av_always_inline and move to common.h
mru
parents: 3947
diff changeset
78 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
79 {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
80 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
81 pthread_mutex_unlock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
82 }
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
83
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
84 void avcodec_thread_free(AVCodecContext *avctx)
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
85 {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
86 ThreadContext *c = avctx->thread_opaque;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
87 int i;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
88
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
89 pthread_mutex_lock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
90 c->done = 1;
2035
e1b69326ae36 10l fixes by ("Debabrata Banerjee" <davatar at comcast dot net>)
michael
parents: 2023
diff changeset
91 pthread_cond_broadcast(&c->current_job_cond);
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
92 pthread_mutex_unlock(&c->current_job_lock);
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
93
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
94 for (i=0; i<avctx->thread_count; i++)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
95 pthread_join(c->workers[i], NULL);
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
96
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
97 pthread_mutex_destroy(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
98 pthread_cond_destroy(&c->current_job_cond);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
99 pthread_cond_destroy(&c->last_job_cond);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
100 av_free(c->workers);
2035
e1b69326ae36 10l fixes by ("Debabrata Banerjee" <davatar at comcast dot net>)
michael
parents: 2023
diff changeset
101 av_free(c);
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
102 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
103
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
104 int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, int *ret, int job_count)
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
105 {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
106 ThreadContext *c= avctx->thread_opaque;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
107 int dummy_ret;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
108
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
109 if (job_count <= 0)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
110 return 0;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
111
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
112 pthread_mutex_lock(&c->current_job_lock);
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
113
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
114 c->current_job = avctx->thread_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
115 c->job_count = job_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
116 c->args = arg;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
117 c->func = func;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
118 if (ret) {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
119 c->rets = ret;
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
120 c->rets_count = job_count;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
121 } else {
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
122 c->rets = &dummy_ret;
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
123 c->rets_count = 1;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
124 }
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
125 pthread_cond_broadcast(&c->current_job_cond);
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
126
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
127 avcodec_thread_park_workers(c, avctx->thread_count);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
128
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
129 return 0;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
130 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
131
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
132 int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
133 {
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
134 int i;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
135 ThreadContext *c;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
136
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
137 c = av_mallocz(sizeof(ThreadContext));
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
138 if (!c)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
139 return -1;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
140
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
141 c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
142 if (!c->workers) {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
143 av_free(c);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
144 return -1;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
145 }
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
146
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
147 avctx->thread_opaque = c;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
148 avctx->thread_count = thread_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
149 c->current_job = 0;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
150 c->job_count = 0;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
151 c->done = 0;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
152 pthread_cond_init(&c->current_job_cond, NULL);
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
153 pthread_cond_init(&c->last_job_cond, NULL);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
154 pthread_mutex_init(&c->current_job_lock, NULL);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
155 pthread_mutex_lock(&c->current_job_lock);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
156 for (i=0; i<thread_count; i++) {
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
157 if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
158 avctx->thread_count = i;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
159 pthread_mutex_unlock(&c->current_job_lock);
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
160 avcodec_thread_free(avctx);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
161 return -1;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
162 }
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
163 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
164
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
165 avcodec_thread_park_workers(c, thread_count);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2035
diff changeset
166
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
167 avctx->execute = avcodec_thread_execute;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
168 return 0;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
169 }