]> git.sesse.net Git - ffmpeg/blob - libavcodec/beosthread.c
BeOS threading support. changed some "if FOO_THREAD||BAR_THREAD" to a more generic...
[ffmpeg] / libavcodec / beosthread.c
1 /*
2  * Copyright (c) 2004 François Revol <revol@free.fr>
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 //#define DEBUG
20
21 #include "avcodec.h"
22 #include "common.h"
23
24 #include <OS.h>
25
26 typedef struct ThreadContext{
27     AVCodecContext *avctx;
28     thread_id thread;
29     sem_id work_sem;
30     sem_id done_sem;
31     int (*func)(AVCodecContext *c, void *arg);
32     void *arg;
33     int ret;
34 }ThreadContext;
35
36
37 static int32 ff_thread_func(void *v){
38     ThreadContext *c= v;
39
40     for(;;){
41 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
42         acquire_sem(c->work_sem);
43 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
44         if(c->func)
45             c->ret= c->func(c->avctx, c->arg);
46         else
47             return 0;
48 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
49         release_sem(c->done_sem);
50     }
51     
52     return B_OK;
53 }
54
55 /**
56  * free what has been allocated by avcodec_thread_init().
57  * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
58  */
59 void avcodec_thread_free(AVCodecContext *s){
60     ThreadContext *c= s->thread_opaque;
61     int i;
62     int32 ret;
63
64     for(i=0; i<s->thread_count; i++){
65         
66         c[i].func= NULL;
67         release_sem(c[i].work_sem);
68         wait_for_thread(c[i].thread, &ret);
69         if(c[i].work_sem > B_OK) delete_sem(c[i].work_sem);
70         if(c[i].done_sem > B_OK) delete_sem(c[i].done_sem);
71     }
72
73     av_freep(&s->thread_opaque);
74 }
75
76 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
77     ThreadContext *c= s->thread_opaque;
78     int i;
79     
80     assert(s == c->avctx);
81     assert(count <= s->thread_count);
82     
83     /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
84
85     for(i=0; i<count; i++){
86         c[i].arg= arg[i];
87         c[i].func= func;
88         c[i].ret= 12345;
89
90         release_sem(c[i].work_sem);
91     }
92     for(i=0; i<count; i++){
93         acquire_sem(c[i].done_sem);
94         
95         c[i].func= NULL;
96         if(ret) ret[i]= c[i].ret;
97     }
98     return 0;
99 }
100
101 int avcodec_thread_init(AVCodecContext *s, int thread_count){
102     int i;
103     ThreadContext *c;
104
105     s->thread_count= thread_count;
106
107     assert(!s->thread_opaque);
108     c= av_mallocz(sizeof(ThreadContext)*thread_count);
109     s->thread_opaque= c;
110     
111     for(i=0; i<thread_count; i++){
112 //printf("init semaphors %d\n", i); fflush(stdout);
113         c[i].avctx= s;
114
115         if((c[i].work_sem = create_sem(0, "ff work sem")) < B_OK)
116             goto fail;
117         if((c[i].done_sem = create_sem(0, "ff done sem")) < B_OK)
118             goto fail;
119
120 //printf("create thread %d\n", i); fflush(stdout);
121         c[i].thread = spawn_thread(ff_thread_func, "libavcodec thread", B_LOW_PRIORITY, &c[i] );
122         if( c[i].thread < B_OK ) goto fail;
123         resume_thread(c[i].thread );
124     }
125 //printf("init done\n"); fflush(stdout);
126     
127     s->execute= avcodec_thread_execute;
128
129     return 0;
130 fail:
131     avcodec_thread_free(s);
132     return -1;
133 }