Mercurial > libavcodec.hg
comparison pthread.c @ 8129:a9734fe0811e libavcodec
Making it easier to send arbitrary structures as work orders to MT workers
| author | romansh |
|---|---|
| date | Wed, 12 Nov 2008 17:47:23 +0000 |
| parents | b0a566346fb1 |
| children | 09aafff47bc0 |
comparison
equal
deleted
inserted
replaced
| 8128:e2241dd85c65 | 8129:a9734fe0811e |
|---|---|
| 28 typedef int (action_t)(AVCodecContext *c, void *arg); | 28 typedef int (action_t)(AVCodecContext *c, void *arg); |
| 29 | 29 |
| 30 typedef struct ThreadContext { | 30 typedef struct ThreadContext { |
| 31 pthread_t *workers; | 31 pthread_t *workers; |
| 32 action_t *func; | 32 action_t *func; |
| 33 void **args; | 33 void *args; |
| 34 int *rets; | 34 int *rets; |
| 35 int rets_count; | 35 int rets_count; |
| 36 int job_count; | 36 int job_count; |
| 37 int job_size; | |
| 37 | 38 |
| 38 pthread_cond_t last_job_cond; | 39 pthread_cond_t last_job_cond; |
| 39 pthread_cond_t current_job_cond; | 40 pthread_cond_t current_job_cond; |
| 40 pthread_mutex_t current_job_lock; | 41 pthread_mutex_t current_job_lock; |
| 41 int current_job; | 42 int current_job; |
| 65 return NULL; | 66 return NULL; |
| 66 } | 67 } |
| 67 } | 68 } |
| 68 pthread_mutex_unlock(&c->current_job_lock); | 69 pthread_mutex_unlock(&c->current_job_lock); |
| 69 | 70 |
| 70 c->rets[our_job%c->rets_count] = c->func(avctx, c->args[our_job]); | 71 c->rets[our_job%c->rets_count] = c->func(avctx, (char*)c->args + our_job*c->job_size); |
| 71 | 72 |
| 72 pthread_mutex_lock(&c->current_job_lock); | 73 pthread_mutex_lock(&c->current_job_lock); |
| 73 our_job = c->current_job++; | 74 our_job = c->current_job++; |
| 74 } | 75 } |
| 75 } | 76 } |
| 98 pthread_cond_destroy(&c->last_job_cond); | 99 pthread_cond_destroy(&c->last_job_cond); |
| 99 av_free(c->workers); | 100 av_free(c->workers); |
| 100 av_freep(&avctx->thread_opaque); | 101 av_freep(&avctx->thread_opaque); |
| 101 } | 102 } |
| 102 | 103 |
| 103 int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, int *ret, int job_count) | 104 int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void *arg, int *ret, int job_count, int job_size) |
| 104 { | 105 { |
| 105 ThreadContext *c= avctx->thread_opaque; | 106 ThreadContext *c= avctx->thread_opaque; |
| 106 int dummy_ret; | 107 int dummy_ret; |
| 107 | 108 |
| 108 if (job_count <= 0) | 109 if (job_count <= 0) |
| 110 | 111 |
| 111 pthread_mutex_lock(&c->current_job_lock); | 112 pthread_mutex_lock(&c->current_job_lock); |
| 112 | 113 |
| 113 c->current_job = avctx->thread_count; | 114 c->current_job = avctx->thread_count; |
| 114 c->job_count = job_count; | 115 c->job_count = job_count; |
| 116 c->job_size = job_size; | |
| 115 c->args = arg; | 117 c->args = arg; |
| 116 c->func = func; | 118 c->func = func; |
| 117 if (ret) { | 119 if (ret) { |
| 118 c->rets = ret; | 120 c->rets = ret; |
| 119 c->rets_count = job_count; | 121 c->rets_count = job_count; |
| 145 | 147 |
| 146 avctx->thread_opaque = c; | 148 avctx->thread_opaque = c; |
| 147 avctx->thread_count = thread_count; | 149 avctx->thread_count = thread_count; |
| 148 c->current_job = 0; | 150 c->current_job = 0; |
| 149 c->job_count = 0; | 151 c->job_count = 0; |
| 152 c->job_size = 0; | |
| 150 c->done = 0; | 153 c->done = 0; |
| 151 pthread_cond_init(&c->current_job_cond, NULL); | 154 pthread_cond_init(&c->current_job_cond, NULL); |
| 152 pthread_cond_init(&c->last_job_cond, NULL); | 155 pthread_cond_init(&c->last_job_cond, NULL); |
| 153 pthread_mutex_init(&c->current_job_lock, NULL); | 156 pthread_mutex_init(&c->current_job_lock, NULL); |
| 154 pthread_mutex_lock(&c->current_job_lock); | 157 pthread_mutex_lock(&c->current_job_lock); |
