libavcodec/w32thread.c
16806499
 /*
  * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
16806499
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
16806499
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
16806499
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16806499
  */
 //#define DEBUG
 
 #include "avcodec.h"
74711b72
 #include "thread.h"
16806499
 
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <process.h>
 
 typedef struct ThreadContext{
     AVCodecContext *avctx;
     HANDLE thread;
     HANDLE work_sem;
1e6ff470
     HANDLE job_sem;
16806499
     HANDLE done_sem;
     int (*func)(AVCodecContext *c, void *arg);
7537726f
     int (*func2)(AVCodecContext *c, void *arg, int, int);
16806499
     void *arg;
1e6ff470
     int argsize;
     int *jobnr;
     int *ret;
7537726f
     int threadnr;
16806499
 }ThreadContext;
 
 
5dea9409
 static unsigned WINAPI attribute_align_arg thread_func(void *v){
16806499
     ThreadContext *c= v;
 
     for(;;){
1e6ff470
         int ret, jobnr;
16806499
 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
         WaitForSingleObject(c->work_sem, INFINITE);
9fe08942
         // avoid trying to access jobnr if we should quit
         if (!c->func && !c->func2)
             break;
1e6ff470
         WaitForSingleObject(c->job_sem, INFINITE);
         jobnr = (*c->jobnr)++;
         ReleaseSemaphore(c->job_sem, 1, 0);
16806499
 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
         if(c->func)
1e6ff470
             ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
16806499
         else
9fe08942
             ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
1e6ff470
         if (c->ret)
             c->ret[jobnr] = ret;
16806499
 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
         ReleaseSemaphore(c->done_sem, 1, 0);
     }
115329f1
 
16806499
     return 0;
 }
 
 /**
043d2ff2
  * Free what has been allocated by ff_thread_init().
755bfeab
  * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
16806499
  */
043d2ff2
 void ff_thread_free(AVCodecContext *s){
16806499
     ThreadContext *c= s->thread_opaque;
     int i;
 
     for(i=0; i<s->thread_count; i++){
115329f1
 
16806499
         c[i].func= NULL;
7537726f
         c[i].func2= NULL;
1e6ff470
     }
     ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
     for(i=0; i<s->thread_count; i++){
16806499
         WaitForSingleObject(c[i].thread, INFINITE);
51fdb6f0
         if(c[i].thread)   CloseHandle(c[i].thread);
16806499
     }
1e6ff470
     if(c[0].work_sem) CloseHandle(c[0].work_sem);
     if(c[0].job_sem)  CloseHandle(c[0].job_sem);
     if(c[0].done_sem) CloseHandle(c[0].done_sem);
16806499
 
     av_freep(&s->thread_opaque);
 }
 
4a287145
 static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
16806499
     ThreadContext *c= s->thread_opaque;
     int i;
1e6ff470
     int jobnr = 0;
115329f1
 
16806499
     assert(s == c->avctx);
115329f1
 
16806499
     /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
 
1e6ff470
     for(i=0; i<s->thread_count; i++){
         c[i].arg= arg;
         c[i].argsize= size;
16806499
         c[i].func= func;
1e6ff470
         c[i].ret= ret;
         c[i].jobnr = &jobnr;
16806499
     }
1e6ff470
     ReleaseSemaphore(c[0].work_sem, count, 0);
     for(i=0; i<count; i++)
         WaitForSingleObject(c[0].done_sem, INFINITE);
115329f1
 
16806499
     return 0;
 }
 
9b066ecb
 static int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
7537726f
     ThreadContext *c= s->thread_opaque;
     int i;
     for(i=0; i<s->thread_count; i++)
         c[i].func2 = func;
     avcodec_thread_execute(s, NULL, arg, ret, count, 0);
 }
 
46027c72
 int ff_thread_init(AVCodecContext *s){
16806499
     int i;
     ThreadContext *c;
     uint32_t threadid;
 
b38f008e
     if(!(s->thread_type & FF_THREAD_SLICE)){
         av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
         return 0;
     }
 
dd9134c2
     if (s->thread_count <= 1)
68cf92ee
         return 0;
 
0aed4f81
     s->active_thread_type= FF_THREAD_SLICE;
 
16806499
     assert(!s->thread_opaque);
dd9134c2
     c= av_mallocz(sizeof(ThreadContext)*s->thread_count);
16806499
     s->thread_opaque= c;
1e6ff470
     if(!(c[0].work_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
         goto fail;
     if(!(c[0].job_sem  = CreateSemaphore(NULL, 1, 1, NULL)))
         goto fail;
     if(!(c[0].done_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
         goto fail;
115329f1
 
dd9134c2
     for(i=0; i<s->thread_count; i++){
16806499
 //printf("init semaphors %d\n", i); fflush(stdout);
         c[i].avctx= s;
1e6ff470
         c[i].work_sem = c[0].work_sem;
         c[i].job_sem  = c[0].job_sem;
         c[i].done_sem = c[0].done_sem;
7537726f
         c[i].threadnr = i;
16806499
 
 //printf("create thread %d\n", i); fflush(stdout);
         c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
         if( !c[i].thread ) goto fail;
     }
 //printf("init done\n"); fflush(stdout);
115329f1
 
16806499
     s->execute= avcodec_thread_execute;
7537726f
     s->execute2= avcodec_thread_execute2;
16806499
 
     return 0;
 fail:
043d2ff2
     ff_thread_free(s);
16806499
     return -1;
 }