comparison w32thread.c @ 10393:5458953068fc libavcodec

Implement execute2 for w32thread
author reimar
date Tue, 13 Oct 2009 13:40:03 +0000
parents 32ee88f14239
children e68792a12c31
comparison
equal deleted inserted replaced
10392:32ee88f14239 10393:5458953068fc
30 HANDLE thread; 30 HANDLE thread;
31 HANDLE work_sem; 31 HANDLE work_sem;
32 HANDLE job_sem; 32 HANDLE job_sem;
33 HANDLE done_sem; 33 HANDLE done_sem;
34 int (*func)(AVCodecContext *c, void *arg); 34 int (*func)(AVCodecContext *c, void *arg);
35 int (*func2)(AVCodecContext *c, void *arg, int, int);
35 void *arg; 36 void *arg;
36 int argsize; 37 int argsize;
37 int *jobnr; 38 int *jobnr;
38 int *ret; 39 int *ret;
40 int threadnr;
39 }ThreadContext; 41 }ThreadContext;
40 42
41 43
42 static unsigned WINAPI attribute_align_arg thread_func(void *v){ 44 static unsigned WINAPI attribute_align_arg thread_func(void *v){
43 ThreadContext *c= v; 45 ThreadContext *c= v;
50 jobnr = (*c->jobnr)++; 52 jobnr = (*c->jobnr)++;
51 ReleaseSemaphore(c->job_sem, 1, 0); 53 ReleaseSemaphore(c->job_sem, 1, 0);
52 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout); 54 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
53 if(c->func) 55 if(c->func)
54 ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize); 56 ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
57 else if (c->func2)
58 ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
55 else 59 else
56 return 0; 60 return 0;
57 if (c->ret) 61 if (c->ret)
58 c->ret[jobnr] = ret; 62 c->ret[jobnr] = ret;
59 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout); 63 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
72 int i; 76 int i;
73 77
74 for(i=0; i<s->thread_count; i++){ 78 for(i=0; i<s->thread_count; i++){
75 79
76 c[i].func= NULL; 80 c[i].func= NULL;
81 c[i].func2= NULL;
77 } 82 }
78 ReleaseSemaphore(c[0].work_sem, s->thread_count, 0); 83 ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
79 for(i=0; i<s->thread_count; i++){ 84 for(i=0; i<s->thread_count; i++){
80 WaitForSingleObject(c[i].thread, INFINITE); 85 WaitForSingleObject(c[i].thread, INFINITE);
81 if(c[i].thread) CloseHandle(c[i].thread); 86 if(c[i].thread) CloseHandle(c[i].thread);
108 WaitForSingleObject(c[0].done_sem, INFINITE); 113 WaitForSingleObject(c[0].done_sem, INFINITE);
109 114
110 return 0; 115 return 0;
111 } 116 }
112 117
118 int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
119 ThreadContext *c= s->thread_opaque;
120 int i;
121 for(i=0; i<s->thread_count; i++)
122 c[i].func2 = func;
123 avcodec_thread_execute(s, NULL, arg, ret, count, 0);
124 }
125
113 int avcodec_thread_init(AVCodecContext *s, int thread_count){ 126 int avcodec_thread_init(AVCodecContext *s, int thread_count){
114 int i; 127 int i;
115 ThreadContext *c; 128 ThreadContext *c;
116 uint32_t threadid; 129 uint32_t threadid;
117 130
131 //printf("init semaphors %d\n", i); fflush(stdout); 144 //printf("init semaphors %d\n", i); fflush(stdout);
132 c[i].avctx= s; 145 c[i].avctx= s;
133 c[i].work_sem = c[0].work_sem; 146 c[i].work_sem = c[0].work_sem;
134 c[i].job_sem = c[0].job_sem; 147 c[i].job_sem = c[0].job_sem;
135 c[i].done_sem = c[0].done_sem; 148 c[i].done_sem = c[0].done_sem;
149 c[i].threadnr = i;
136 150
137 //printf("create thread %d\n", i); fflush(stdout); 151 //printf("create thread %d\n", i); fflush(stdout);
138 c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid ); 152 c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
139 if( !c[i].thread ) goto fail; 153 if( !c[i].thread ) goto fail;
140 } 154 }
141 //printf("init done\n"); fflush(stdout); 155 //printf("init done\n"); fflush(stdout);
142 156
143 s->execute= avcodec_thread_execute; 157 s->execute= avcodec_thread_execute;
158 s->execute2= avcodec_thread_execute2;
144 159
145 return 0; 160 return 0;
146 fail: 161 fail:
147 avcodec_thread_free(s); 162 avcodec_thread_free(s);
148 return -1; 163 return -1;