Use FFABS instead of abs.
[FFMpeg-mirror/ordered_chapters.git] / libavcodec / beosthread.c
blobb1c65ddc1c845dab446f3b5606200558c3197703
1 /*
2 * Copyright (c) 2004 François Revol <revol@free.fr>
4 * This file is part of FFmpeg.
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.
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.
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
21 //#define DEBUG
23 #include "avcodec.h"
25 #include <OS.h>
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;
37 // it's odd Be never patented that :D
38 struct benaphore {
39 vint32 atom;
40 sem_id sem;
42 static inline int lock_ben(struct benaphore *ben)
44 if (atomic_add(&ben->atom, 1) > 0)
45 return acquire_sem(ben->sem);
46 return B_OK;
48 static inline int unlock_ben(struct benaphore *ben)
50 if (atomic_add(&ben->atom, -1) > 1)
51 return release_sem(ben->sem);
52 return B_OK;
55 static struct benaphore av_thread_lib_ben;
57 static int32 ff_thread_func(void *v){
58 ThreadContext *c= v;
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);
72 return B_OK;
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
79 void avcodec_thread_free(AVCodecContext *s){
80 ThreadContext *c= s->thread_opaque;
81 int i;
82 int32 ret;
84 for(i=0; i<s->thread_count; i++){
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);
93 av_freep(&s->thread_opaque);
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;
100 assert(s == c->avctx);
101 assert(count <= s->thread_count);
103 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
105 for(i=0; i<count; i++){
106 c[i].arg= arg[i];
107 c[i].func= func;
108 c[i].ret= 12345;
110 release_sem(c[i].work_sem);
112 for(i=0; i<count; i++){
113 acquire_sem(c[i].done_sem);
115 c[i].func= NULL;
116 if(ret) ret[i]= c[i].ret;
118 return 0;
121 int avcodec_thread_init(AVCodecContext *s, int thread_count){
122 int i;
123 ThreadContext *c;
125 s->thread_count= thread_count;
127 assert(!s->thread_opaque);
128 c= av_mallocz(sizeof(ThreadContext)*thread_count);
129 s->thread_opaque= c;
131 for(i=0; i<thread_count; i++){
132 //printf("init semaphors %d\n", i); fflush(stdout);
133 c[i].avctx= s;
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;
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 );
145 //printf("init done\n"); fflush(stdout);
147 s->execute= avcodec_thread_execute;
149 return 0;
150 fail:
151 avcodec_thread_free(s);
152 return -1;
155 /* provide a mean to serialize calls to avcodec_*() for thread safety. */
157 int avcodec_thread_lock_lib(void)
159 return lock_ben(&av_thread_lib_ben);
162 int avcodec_thread_unlock_lib(void)
164 return unlock_ben(&av_thread_lib_ben);
167 /* our versions of _init and _fini (which are called by those actually from crt.o) */
169 void initialize_after(void)
171 av_thread_lib_ben.atom = 0;
172 av_thread_lib_ben.sem = create_sem(0, "libavcodec benaphore");
175 void uninitialize_before(void)
177 delete_sem(av_thread_lib_ben.sem);