LCOV - code coverage report
Current view: top level - libavutil - slicethread.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 107 119 89.9 %
Date: 2018-05-20 11:54:08 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include <stdatomic.h>
      20             : #include "slicethread.h"
      21             : #include "mem.h"
      22             : #include "thread.h"
      23             : #include "avassert.h"
      24             : 
      25             : #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
      26             : 
      27             : typedef struct WorkerContext {
      28             :     AVSliceThread   *ctx;
      29             :     pthread_mutex_t mutex;
      30             :     pthread_cond_t  cond;
      31             :     pthread_t       thread;
      32             :     int             done;
      33             : } WorkerContext;
      34             : 
      35             : struct AVSliceThread {
      36             :     WorkerContext   *workers;
      37             :     int             nb_threads;
      38             :     int             nb_active_threads;
      39             :     int             nb_jobs;
      40             : 
      41             :     atomic_uint     first_job;
      42             :     atomic_uint     current_job;
      43             :     pthread_mutex_t done_mutex;
      44             :     pthread_cond_t  done_cond;
      45             :     int             done;
      46             :     int             finished;
      47             : 
      48             :     void            *priv;
      49             :     void            (*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads);
      50             :     void            (*main_func)(void *priv);
      51             : };
      52             : 
      53       23321 : static int run_jobs(AVSliceThread *ctx)
      54             : {
      55       23321 :     unsigned nb_jobs    = ctx->nb_jobs;
      56       23321 :     unsigned nb_active_threads = ctx->nb_active_threads;
      57       23321 :     unsigned first_job    = atomic_fetch_add_explicit(&ctx->first_job, 1, memory_order_acq_rel);
      58       23321 :     unsigned current_job  = first_job;
      59             : 
      60             :     do {
      61       23321 :         ctx->worker_func(ctx->priv, current_job, first_job, nb_jobs, nb_active_threads);
      62       23321 :     } while ((current_job = atomic_fetch_add_explicit(&ctx->current_job, 1, memory_order_acq_rel)) < nb_jobs);
      63             : 
      64       23321 :     return current_job == nb_jobs + nb_active_threads - 1;
      65             : }
      66             : 
      67       20169 : static void *attribute_align_arg thread_worker(void *v)
      68             : {
      69       20169 :     WorkerContext *w = v;
      70       20169 :     AVSliceThread *ctx = w->ctx;
      71             : 
      72       20169 :     pthread_mutex_lock(&w->mutex);
      73       20169 :     pthread_cond_signal(&w->cond);
      74             : 
      75             :     while (1) {
      76       59821 :         w->done = 1;
      77      119985 :         while (w->done)
      78       39995 :             pthread_cond_wait(&w->cond, &w->mutex);
      79             : 
      80       39995 :         if (ctx->finished) {
      81       20169 :             pthread_mutex_unlock(&w->mutex);
      82       40338 :             return NULL;
      83             :         }
      84             : 
      85       19826 :         if (run_jobs(ctx)) {
      86        1565 :             pthread_mutex_lock(&ctx->done_mutex);
      87        1565 :             ctx->done = 1;
      88        1565 :             pthread_cond_signal(&ctx->done_cond);
      89        1565 :             pthread_mutex_unlock(&ctx->done_mutex);
      90             :         }
      91             :     }
      92             : }
      93             : 
      94        2544 : int avpriv_slicethread_create(AVSliceThread **pctx, void *priv,
      95             :                               void (*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads),
      96             :                               void (*main_func)(void *priv),
      97             :                               int nb_threads)
      98             : {
      99             :     AVSliceThread *ctx;
     100             :     int nb_workers, i;
     101             : 
     102        2544 :     av_assert0(nb_threads >= 0);
     103        2544 :     if (!nb_threads) {
     104        2517 :         int nb_cpus = av_cpu_count();
     105        2517 :         if (nb_cpus > 1)
     106        2517 :             nb_threads = nb_cpus + 1;
     107             :         else
     108           0 :             nb_threads = 1;
     109             :     }
     110             : 
     111        2544 :     nb_workers = nb_threads;
     112        2544 :     if (!main_func)
     113        2544 :         nb_workers--;
     114             : 
     115        2544 :     *pctx = ctx = av_mallocz(sizeof(*ctx));
     116        2544 :     if (!ctx)
     117           0 :         return AVERROR(ENOMEM);
     118             : 
     119        2544 :     if (nb_workers && !(ctx->workers = av_calloc(nb_workers, sizeof(*ctx->workers)))) {
     120           0 :         av_freep(pctx);
     121           0 :         return AVERROR(ENOMEM);
     122             :     }
     123             : 
     124        2544 :     ctx->priv        = priv;
     125        2544 :     ctx->worker_func = worker_func;
     126        2544 :     ctx->main_func   = main_func;
     127        2544 :     ctx->nb_threads  = nb_threads;
     128        2544 :     ctx->nb_active_threads = 0;
     129        2544 :     ctx->nb_jobs     = 0;
     130        2544 :     ctx->finished    = 0;
     131             : 
     132        2544 :     atomic_init(&ctx->first_job, 0);
     133        2544 :     atomic_init(&ctx->current_job, 0);
     134        2544 :     pthread_mutex_init(&ctx->done_mutex, NULL);
     135        2544 :     pthread_cond_init(&ctx->done_cond, NULL);
     136        2544 :     ctx->done        = 0;
     137             : 
     138       22713 :     for (i = 0; i < nb_workers; i++) {
     139       20169 :         WorkerContext *w = &ctx->workers[i];
     140             :         int ret;
     141       20169 :         w->ctx = ctx;
     142       20169 :         pthread_mutex_init(&w->mutex, NULL);
     143       20169 :         pthread_cond_init(&w->cond, NULL);
     144       20169 :         pthread_mutex_lock(&w->mutex);
     145       20169 :         w->done = 0;
     146             : 
     147       20169 :         if (ret = pthread_create(&w->thread, NULL, thread_worker, w)) {
     148           0 :             ctx->nb_threads = main_func ? i : i + 1;
     149           0 :             pthread_mutex_unlock(&w->mutex);
     150           0 :             pthread_cond_destroy(&w->cond);
     151           0 :             pthread_mutex_destroy(&w->mutex);
     152           0 :             avpriv_slicethread_free(pctx);
     153           0 :             return AVERROR(ret);
     154             :         }
     155             : 
     156       60507 :         while (!w->done)
     157       20169 :             pthread_cond_wait(&w->cond, &w->mutex);
     158       20169 :         pthread_mutex_unlock(&w->mutex);
     159             :     }
     160             : 
     161        2544 :     return nb_threads;
     162             : }
     163             : 
     164        3495 : void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
     165             : {
     166        3495 :     int nb_workers, i, is_last = 0;
     167             : 
     168        3495 :     av_assert0(nb_jobs > 0);
     169        3495 :     ctx->nb_jobs           = nb_jobs;
     170        3495 :     ctx->nb_active_threads = FFMIN(nb_jobs, ctx->nb_threads);
     171        3495 :     atomic_store_explicit(&ctx->first_job, 0, memory_order_relaxed);
     172        3495 :     atomic_store_explicit(&ctx->current_job, ctx->nb_active_threads, memory_order_relaxed);
     173        3495 :     nb_workers             = ctx->nb_active_threads;
     174        3495 :     if (!ctx->main_func || !execute_main)
     175        3495 :         nb_workers--;
     176             : 
     177       23321 :     for (i = 0; i < nb_workers; i++) {
     178       19826 :         WorkerContext *w = &ctx->workers[i];
     179       19826 :         pthread_mutex_lock(&w->mutex);
     180       19826 :         w->done = 0;
     181       19826 :         pthread_cond_signal(&w->cond);
     182       19826 :         pthread_mutex_unlock(&w->mutex);
     183             :     }
     184             : 
     185        3495 :     if (ctx->main_func && execute_main)
     186           0 :         ctx->main_func(ctx->priv);
     187             :     else
     188        3495 :         is_last = run_jobs(ctx);
     189             : 
     190        3495 :     if (!is_last) {
     191        1565 :         pthread_mutex_lock(&ctx->done_mutex);
     192        4695 :         while (!ctx->done)
     193        1565 :             pthread_cond_wait(&ctx->done_cond, &ctx->done_mutex);
     194        1565 :         ctx->done = 0;
     195        1565 :         pthread_mutex_unlock(&ctx->done_mutex);
     196             :     }
     197        3495 : }
     198             : 
     199        2544 : void avpriv_slicethread_free(AVSliceThread **pctx)
     200             : {
     201             :     AVSliceThread *ctx;
     202             :     int nb_workers, i;
     203             : 
     204        2544 :     if (!pctx || !*pctx)
     205           0 :         return;
     206             : 
     207        2544 :     ctx = *pctx;
     208        2544 :     nb_workers = ctx->nb_threads;
     209        2544 :     if (!ctx->main_func)
     210        2544 :         nb_workers--;
     211             : 
     212        2544 :     ctx->finished = 1;
     213       22713 :     for (i = 0; i < nb_workers; i++) {
     214       20169 :         WorkerContext *w = &ctx->workers[i];
     215       20169 :         pthread_mutex_lock(&w->mutex);
     216       20169 :         w->done = 0;
     217       20169 :         pthread_cond_signal(&w->cond);
     218       20169 :         pthread_mutex_unlock(&w->mutex);
     219             :     }
     220             : 
     221       22713 :     for (i = 0; i < nb_workers; i++) {
     222       20169 :         WorkerContext *w = &ctx->workers[i];
     223       20169 :         pthread_join(w->thread, NULL);
     224       20169 :         pthread_cond_destroy(&w->cond);
     225       20169 :         pthread_mutex_destroy(&w->mutex);
     226             :     }
     227             : 
     228        2544 :     pthread_cond_destroy(&ctx->done_cond);
     229        2544 :     pthread_mutex_destroy(&ctx->done_mutex);
     230        2544 :     av_freep(&ctx->workers);
     231        2544 :     av_freep(pctx);
     232             : }
     233             : 
     234             : #else /* HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS32THREADS */
     235             : 
     236             : int avpriv_slicethread_create(AVSliceThread **pctx, void *priv,
     237             :                               void (*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads),
     238             :                               void (*main_func)(void *priv),
     239             :                               int nb_threads)
     240             : {
     241             :     *pctx = NULL;
     242             :     return AVERROR(EINVAL);
     243             : }
     244             : 
     245             : void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
     246             : {
     247             :     av_assert0(0);
     248             : }
     249             : 
     250             : void avpriv_slicethread_free(AVSliceThread **pctx)
     251             : {
     252             :     av_assert0(!pctx || !*pctx);
     253             : }
     254             : 
     255             : #endif /* HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS32THREADS */

Generated by: LCOV version 1.13