00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "avcodec.h"
00025
00026 #define INCL_DOS
00027 #define INCL_DOSERRORS
00028 #define INCL_DOSDEVIOCTL
00029 #include <os2.h>
00030
00031 typedef struct ThreadContext{
00032 AVCodecContext *avctx;
00033 int thread;
00034 HEV work_sem;
00035 HEV done_sem;
00036 int (*func)(AVCodecContext *c, void *arg);
00037 void *arg;
00038 int ret;
00039 }ThreadContext;
00040
00041
00042 static void attribute_align_arg thread_func(void *v){
00043 ThreadContext *c= v;
00044
00045 for(;;){
00046
00047 DosWaitEventSem(c->work_sem, SEM_INDEFINITE_WAIT);
00048
00049
00050 if(c->func)
00051 c->ret= c->func(c->avctx, c->arg);
00052 else
00053 return;
00054
00055 DosPostEventSem(c->done_sem);
00056
00057 }
00058
00059 return;
00060 }
00061
00066 void avcodec_thread_free(AVCodecContext *s){
00067 ThreadContext *c= s->thread_opaque;
00068 int i;
00069
00070 for(i=0; i<s->thread_count; i++){
00071
00072 c[i].func= NULL;
00073 DosPostEventSem(c[i].work_sem);
00074
00075 DosWaitThread((PTID)&c[i].thread,DCWW_WAIT);
00076
00077 if(c[i].work_sem) DosCloseEventSem(c[i].work_sem);
00078 if(c[i].done_sem) DosCloseEventSem(c[i].done_sem);
00079 }
00080
00081 av_freep(&s->thread_opaque);
00082 }
00083
00084 static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
00085 ThreadContext *c= s->thread_opaque;
00086 int i;
00087
00088 assert(s == c->avctx);
00089 assert(count <= s->thread_count);
00090
00091
00092
00093 for(i=0; i<count; i++){
00094
00095 c[i].arg= (char*)arg + i*size;
00096 c[i].func= func;
00097 c[i].ret= 12345;
00098
00099 DosPostEventSem(c[i].work_sem);
00100
00101 }
00102 for(i=0; i<count; i++){
00103 DosWaitEventSem(c[i].done_sem,SEM_INDEFINITE_WAIT);
00104
00105
00106 c[i].func= NULL;
00107 if(ret) ret[i]= c[i].ret;
00108 }
00109 return 0;
00110 }
00111
00112 int avcodec_thread_init(AVCodecContext *s, int thread_count){
00113 int i;
00114 ThreadContext *c;
00115 uint32_t threadid;
00116
00117 s->thread_count= thread_count;
00118
00119 if (thread_count <= 1)
00120 return 0;
00121
00122 assert(!s->thread_opaque);
00123 c= av_mallocz(sizeof(ThreadContext)*thread_count);
00124 s->thread_opaque= c;
00125
00126 for(i=0; i<thread_count; i++){
00127
00128 c[i].avctx= s;
00129
00130 if (DosCreateEventSem(NULL,&c[i].work_sem,DC_SEM_SHARED,0))
00131 goto fail;
00132 if (DosCreateEventSem(NULL,&c[i].done_sem,DC_SEM_SHARED,0))
00133 goto fail;
00134
00135
00136
00137 c[i].thread = _beginthread(thread_func, NULL, 0x10000, &c[i]);
00138 if( c[i].thread <= 0 ) goto fail;
00139 }
00140
00141
00142 s->execute= avcodec_thread_execute;
00143
00144 return 0;
00145 fail:
00146 avcodec_thread_free(s);
00147 return -1;
00148 }