Mercurial > libavcodec.hg
annotate pthread.c @ 1893:779bdf5063ce libavcodec
1000000l (using uninitalized variable for initalizing bits per MV table)
author | michael |
---|---|
date | Tue, 16 Mar 2004 16:57:27 +0000 |
parents | 00a6bfc81010 |
children | 50e92cec1b84 |
rev | line source |
---|---|
1799 | 1 /* |
2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> | |
3 * | |
4 * This library 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 of the License, or (at your option) any later version. | |
8 * | |
9 * This library 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. | |
13 * | |
14 * You should have received a copy of the GNU Lesser General Public | |
15 * License along with this library; if not, write to the Free Software | |
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 * | |
18 */ | |
19 #include <semaphore.h> | |
20 #include <pthread.h> | |
21 | |
22 //#define DEBUG | |
23 | |
24 #include "avcodec.h" | |
25 #include "common.h" | |
26 | |
1857 | 27 typedef struct JobContext{ |
28 sem_t available_sem; | |
29 int assigned; | |
1799 | 30 int (*func)(AVCodecContext *c, void *arg); |
31 void *arg; | |
32 int ret; | |
1857 | 33 }JobContext; |
34 | |
35 typedef struct WorkerContext{ | |
36 AVCodecContext *avctx; | |
37 pthread_t thread; | |
38 int start_index; | |
39 sem_t work_sem; | |
40 sem_t done_sem; | |
41 }WorkerContext; | |
42 | |
43 typedef struct ThreadContext{ | |
44 WorkerContext *worker; | |
45 JobContext *job; | |
46 int job_count; | |
47 int allocated_job_count; | |
1799 | 48 }ThreadContext; |
49 | |
50 static void * thread_func(void *v){ | |
1857 | 51 WorkerContext *w= v; |
52 ThreadContext *c= w->avctx->thread_opaque; | |
53 int i; | |
1799 | 54 |
55 for(;;){ | |
1857 | 56 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X enter wait\n", (int)v); |
57 sem_wait(&w->work_sem); | |
58 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X after wait\n", (int)v); | |
59 if(c->job_count == 0) | |
60 break; | |
61 | |
62 for(i=0; i<c->job_count; i++){ | |
63 int index= (i + w->start_index) % c->job_count; | |
64 JobContext *j= &c->job[index]; | |
65 | |
66 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X first check of %d\n", (int)v, index); | |
67 if(j->assigned) continue; //unsynced check, if != 0 it is already given to another worker, it never becomes available before the next execute() call so this should be safe | |
68 | |
69 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X second check of %d\n", (int)v, index); | |
70 if(sem_trywait(&j->available_sem) == 0){ | |
71 j->assigned=1; | |
72 j->ret= j->func(w->avctx, j->arg); | |
73 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X done %d\n", (int)v, index); | |
74 } | |
75 } | |
76 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X complete\n", (int)v); | |
77 sem_post(&w->done_sem); | |
1799 | 78 } |
79 | |
80 return NULL; | |
81 } | |
82 | |
83 /** | |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1799
diff
changeset
|
84 * free what has been allocated by avcodec_thread_init(). |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1799
diff
changeset
|
85 * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running |
1799 | 86 */ |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1799
diff
changeset
|
87 void avcodec_thread_free(AVCodecContext *s){ |
1799 | 88 ThreadContext *c= s->thread_opaque; |
1857 | 89 int i, val; |
90 | |
91 for(i=0; i<c->allocated_job_count; i++){ | |
92 sem_getvalue(&c->job[i].available_sem, &val); assert(val == 0); | |
93 sem_destroy(&c->job[i].available_sem); | |
1799 | 94 } |
95 | |
1857 | 96 c->job_count= 0; |
97 for(i=0; i<s->thread_count; i++){ | |
98 sem_getvalue(&c->worker[i].work_sem, &val); assert(val == 0); | |
99 sem_getvalue(&c->worker[i].done_sem, &val); assert(val == 0); | |
100 | |
101 sem_post(&c->worker[i].work_sem); | |
102 pthread_join(c->worker[i].thread, NULL); | |
103 sem_destroy(&c->worker[i].work_sem); | |
104 sem_destroy(&c->worker[i].done_sem); | |
105 } | |
106 | |
107 av_freep(&c->job); | |
108 av_freep(&c->worker); | |
1799 | 109 av_freep(&s->thread_opaque); |
110 } | |
111 | |
1857 | 112 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int job_count){ |
1799 | 113 ThreadContext *c= s->thread_opaque; |
114 int i, val; | |
115 | |
116 assert(s == c->avctx); | |
1857 | 117 if(job_count > c->allocated_job_count){ |
118 c->job= av_realloc(c->job, job_count*sizeof(JobContext)); | |
119 | |
120 for(i=c->allocated_job_count; i<job_count; i++){ | |
121 memset(&c->job[i], 0, sizeof(JobContext)); | |
122 c->allocated_job_count++; | |
123 | |
124 if(sem_init(&c->job[i].available_sem, 0, 0)) | |
125 return -1; | |
126 } | |
127 } | |
128 c->job_count= job_count; | |
1799 | 129 |
130 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */ | |
131 | |
1857 | 132 for(i=0; i<job_count; i++){ |
133 sem_getvalue(&c->job[i].available_sem, &val); assert(val == 0); | |
1799 | 134 |
1857 | 135 c->job[i].arg= arg[i]; |
136 c->job[i].func= func; | |
137 c->job[i].ret= 12345; | |
138 c->job[i].assigned= 0; | |
139 sem_post(&c->job[i].available_sem); | |
1799 | 140 } |
1857 | 141 |
142 for(i=0; i<s->thread_count && i<job_count; i++){ | |
143 sem_getvalue(&c->worker[i].work_sem, &val); assert(val == 0); | |
144 sem_getvalue(&c->worker[i].done_sem, &val); assert(val == 0); | |
145 | |
146 c->worker[i].start_index= (i + job_count/2)/job_count; | |
147 //av_log(s, AV_LOG_DEBUG, "start worker %d\n", i); | |
148 sem_post(&c->worker[i].work_sem); | |
149 } | |
1799 | 150 |
1857 | 151 for(i=0; i<s->thread_count && i<job_count; i++){ |
152 //av_log(s, AV_LOG_DEBUG, "wait for worker %d\n", i); | |
153 sem_wait(&c->worker[i].done_sem); | |
154 | |
155 sem_getvalue(&c->worker[i].work_sem, &val); assert(val == 0); | |
156 sem_getvalue(&c->worker[i].done_sem, &val); assert(val == 0); | |
157 } | |
158 | |
159 for(i=0; i<job_count; i++){ | |
160 sem_getvalue(&c->job[i].available_sem, &val); assert(val == 0); | |
1799 | 161 |
1857 | 162 c->job[i].func= NULL; |
163 if(ret) ret[i]= c->job[i].ret; | |
1799 | 164 } |
1857 | 165 |
1799 | 166 return 0; |
167 } | |
168 | |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1799
diff
changeset
|
169 int avcodec_thread_init(AVCodecContext *s, int thread_count){ |
1799 | 170 int i; |
171 ThreadContext *c; | |
1857 | 172 WorkerContext *worker; |
1799 | 173 |
174 s->thread_count= thread_count; | |
175 | |
176 assert(!s->thread_opaque); | |
1857 | 177 c= av_mallocz(sizeof(ThreadContext)); |
178 worker= av_mallocz(sizeof(WorkerContext)*thread_count); | |
1799 | 179 s->thread_opaque= c; |
1857 | 180 c->worker= worker; |
181 | |
1799 | 182 for(i=0; i<thread_count; i++){ |
183 //printf("init semaphors %d\n", i); fflush(stdout); | |
1857 | 184 worker[i].avctx= s; |
185 if(sem_init(&worker[i].work_sem, 0, 0)) | |
1799 | 186 goto fail; |
1857 | 187 if(sem_init(&worker[i].done_sem, 0, 0)) |
1799 | 188 goto fail; |
189 //printf("create thread %d\n", i); fflush(stdout); | |
1857 | 190 if(pthread_create(&worker[i].thread, NULL, thread_func, &worker[i])) |
1799 | 191 goto fail; |
192 } | |
193 //printf("init done\n"); fflush(stdout); | |
194 | |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1799
diff
changeset
|
195 s->execute= avcodec_thread_execute; |
1799 | 196 |
197 return 0; | |
198 fail: | |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1799
diff
changeset
|
199 avcodec_thread_free(s); |
1799 | 200 return -1; |
201 } |