diff 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
line wrap: on
line diff
--- a/pthread.c	Sun Oct 11 16:16:08 2009 +0000
+++ b/pthread.c	Mon Oct 12 11:35:35 2009 +0000
@@ -26,10 +26,12 @@
 #include "avcodec.h"
 
 typedef int (action_func)(AVCodecContext *c, void *arg);
+typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
 
 typedef struct ThreadContext {
     pthread_t *workers;
     action_func *func;
+    action_func2 *func2;
     void *args;
     int *rets;
     int rets_count;
@@ -68,7 +70,8 @@
         }
         pthread_mutex_unlock(&c->current_job_lock);
 
-        c->rets[our_job%c->rets_count] = c->func(avctx, (char*)c->args + our_job*c->job_size);
+        c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
+                                                   c->func2(avctx, c->args, our_job, self_id);
 
         pthread_mutex_lock(&c->current_job_lock);
         our_job = c->current_job++;
@@ -130,6 +133,13 @@
     return 0;
 }
 
+int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
+{
+    ThreadContext *c= avctx->thread_opaque;
+    c->func2 = func2;
+    return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
+}
+
 int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
 {
     int i;
@@ -167,5 +177,6 @@
     avcodec_thread_park_workers(c, thread_count);
 
     avctx->execute = avcodec_thread_execute;
+    avctx->execute2 = avcodec_thread_execute2;
     return 0;
 }