]> git.sesse.net Git - ffmpeg/blob - libavcodec/beosthread.c
Remove duplicate #includes, avcodec.h #includes common.h.
[ffmpeg] / libavcodec / beosthread.c
1 /*
2  * Copyright (c) 2004 François Revol <revol@free.fr>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21 //#define DEBUG
22
23 #include "avcodec.h"
24
25 #include <OS.h>
26
27 typedef struct ThreadContext{
28     AVCodecContext *avctx;
29     thread_id thread;
30     sem_id work_sem;
31     sem_id done_sem;
32     int (*func)(AVCodecContext *c, void *arg);
33     void *arg;
34     int ret;
35 }ThreadContext;
36
37 // it's odd Be never patented that :D
38 struct benaphore {
39         vint32 atom;
40         sem_id sem;
41 };
42 static inline int lock_ben(struct benaphore *ben)
43 {
44         if (atomic_add(&ben->atom, 1) > 0)
45                 return acquire_sem(ben->sem);
46         return B_OK;
47 }
48 static inline int unlock_ben(struct benaphore *ben)
49 {
50         if (atomic_add(&ben->atom, -1) > 1)
51                 return release_sem(ben->sem);
52         return B_OK;
53 }
54
55 static struct benaphore av_thread_lib_ben;
56
57 static int32 ff_thread_func(void *v){
58     ThreadContext *c= v;
59
60     for(;;){
61 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
62         acquire_sem(c->work_sem);
63 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
64         if(c->func)
65             c->ret= c->func(c->avctx, c->arg);
66         else
67             return 0;
68 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
69         release_sem(c->done_sem);
70     }
71
72     return B_OK;
73 }
74
75 /**
76  * free what has been allocated by avcodec_thread_init().
77  * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
78  */
79 void avcodec_thread_free(AVCodecContext *s){
80     ThreadContext *c= s->thread_opaque;
81     int i;
82     int32 ret;
83
84     for(i=0; i<s->thread_count; i++){
85
86         c[i].func= NULL;
87         release_sem(c[i].work_sem);
88         wait_for_thread(c[i].thread, &ret);
89         if(c[i].work_sem > B_OK) delete_sem(c[i].work_sem);
90         if(c[i].done_sem > B_OK) delete_sem(c[i].done_sem);
91     }
92
93     av_freep(&s->thread_opaque);
94 }
95
96 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
97     ThreadContext *c= s->thread_opaque;
98     int i;
99
100     assert(s == c->avctx);
101     assert(count <= s->thread_count);
102
103     /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
104
105     for(i=0; i<count; i++){
106         c[i].arg= arg[i];
107         c[i].func= func;
108         c[i].ret= 12345;
109
110         release_sem(c[i].work_sem);
111     }
112     for(i=0; i<count; i++){
113         acquire_sem(c[i].done_sem);
114
115         c[i].func= NULL;
116         if(ret) ret[i]= c[i].ret;
117     }
118     return 0;
119 }
120
121 int avcodec_thread_init(AVCodecContext *s, int thread_count){
122     int i;
123     ThreadContext *c;
124
125     s->thread_count= thread_count;
126
127     assert(!s->thread_opaque);
128     c= av_mallocz(sizeof(ThreadContext)*thread_count);
129     s->thread_opaque= c;
130
131     for(i=0; i<thread_count; i++){
132 //printf("init semaphors %d\n", i); fflush(stdout);
133         c[i].avctx= s;
134
135         if((c[i].work_sem = create_sem(0, "ff work sem")) < B_OK)
136             goto fail;
137         if((c[i].done_sem = create_sem(0, "ff done sem")) < B_OK)
138             goto fail;
139
140 //printf("create thread %d\n", i); fflush(stdout);
141         c[i].thread = spawn_thread(ff_thread_func, "libavcodec thread", B_LOW_PRIORITY, &c[i] );
142         if( c[i].thread < B_OK ) goto fail;
143         resume_thread(c[i].thread );
144     }
145 //printf("init done\n"); fflush(stdout);
146
147     s->execute= avcodec_thread_execute;
148
149     return 0;
150 fail:
151     avcodec_thread_free(s);
152     return -1;
153 }
154
155 /* provide a mean to serialize calls to avcodec_*() for thread safety. */
156
157 int avcodec_thread_lock_lib(void)
158 {
159         return lock_ben(&av_thread_lib_ben);
160 }
161
162 int avcodec_thread_unlock_lib(void)
163 {
164         return unlock_ben(&av_thread_lib_ben);
165 }
166
167 /* our versions of _init and _fini (which are called by those actually from crt.o) */
168
169 void initialize_after(void)
170 {
171         av_thread_lib_ben.atom = 0;
172         av_thread_lib_ben.sem = create_sem(0, "libavcodec benaphore");
173 }
174
175 void uninitialize_before(void)
176 {
177         delete_sem(av_thread_lib_ben.sem);
178 }
179
180
181