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