comparison pthread.c @ 10386:98501365c3aa libavcodec

Add an execute2 function that is more flexible and allows to use parallel processing with jobs > threads without wasting too much memory. It also avoids needing a separate int array when the only additional data the jobs needs is a single int running from 0 to count-1.
author reimar
date Mon, 12 Oct 2009 11:35:35 +0000
parents 04423b2f6e0b
children d7ef6611a49e
comparison
equal deleted inserted replaced
10385:bc98e5724513 10386:98501365c3aa
24 #include <pthread.h> 24 #include <pthread.h>
25 25
26 #include "avcodec.h" 26 #include "avcodec.h"
27 27
28 typedef int (action_func)(AVCodecContext *c, void *arg); 28 typedef int (action_func)(AVCodecContext *c, void *arg);
29 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
29 30
30 typedef struct ThreadContext { 31 typedef struct ThreadContext {
31 pthread_t *workers; 32 pthread_t *workers;
32 action_func *func; 33 action_func *func;
34 action_func2 *func2;
33 void *args; 35 void *args;
34 int *rets; 36 int *rets;
35 int rets_count; 37 int rets_count;
36 int job_count; 38 int job_count;
37 int job_size; 39 int job_size;
66 return NULL; 68 return NULL;
67 } 69 }
68 } 70 }
69 pthread_mutex_unlock(&c->current_job_lock); 71 pthread_mutex_unlock(&c->current_job_lock);
70 72
71 c->rets[our_job%c->rets_count] = c->func(avctx, (char*)c->args + our_job*c->job_size); 73 c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
74 c->func2(avctx, c->args, our_job, self_id);
72 75
73 pthread_mutex_lock(&c->current_job_lock); 76 pthread_mutex_lock(&c->current_job_lock);
74 our_job = c->current_job++; 77 our_job = c->current_job++;
75 } 78 }
76 } 79 }
128 avcodec_thread_park_workers(c, avctx->thread_count); 131 avcodec_thread_park_workers(c, avctx->thread_count);
129 132
130 return 0; 133 return 0;
131 } 134 }
132 135
136 int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
137 {
138 ThreadContext *c= avctx->thread_opaque;
139 c->func2 = func2;
140 return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
141 }
142
133 int avcodec_thread_init(AVCodecContext *avctx, int thread_count) 143 int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
134 { 144 {
135 int i; 145 int i;
136 ThreadContext *c; 146 ThreadContext *c;
137 147
165 } 175 }
166 176
167 avcodec_thread_park_workers(c, thread_count); 177 avcodec_thread_park_workers(c, thread_count);
168 178
169 avctx->execute = avcodec_thread_execute; 179 avctx->execute = avcodec_thread_execute;
180 avctx->execute2 = avcodec_thread_execute2;
170 return 0; 181 return 0;
171 } 182 }