upload tizen1.0 source
[framework/multimedia/gstreamer0.10-ffmpeg.git] / gst-libs / ext / ffmpeg / libavcodec / os2thread.c
1 /*
2  * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
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 //#define DEBUG
21
22 // Ported by Vlad Stelmahovsky
23
24 #include "avcodec.h"
25
26 #define INCL_DOS
27 #define INCL_DOSERRORS
28 #define INCL_DOSDEVIOCTL
29 #include <os2.h>
30
31 typedef struct ThreadContext{
32     AVCodecContext *avctx;
33     int thread;
34     HEV work_sem;
35     HEV done_sem;
36     int (*func)(AVCodecContext *c, void *arg);
37     void *arg;
38     int ret;
39 }ThreadContext;
40
41
42 static void attribute_align_arg thread_func(void *v){
43     ThreadContext *c= v;
44
45     for(;;){
46         //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
47         DosWaitEventSem(c->work_sem, SEM_INDEFINITE_WAIT);
48 //        WaitForSingleObject(c->work_sem, INFINITE);
49 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
50         if(c->func)
51             c->ret= c->func(c->avctx, c->arg);
52         else
53             return;
54         //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
55         DosPostEventSem(c->done_sem);
56 //        ReleaseSemaphore(c->done_sem, 1, 0);
57     }
58
59     return;
60 }
61
62 /**
63  * free what has been allocated by avcodec_thread_init().
64  * must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running
65  */
66 void avcodec_thread_free(AVCodecContext *s){
67     ThreadContext *c= s->thread_opaque;
68     int i;
69
70     for(i=0; i<s->thread_count; i++){
71
72         c[i].func= NULL;
73         DosPostEventSem(c[i].work_sem);
74         //        ReleaseSemaphore(c[i].work_sem, 1, 0);
75         DosWaitThread((PTID)&c[i].thread,DCWW_WAIT);
76 //        WaitForSingleObject(c[i].thread, INFINITE);
77         if(c[i].work_sem) DosCloseEventSem(c[i].work_sem);//CloseHandle(c[i].work_sem);
78         if(c[i].done_sem) DosCloseEventSem(c[i].done_sem);//CloseHandle(c[i].done_sem);
79     }
80
81     av_freep(&s->thread_opaque);
82 }
83
84 static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
85     ThreadContext *c= s->thread_opaque;
86     int i;
87
88     assert(s == c->avctx);
89     assert(count <= s->thread_count);
90
91     /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
92
93     for(i=0; i<count; i++){
94
95         c[i].arg= (char*)arg + i*size;
96         c[i].func= func;
97         c[i].ret= 12345;
98
99         DosPostEventSem(c[i].work_sem);
100 //        ReleaseSemaphore(c[i].work_sem, 1, 0);
101     }
102     for(i=0; i<count; i++){
103         DosWaitEventSem(c[i].done_sem,SEM_INDEFINITE_WAIT);
104 //        WaitForSingleObject(c[i].done_sem, INFINITE);
105
106         c[i].func= NULL;
107         if(ret) ret[i]= c[i].ret;
108     }
109     return 0;
110 }
111
112 int avcodec_thread_init(AVCodecContext *s, int thread_count){
113     int i;
114     ThreadContext *c;
115     uint32_t threadid;
116
117     s->thread_count= thread_count;
118
119     if (thread_count <= 1)
120         return 0;
121
122     assert(!s->thread_opaque);
123     c= av_mallocz(sizeof(ThreadContext)*thread_count);
124     s->thread_opaque= c;
125
126     for(i=0; i<thread_count; i++){
127 //printf("init semaphors %d\n", i); fflush(stdout);
128         c[i].avctx= s;
129
130         if (DosCreateEventSem(NULL,&c[i].work_sem,DC_SEM_SHARED,0))
131             goto fail;
132         if (DosCreateEventSem(NULL,&c[i].done_sem,DC_SEM_SHARED,0))
133             goto fail;
134
135 //printf("create thread %d\n", i); fflush(stdout);
136 //        c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
137         c[i].thread = _beginthread(thread_func, NULL, 0x10000, &c[i]);
138         if( c[i].thread <= 0 ) goto fail;
139     }
140 //printf("init done\n"); fflush(stdout);
141
142     s->execute= avcodec_thread_execute;
143
144     return 0;
145 fail:
146     avcodec_thread_free(s);
147     return -1;
148 }