• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/w32thread.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 //#define DEBUG
00021 
00022 #include "avcodec.h"
00023 #include "thread.h"
00024 
00025 #define WIN32_LEAN_AND_MEAN
00026 #include <windows.h>
00027 #include <process.h>
00028 
00029 typedef struct ThreadContext{
00030     AVCodecContext *avctx;
00031     HANDLE thread;
00032     HANDLE work_sem;
00033     HANDLE job_sem;
00034     HANDLE done_sem;
00035     int (*func)(AVCodecContext *c, void *arg);
00036     int (*func2)(AVCodecContext *c, void *arg, int, int);
00037     void *arg;
00038     int argsize;
00039     int *jobnr;
00040     int *ret;
00041     int threadnr;
00042 }ThreadContext;
00043 
00044 
00045 static unsigned WINAPI attribute_align_arg thread_func(void *v){
00046     ThreadContext *c= v;
00047 
00048     for(;;){
00049         int ret, jobnr;
00050 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
00051         WaitForSingleObject(c->work_sem, INFINITE);
00052         // avoid trying to access jobnr if we should quit
00053         if (!c->func && !c->func2)
00054             break;
00055         WaitForSingleObject(c->job_sem, INFINITE);
00056         jobnr = (*c->jobnr)++;
00057         ReleaseSemaphore(c->job_sem, 1, 0);
00058 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
00059         if(c->func)
00060             ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
00061         else
00062             ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
00063         if (c->ret)
00064             c->ret[jobnr] = ret;
00065 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
00066         ReleaseSemaphore(c->done_sem, 1, 0);
00067     }
00068 
00069     return 0;
00070 }
00071 
00076 void ff_thread_free(AVCodecContext *s){
00077     ThreadContext *c= s->thread_opaque;
00078     int i;
00079 
00080     for(i=0; i<s->thread_count; i++){
00081 
00082         c[i].func= NULL;
00083         c[i].func2= NULL;
00084     }
00085     ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
00086     for(i=0; i<s->thread_count; i++){
00087         WaitForSingleObject(c[i].thread, INFINITE);
00088         if(c[i].thread)   CloseHandle(c[i].thread);
00089     }
00090     if(c[0].work_sem) CloseHandle(c[0].work_sem);
00091     if(c[0].job_sem)  CloseHandle(c[0].job_sem);
00092     if(c[0].done_sem) CloseHandle(c[0].done_sem);
00093 
00094     av_freep(&s->thread_opaque);
00095 }
00096 
00097 static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
00098     ThreadContext *c= s->thread_opaque;
00099     int i;
00100     int jobnr = 0;
00101 
00102     assert(s == c->avctx);
00103 
00104     /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
00105 
00106     for(i=0; i<s->thread_count; i++){
00107         c[i].arg= arg;
00108         c[i].argsize= size;
00109         c[i].func= func;
00110         c[i].ret= ret;
00111         c[i].jobnr = &jobnr;
00112     }
00113     ReleaseSemaphore(c[0].work_sem, count, 0);
00114     for(i=0; i<count; i++)
00115         WaitForSingleObject(c[0].done_sem, INFINITE);
00116 
00117     return 0;
00118 }
00119 
00120 static int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
00121     ThreadContext *c= s->thread_opaque;
00122     int i;
00123     for(i=0; i<s->thread_count; i++)
00124         c[i].func2 = func;
00125     avcodec_thread_execute(s, NULL, arg, ret, count, 0);
00126 }
00127 
00128 int ff_thread_init(AVCodecContext *s){
00129     int i;
00130     ThreadContext *c;
00131     uint32_t threadid;
00132 
00133     if(!(s->thread_type & FF_THREAD_SLICE)){
00134         av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
00135         return 0;
00136     }
00137 
00138     if (s->thread_count <= 1)
00139         return 0;
00140 
00141     s->active_thread_type= FF_THREAD_SLICE;
00142 
00143     assert(!s->thread_opaque);
00144     c= av_mallocz(sizeof(ThreadContext)*s->thread_count);
00145     s->thread_opaque= c;
00146     if(!(c[0].work_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
00147         goto fail;
00148     if(!(c[0].job_sem  = CreateSemaphore(NULL, 1, 1, NULL)))
00149         goto fail;
00150     if(!(c[0].done_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
00151         goto fail;
00152 
00153     for(i=0; i<s->thread_count; i++){
00154 //printf("init semaphors %d\n", i); fflush(stdout);
00155         c[i].avctx= s;
00156         c[i].work_sem = c[0].work_sem;
00157         c[i].job_sem  = c[0].job_sem;
00158         c[i].done_sem = c[0].done_sem;
00159         c[i].threadnr = i;
00160 
00161 //printf("create thread %d\n", i); fflush(stdout);
00162         c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
00163         if( !c[i].thread ) goto fail;
00164     }
00165 //printf("init done\n"); fflush(stdout);
00166 
00167     s->execute= avcodec_thread_execute;
00168     s->execute2= avcodec_thread_execute2;
00169 
00170     return 0;
00171 fail:
00172     ff_thread_free(s);
00173     return -1;
00174 }

Generated on Fri Feb 22 2013 07:24:29 for FFmpeg by  doxygen 1.7.1