comparison src/ffmpeg/libavcodec/beosthread.c @ 808:e8776388b02a trunk

[svn] - add ffmpeg
author nenolod
date Mon, 12 Mar 2007 11:18:54 -0700
parents
children
comparison
equal deleted inserted replaced
807:0f9c8d4d3ac4 808:e8776388b02a
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 #include "common.h"
25
26 #include <OS.h>
27
28 typedef struct ThreadContext{
29 AVCodecContext *avctx;
30 thread_id thread;
31 sem_id work_sem;
32 sem_id done_sem;
33 int (*func)(AVCodecContext *c, void *arg);
34 void *arg;
35 int ret;
36 }ThreadContext;
37
38 // it's odd Be never patented that :D
39 struct benaphore {
40 vint32 atom;
41 sem_id sem;
42 };
43 static inline int lock_ben(struct benaphore *ben)
44 {
45 if (atomic_add(&ben->atom, 1) > 0)
46 return acquire_sem(ben->sem);
47 return B_OK;
48 }
49 static inline int unlock_ben(struct benaphore *ben)
50 {
51 if (atomic_add(&ben->atom, -1) > 1)
52 return release_sem(ben->sem);
53 return B_OK;
54 }
55
56 static struct benaphore av_thread_lib_ben;
57
58 static int32 ff_thread_func(void *v){
59 ThreadContext *c= v;
60
61 for(;;){
62 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
63 acquire_sem(c->work_sem);
64 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
65 if(c->func)
66 c->ret= c->func(c->avctx, c->arg);
67 else
68 return 0;
69 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
70 release_sem(c->done_sem);
71 }
72
73 return B_OK;
74 }
75
76 /**
77 * free what has been allocated by avcodec_thread_init().
78 * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
79 */
80 void avcodec_thread_free(AVCodecContext *s){
81 ThreadContext *c= s->thread_opaque;
82 int i;
83 int32 ret;
84
85 for(i=0; i<s->thread_count; i++){
86
87 c[i].func= NULL;
88 release_sem(c[i].work_sem);
89 wait_for_thread(c[i].thread, &ret);
90 if(c[i].work_sem > B_OK) delete_sem(c[i].work_sem);
91 if(c[i].done_sem > B_OK) delete_sem(c[i].done_sem);
92 }
93
94 av_freep(&s->thread_opaque);
95 }
96
97 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
98 ThreadContext *c= s->thread_opaque;
99 int i;
100
101 assert(s == c->avctx);
102 assert(count <= s->thread_count);
103
104 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
105
106 for(i=0; i<count; i++){
107 c[i].arg= arg[i];
108 c[i].func= func;
109 c[i].ret= 12345;
110
111 release_sem(c[i].work_sem);
112 }
113 for(i=0; i<count; i++){
114 acquire_sem(c[i].done_sem);
115
116 c[i].func= NULL;
117 if(ret) ret[i]= c[i].ret;
118 }
119 return 0;
120 }
121
122 int avcodec_thread_init(AVCodecContext *s, int thread_count){
123 int i;
124 ThreadContext *c;
125
126 s->thread_count= thread_count;
127
128 assert(!s->thread_opaque);
129 c= av_mallocz(sizeof(ThreadContext)*thread_count);
130 s->thread_opaque= c;
131
132 for(i=0; i<thread_count; i++){
133 //printf("init semaphors %d\n", i); fflush(stdout);
134 c[i].avctx= s;
135
136 if((c[i].work_sem = create_sem(0, "ff work sem")) < B_OK)
137 goto fail;
138 if((c[i].done_sem = create_sem(0, "ff done sem")) < B_OK)
139 goto fail;
140
141 //printf("create thread %d\n", i); fflush(stdout);
142 c[i].thread = spawn_thread(ff_thread_func, "libavcodec thread", B_LOW_PRIORITY, &c[i] );
143 if( c[i].thread < B_OK ) goto fail;
144 resume_thread(c[i].thread );
145 }
146 //printf("init done\n"); fflush(stdout);
147
148 s->execute= avcodec_thread_execute;
149
150 return 0;
151 fail:
152 avcodec_thread_free(s);
153 return -1;
154 }
155
156 /* provide a mean to serialize calls to avcodec_*() for thread safety. */
157
158 int avcodec_thread_lock_lib(void)
159 {
160 return lock_ben(&av_thread_lib_ben);
161 }
162
163 int avcodec_thread_unlock_lib(void)
164 {
165 return unlock_ben(&av_thread_lib_ben);
166 }
167
168 /* our versions of _init and _fini (which are called by those actually from crt.o) */
169
170 void initialize_after(void)
171 {
172 av_thread_lib_ben.atom = 0;
173 av_thread_lib_ben.sem = create_sem(0, "libavcodec benaphore");
174 }
175
176 void uninitialize_before(void)
177 {
178 delete_sem(av_thread_lib_ben.sem);
179 }
180
181
182