LCOV - code coverage report
Current view: top level - libavutil - threadmessage.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 85 101 84.2 %
Date: 2018-05-20 11:54:08 Functions: 12 12 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2014 Nicolas George
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public License
       8             :  * as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public License
      17             :  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
      18             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include "fifo.h"
      22             : #include "threadmessage.h"
      23             : #include "thread.h"
      24             : 
      25             : struct AVThreadMessageQueue {
      26             : #if HAVE_THREADS
      27             :     AVFifoBuffer *fifo;
      28             :     pthread_mutex_t lock;
      29             :     pthread_cond_t cond_recv;
      30             :     pthread_cond_t cond_send;
      31             :     int err_send;
      32             :     int err_recv;
      33             :     unsigned elsize;
      34             :     void (*free_func)(void *msg);
      35             : #else
      36             :     int dummy;
      37             : #endif
      38             : };
      39             : 
      40         142 : int av_thread_message_queue_alloc(AVThreadMessageQueue **mq,
      41             :                                   unsigned nelem,
      42             :                                   unsigned elsize)
      43             : {
      44             : #if HAVE_THREADS
      45             :     AVThreadMessageQueue *rmq;
      46         142 :     int ret = 0;
      47             : 
      48         142 :     if (nelem > INT_MAX / elsize)
      49           0 :         return AVERROR(EINVAL);
      50         142 :     if (!(rmq = av_mallocz(sizeof(*rmq))))
      51           0 :         return AVERROR(ENOMEM);
      52         142 :     if ((ret = pthread_mutex_init(&rmq->lock, NULL))) {
      53           0 :         av_free(rmq);
      54           0 :         return AVERROR(ret);
      55             :     }
      56         142 :     if ((ret = pthread_cond_init(&rmq->cond_recv, NULL))) {
      57           0 :         pthread_mutex_destroy(&rmq->lock);
      58           0 :         av_free(rmq);
      59           0 :         return AVERROR(ret);
      60             :     }
      61         142 :     if ((ret = pthread_cond_init(&rmq->cond_send, NULL))) {
      62           0 :         pthread_cond_destroy(&rmq->cond_recv);
      63           0 :         pthread_mutex_destroy(&rmq->lock);
      64           0 :         av_free(rmq);
      65           0 :         return AVERROR(ret);
      66             :     }
      67         142 :     if (!(rmq->fifo = av_fifo_alloc(elsize * nelem))) {
      68           0 :         pthread_cond_destroy(&rmq->cond_send);
      69           0 :         pthread_cond_destroy(&rmq->cond_recv);
      70           0 :         pthread_mutex_destroy(&rmq->lock);
      71           0 :         av_free(rmq);
      72           0 :         return AVERROR(ENOMEM);
      73             :     }
      74         142 :     rmq->elsize = elsize;
      75         142 :     *mq = rmq;
      76         142 :     return 0;
      77             : #else
      78             :     *mq = NULL;
      79             :     return AVERROR(ENOSYS);
      80             : #endif /* HAVE_THREADS */
      81             : }
      82             : 
      83           7 : void av_thread_message_queue_set_free_func(AVThreadMessageQueue *mq,
      84             :                                            void (*free_func)(void *msg))
      85             : {
      86             : #if HAVE_THREADS
      87           7 :     mq->free_func = free_func;
      88             : #endif
      89           7 : }
      90             : 
      91         142 : void av_thread_message_queue_free(AVThreadMessageQueue **mq)
      92             : {
      93             : #if HAVE_THREADS
      94         142 :     if (*mq) {
      95         142 :         av_thread_message_flush(*mq);
      96         142 :         av_fifo_freep(&(*mq)->fifo);
      97         142 :         pthread_cond_destroy(&(*mq)->cond_send);
      98         142 :         pthread_cond_destroy(&(*mq)->cond_recv);
      99         142 :         pthread_mutex_destroy(&(*mq)->lock);
     100         142 :         av_freep(mq);
     101             :     }
     102             : #endif
     103         142 : }
     104             : 
     105           3 : int av_thread_message_queue_nb_elems(AVThreadMessageQueue *mq)
     106             : {
     107             : #if HAVE_THREADS
     108             :     int ret;
     109           3 :     pthread_mutex_lock(&mq->lock);
     110           3 :     ret = av_fifo_size(mq->fifo);
     111           3 :     pthread_mutex_unlock(&mq->lock);
     112           3 :     return ret / mq->elsize;
     113             : #else
     114             :     return AVERROR(ENOSYS);
     115             : #endif
     116             : }
     117             : 
     118             : #if HAVE_THREADS
     119             : 
     120        6903 : static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
     121             :                                                void *msg,
     122             :                                                unsigned flags)
     123             : {
     124       19150 :     while (!mq->err_send && av_fifo_space(mq->fifo) < mq->elsize) {
     125        5404 :         if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
     126          60 :             return AVERROR(EAGAIN);
     127        5344 :         pthread_cond_wait(&mq->cond_send, &mq->lock);
     128             :     }
     129        6843 :     if (mq->err_send)
     130          99 :         return mq->err_send;
     131        6744 :     av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL);
     132             :     /* one message is sent, signal one receiver */
     133        6744 :     pthread_cond_signal(&mq->cond_recv);
     134        6744 :     return 0;
     135             : }
     136             : 
     137     1069989 : static int av_thread_message_queue_recv_locked(AVThreadMessageQueue *mq,
     138             :                                                void *msg,
     139             :                                                unsigned flags)
     140             : {
     141     2140044 :     while (!mq->err_recv && av_fifo_size(mq->fifo) < mq->elsize) {
     142     1063158 :         if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
     143     1063092 :             return AVERROR(EAGAIN);
     144          66 :         pthread_cond_wait(&mq->cond_recv, &mq->lock);
     145             :     }
     146        6897 :     if (av_fifo_size(mq->fifo) < mq->elsize)
     147         183 :         return mq->err_recv;
     148        6714 :     av_fifo_generic_read(mq->fifo, msg, mq->elsize, NULL);
     149             :     /* one message space appeared, signal one sender */
     150        6714 :     pthread_cond_signal(&mq->cond_send);
     151        6714 :     return 0;
     152             : }
     153             : 
     154             : #endif /* HAVE_THREADS */
     155             : 
     156        6903 : int av_thread_message_queue_send(AVThreadMessageQueue *mq,
     157             :                                  void *msg,
     158             :                                  unsigned flags)
     159             : {
     160             : #if HAVE_THREADS
     161             :     int ret;
     162             : 
     163        6903 :     pthread_mutex_lock(&mq->lock);
     164        6903 :     ret = av_thread_message_queue_send_locked(mq, msg, flags);
     165        6903 :     pthread_mutex_unlock(&mq->lock);
     166        6903 :     return ret;
     167             : #else
     168             :     return AVERROR(ENOSYS);
     169             : #endif /* HAVE_THREADS */
     170             : }
     171             : 
     172     1069989 : int av_thread_message_queue_recv(AVThreadMessageQueue *mq,
     173             :                                  void *msg,
     174             :                                  unsigned flags)
     175             : {
     176             : #if HAVE_THREADS
     177             :     int ret;
     178             : 
     179     1069989 :     pthread_mutex_lock(&mq->lock);
     180     1069989 :     ret = av_thread_message_queue_recv_locked(mq, msg, flags);
     181     1069989 :     pthread_mutex_unlock(&mq->lock);
     182     1069989 :     return ret;
     183             : #else
     184             :     return AVERROR(ENOSYS);
     185             : #endif /* HAVE_THREADS */
     186             : }
     187             : 
     188         143 : void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq,
     189             :                                           int err)
     190             : {
     191             : #if HAVE_THREADS
     192         143 :     pthread_mutex_lock(&mq->lock);
     193         143 :     mq->err_send = err;
     194         143 :     pthread_cond_broadcast(&mq->cond_send);
     195         143 :     pthread_mutex_unlock(&mq->lock);
     196             : #endif /* HAVE_THREADS */
     197         143 : }
     198             : 
     199         151 : void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
     200             :                                           int err)
     201             : {
     202             : #if HAVE_THREADS
     203         151 :     pthread_mutex_lock(&mq->lock);
     204         151 :     mq->err_recv = err;
     205         151 :     pthread_cond_broadcast(&mq->cond_recv);
     206         151 :     pthread_mutex_unlock(&mq->lock);
     207             : #endif /* HAVE_THREADS */
     208         151 : }
     209             : 
     210             : #if HAVE_THREADS
     211          30 : static void free_func_wrap(void *arg, void *msg, int size)
     212             : {
     213          30 :     AVThreadMessageQueue *mq = arg;
     214          30 :     mq->free_func(msg);
     215          30 : }
     216             : #endif
     217             : 
     218         159 : void av_thread_message_flush(AVThreadMessageQueue *mq)
     219             : {
     220             : #if HAVE_THREADS
     221             :     int used, off;
     222         159 :     void *free_func = mq->free_func;
     223             : 
     224         159 :     pthread_mutex_lock(&mq->lock);
     225         159 :     used = av_fifo_size(mq->fifo);
     226         159 :     if (free_func)
     227          54 :         for (off = 0; off < used; off += mq->elsize)
     228          30 :             av_fifo_generic_peek_at(mq->fifo, mq, off, mq->elsize, free_func_wrap);
     229         159 :     av_fifo_drain(mq->fifo, used);
     230             :     /* only the senders need to be notified since the queue is empty and there
     231             :      * is nothing to read */
     232         159 :     pthread_cond_broadcast(&mq->cond_send);
     233         159 :     pthread_mutex_unlock(&mq->lock);
     234             : #endif /* HAVE_THREADS */
     235         159 : }

Generated by: LCOV version 1.13