[FFmpeg-devel] [PATCH 1/3] avutil/threadmessage: add av_thread_message_flush()
Nicolas George
george at nsup.org
Wed Dec 2 16:59:15 CET 2015
Le duodi 12 frimaire, an CCXXIV, Clement Boesch a écrit :
> From: Clément Bœsch <clement at stupeflix.com>
>
> ---
> libavutil/threadmessage.c | 32 ++++++++++++++++++++++++++++++++
> libavutil/threadmessage.h | 12 ++++++++++++
> 2 files changed, 44 insertions(+)
>
> diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c
> index b7fcbe2..66b5fc6 100644
> --- a/libavutil/threadmessage.c
> +++ b/libavutil/threadmessage.c
> @@ -40,6 +40,8 @@ struct AVThreadMessageQueue {
> int err_send;
> int err_recv;
> unsigned elsize;
> + void (*free_func)(void *msg);
> + void *tmp_msg;
> #else
> int dummy;
> #endif
> @@ -81,10 +83,21 @@ int av_thread_message_queue_alloc(AVThreadMessageQueue **mq,
> #endif /* HAVE_THREADS */
> }
>
> +int av_thread_message_queue_set_free_func(AVThreadMessageQueue *mq,
> + void (*free_func)(void *msg))
> +{
> + mq->free_func = free_func;
> + av_freep(&mq->tmp_msg);
> + mq->tmp_msg = av_mallocz(mq->elsize);
> + return mq->tmp_msg ? 0 : AVERROR(ENOMEM);
> +}
> +
> void av_thread_message_queue_free(AVThreadMessageQueue **mq)
> {
> #if HAVE_THREADS
> if (*mq) {
> + av_thread_message_flush(*mq);
> + av_freep(&(*mq)->tmp_msg);
> av_fifo_freep(&(*mq)->fifo);
> pthread_cond_destroy(&(*mq)->cond);
> pthread_mutex_destroy(&(*mq)->lock);
> @@ -182,3 +195,22 @@ void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
> pthread_mutex_unlock(&mq->lock);
> #endif /* HAVE_THREADS */
> }
> +
> +void av_thread_message_flush(AVThreadMessageQueue *mq)
> +{
> +#if HAVE_THREADS
> + int used, off;
> +
> + pthread_mutex_lock(&mq->lock);
> + used = av_fifo_size(mq->fifo);
> + if (mq->free_func) {
> + for (off = 0; off < used; off += mq->elsize) {
> + av_fifo_generic_peek_at(mq->fifo, mq->tmp_msg, off, mq->elsize, NULL);
> + mq->free_func(mq->tmp_msg);
Could this use av_fifo_generic_peek() to avoid the ugly extra allocation?
> + }
> + }
> + av_fifo_drain(mq->fifo, used);
> + pthread_cond_broadcast(&mq->cond);
> + pthread_mutex_unlock(&mq->lock);
> +#endif /* HAVE_THREADS */
> +}
> diff --git a/libavutil/threadmessage.h b/libavutil/threadmessage.h
> index a8481d8..c59cb06 100644
> --- a/libavutil/threadmessage.h
> +++ b/libavutil/threadmessage.h
> @@ -88,4 +88,16 @@ void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq,
> void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
> int err);
>
> +/**
> + * Set the optional free message callback function which will be called if an
> + * operation is removing messages from the queue.
> + */
> +int av_thread_message_queue_set_free_func(AVThreadMessageQueue *mq,
> + void (*free_func)(void *msg));
> +
> +/**
> + * Flush the message queue
I will not block for that, but I am still not satisfied with the
explanations, and therefore I find this documentation insufficient. Maybe
wait for other advice?
> + */
> +void av_thread_message_flush(AVThreadMessageQueue *mq);
> +
> #endif /* AVUTIL_THREADMESSAGE_H */
Regards,
--
Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20151202/b6a6f388/attachment.sig>
More information about the ffmpeg-devel
mailing list