static int thread_init(AVCodecContext *avctx) { int i; ThreadContext *c; int thread_count = avctx->thread_count; if (!thread_count) { int nb_cpus = get_logical_cpus(avctx); // use number of cores + 1 as thread count if there is more than one if (nb_cpus > 1) thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); else thread_count = avctx->thread_count = 1; } if (thread_count <= 1) { avctx->active_thread_type = 0; return 0; } c = av_mallocz(sizeof(ThreadContext)); if (!c) return -1; c->workers = av_mallocz(sizeof(pthread_t)*thread_count); if (!c->workers) { av_free(c); return -1; } avctx->thread_opaque = c; c->current_job = 0; c->job_count = 0; c->job_size = 0; c->done = 0; pthread_cond_init(&c->current_job_cond, NULL); pthread_cond_init(&c->last_job_cond, NULL); pthread_mutex_init(&c->current_job_lock, NULL); pthread_mutex_lock(&c->current_job_lock); for (i=0; i<thread_count; i++) { if(pthread_create(&c->workers[i], NULL, worker, avctx)) { avctx->thread_count = i; pthread_mutex_unlock(&c->current_job_lock); ff_thread_free(avctx); return -1; } } avcodec_thread_park_workers(c, thread_count); avctx->execute = avcodec_thread_execute; avctx->execute2 = avcodec_thread_execute2; return 0; }
int ff_thread_init(AVCodecContext *s){ int i; ThreadContext *c; uint32_t threadid; if (s->thread_type && !(s->thread_type & FF_THREAD_SLICE)) { av_log(s, AV_LOG_WARNING, "This thread library only supports FF_THREAD_SLICE" " threading algorithm.\n"); return 0; } if (s->thread_count <= 1) return 0; s->active_thread_type= FF_THREAD_SLICE; assert(!s->thread_opaque); c= av_mallocz(sizeof(ThreadContext)*s->thread_count); s->thread_opaque= c; 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; for(i=0; i<s->thread_count; i++){ //printf("init semaphors %d\n", i); fflush(stdout); c[i].avctx= s; c[i].work_sem = c[0].work_sem; c[i].job_sem = c[0].job_sem; c[i].done_sem = c[0].done_sem; c[i].threadnr = i; //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); s->execute= avcodec_thread_execute; s->execute2= avcodec_thread_execute2; return 0; fail: ff_thread_free(s); return -1; }