annotate pthread.c @ 1822:7366bb5c363f libavcodec

w32threads by (Gildas Bazin <gbazin at altern dot org>)
author michael
date Thu, 19 Feb 2004 20:29:26 +0000
parents 95612d423fde
children 00a6bfc81010
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
1 /*
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
3 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
4 * This library is free software; you can redistribute it and/or
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
5 * modify it under the terms of the GNU Lesser General Public
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
6 * License as published by the Free Software Foundation; either
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
7 * version 2 of the License, or (at your option) any later version.
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
8 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
9 * This library is distributed in the hope that it will be useful,
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
12 * Lesser General Public License for more details.
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
13 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
14 * You should have received a copy of the GNU Lesser General Public
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
15 * License along with this library; if not, write to the Free Software
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
17 *
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
18 */
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
19 #include <semaphore.h>
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
20 #include <pthread.h>
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
21
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
22 //#define DEBUG
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
23
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
24 #include "avcodec.h"
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
25 #include "common.h"
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
26
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
27
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
28 typedef struct ThreadContext{
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
29 AVCodecContext *avctx;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
30 pthread_t thread;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
31 sem_t work_sem;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
32 sem_t done_sem;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
33 int (*func)(AVCodecContext *c, void *arg);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
34 void *arg;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
35 int ret;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
36 }ThreadContext;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
37
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
38 static void * thread_func(void *v){
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
39 ThreadContext *c= v;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
40
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
41 for(;;){
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
42 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
43 sem_wait(&c->work_sem);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
44 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
45 if(c->func)
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
46 c->ret= c->func(c->avctx, c->arg);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
47 else
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
48 return NULL;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
49 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
50 sem_post(&c->done_sem);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
51 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
52
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
53 return NULL;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
54 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
55
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
56 /**
1822
7366bb5c363f w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents: 1799
diff changeset
57 * free what has been allocated by avcodec_thread_init().
7366bb5c363f w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents: 1799
diff changeset
58 * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
59 */
1822
7366bb5c363f w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents: 1799
diff changeset
60 void avcodec_thread_free(AVCodecContext *s){
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
61 ThreadContext *c= s->thread_opaque;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
62 int i;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
63
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
64 for(i=0; i<s->thread_count; i++){
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
65 int val;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
66
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
67 sem_getvalue(&c[i].work_sem, &val); assert(val == 0);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
68 sem_getvalue(&c[i].done_sem, &val); assert(val == 0);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
69
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
70 c[i].func= NULL;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
71 sem_post(&c[i].work_sem);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
72 pthread_join(c[i].thread, NULL);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
73 sem_destroy(&c[i].work_sem);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
74 sem_destroy(&c[i].done_sem);
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 av_freep(&s->thread_opaque);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
78 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
79
1822
7366bb5c363f w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents: 1799
diff changeset
80 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
81 ThreadContext *c= s->thread_opaque;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
82 int i, val;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
83
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
84 assert(s == c->avctx);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
85 assert(count <= s->thread_count);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
86
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
87 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
88
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
89 for(i=0; i<count; i++){
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
90 sem_getvalue(&c[i].work_sem, &val); assert(val == 0);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
91 sem_getvalue(&c[i].done_sem, &val); assert(val == 0);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
92
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
93 c[i].arg= arg[i];
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
94 c[i].func= func;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
95 c[i].ret= 12345;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
96 sem_post(&c[i].work_sem);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
97 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
98 for(i=0; i<count; i++){
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
99 sem_wait(&c[i].done_sem);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
100
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
101 sem_getvalue(&c[i].work_sem, &val); assert(val == 0);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
102 sem_getvalue(&c[i].done_sem, &val); assert(val == 0);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
103
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
104 c[i].func= NULL;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
105 if(ret) ret[i]= c[i].ret;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
106 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
107 return 0;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
108 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
109
1822
7366bb5c363f w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents: 1799
diff changeset
110 int avcodec_thread_init(AVCodecContext *s, int thread_count){
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
111 int i;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
112 ThreadContext *c;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
113
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
114 s->thread_count= thread_count;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
115
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
116 assert(!s->thread_opaque);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
117 c= av_mallocz(sizeof(ThreadContext)*thread_count);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
118 s->thread_opaque= c;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
119
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
120 for(i=0; i<thread_count; i++){
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
121 //printf("init semaphors %d\n", i); fflush(stdout);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
122 c[i].avctx= s;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
123 if(sem_init(&c[i].work_sem, 0, 0))
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
124 goto fail;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
125 if(sem_init(&c[i].done_sem, 0, 0))
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
126 goto fail;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
127 //printf("create thread %d\n", i); fflush(stdout);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
128 if(pthread_create(&c[i].thread, NULL, thread_func, &c[i]))
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
129 goto fail;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
130 }
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
131 //printf("init done\n"); fflush(stdout);
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
132
1822
7366bb5c363f w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents: 1799
diff changeset
133 s->execute= avcodec_thread_execute;
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
134
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
135 return 0;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
136 fail:
1822
7366bb5c363f w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents: 1799
diff changeset
137 avcodec_thread_free(s);
1799
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
138 return -1;
95612d423fde multithreaded/SMP motion estimation
michael
parents:
diff changeset
139 }