annotate pthread.c @ 2347:c6280d48be02 libavcodec

When bswap_32 is a macro, png images fail to decode properly, patch by (Milan Cutka <cutka>at<szm>dot<sk>)
author michael
date Fri, 12 Nov 2004 09:48:06 +0000
parents e1b69326ae36
children ef2149182f1c
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.
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
3 *
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 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
8 * This library is free software; you can redistribute it and/or
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
11 * version 2 of the License, or (at your option) any later version.
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
12 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
13 * This library is distributed in the hope that it will be useful,
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
16 * Lesser General Public License for more details.
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
17 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
19 * License along with this library; if not, write to the Free Software
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
21 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
22 */
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
23 #include <pthread.h>
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
24
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
25 #include "avcodec.h"
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
26 #include "common.h"
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
27
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
28 typedef int (action_t)(AVCodecContext *c, void *arg);
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
29
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
30 typedef struct ThreadContext {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
31 pthread_t *workers;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
32 action_t *func;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
33 void **args;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
34 int *rets;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
35 int rets_count;
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
36 int job_count;
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
37
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
38 pthread_cond_t last_job_cond;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
39 pthread_cond_t current_job_cond;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
40 pthread_mutex_t current_job_lock;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
41 int current_job;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
42 int done;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
43 } ThreadContext;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
44
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
45 static void* worker(void *v)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
46 {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
47 AVCodecContext *avctx = v;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
48 ThreadContext *c = avctx->thread_opaque;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
49 int our_job = c->job_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
50 int thread_count = avctx->thread_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
51 int self_id;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
52
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
53 pthread_mutex_lock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
54 self_id = c->current_job++;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
55 for (;;){
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
56 while (our_job >= c->job_count) {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
57 if (c->current_job == thread_count + c->job_count)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
58 pthread_cond_signal(&c->last_job_cond);
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
59
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
60 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
61 our_job = self_id;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
62
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
63 if (c->done) {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
64 pthread_mutex_unlock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
65 return NULL;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
66 }
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
67 }
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
68 pthread_mutex_unlock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
69
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
70 c->rets[our_job%c->rets_count] = c->func(avctx, c->args[our_job]);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
71
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
72 pthread_mutex_lock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
73 our_job = c->current_job++;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
74 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
75 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
76
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
77 static always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
78 {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
79 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
80 pthread_mutex_unlock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
81 }
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
82
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
83 void avcodec_thread_free(AVCodecContext *avctx)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
84 {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
85 ThreadContext *c = avctx->thread_opaque;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
86 int i;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
87
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
88 pthread_mutex_lock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
89 c->done = 1;
2035
e1b69326ae36 10l fixes by ("Debabrata Banerjee" <davatar at comcast dot net>)
michael
parents: 2023
diff changeset
90 pthread_cond_broadcast(&c->current_job_cond);
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
91 pthread_mutex_unlock(&c->current_job_lock);
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
92
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
93 for (i=0; i<avctx->thread_count; i++)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
94 pthread_join(c->workers[i], NULL);
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
95
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
96 pthread_mutex_destroy(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
97 pthread_cond_destroy(&c->current_job_cond);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
98 pthread_cond_destroy(&c->last_job_cond);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
99 av_free(c->workers);
2035
e1b69326ae36 10l fixes by ("Debabrata Banerjee" <davatar at comcast dot net>)
michael
parents: 2023
diff changeset
100 av_free(c);
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
101 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
102
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
103 int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, int *ret, int job_count)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
104 {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
105 ThreadContext *c= avctx->thread_opaque;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
106 int dummy_ret;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
107
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
108 if (job_count <= 0)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
109 return 0;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
110
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
111 pthread_mutex_lock(&c->current_job_lock);
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
112
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
113 c->current_job = avctx->thread_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
114 c->job_count = job_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
115 c->args = arg;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
116 c->func = func;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
117 if (ret) {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
118 c->rets = ret;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
119 c->rets_count = job_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
120 } else {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
121 c->rets = &dummy_ret;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
122 c->rets_count = 1;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
123 }
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
124 pthread_cond_broadcast(&c->current_job_cond);
1857
00a6bfc81010 count > thread_count for execute()
michael
parents: 1822
diff changeset
125
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
126 avcodec_thread_park_workers(c, avctx->thread_count);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
127
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
128 return 0;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
129 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
130
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
131 int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
132 {
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
133 int i;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
134 ThreadContext *c;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
135
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
136 c = av_mallocz(sizeof(ThreadContext));
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
137 if (!c)
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
138 return -1;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
139
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
140 c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
141 if (!c->workers) {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
142 av_free(c);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
143 return -1;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
144 }
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
145
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
146 avctx->thread_opaque = c;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
147 avctx->thread_count = thread_count;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
148 c->current_job = 0;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
149 c->job_count = 0;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
150 c->done = 0;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
151 pthread_cond_init(&c->current_job_cond, NULL);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
152 pthread_cond_init(&c->last_job_cond, NULL);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
153 pthread_mutex_init(&c->current_job_lock, NULL);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
154 pthread_mutex_lock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
155 for (i=0; i<thread_count; i++) {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
156 if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
157 avctx->thread_count = i;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
158 pthread_mutex_unlock(&c->current_job_lock);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
159 avcodec_thread_free(avctx);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
160 return -1;
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
161 }
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
162 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
163
2023
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
164 avcodec_thread_park_workers(c, thread_count);
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
165
50e92cec1b84 * reimplementation using mutexes and condition variables.
romansh
parents: 1857
diff changeset
166 avctx->execute = avcodec_thread_execute;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
167 return 0;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
168 }