[PATCH 1/2] lavu: add thread message API.
TODO minor bump and APIchanges entry. Signed-off-by: Nicolas George <george@nsup.org> --- libavutil/Makefile | 2 + libavutil/threadmessage.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++ libavutil/threadmessage.h | 92 +++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 libavutil/threadmessage.c create mode 100644 libavutil/threadmessage.h diff --git a/libavutil/Makefile b/libavutil/Makefile index e4ee47c..4b173d8 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -50,6 +50,7 @@ HEADERS = adler32.h \ sha.h \ sha512.h \ stereo3d.h \ + threadmessage.h \ time.h \ timecode.h \ timestamp.h \ @@ -115,6 +116,7 @@ OBJS = adler32.o \ sha.o \ sha512.o \ stereo3d.o \ + threadmessage.o \ time.o \ timecode.o \ tree.o \ diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c new file mode 100644 index 0000000..5389246 --- /dev/null +++ b/libavutil/threadmessage.c @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2014 Nicolas George + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 License + * along with FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <pthread.h> +#include "fifo.h" +#include "threadmessage.h" + +#include <pthread.h> +#include "fifo.h" + +struct AVThreadMessageQueue { + AVFifoBuffer *fifo; + pthread_mutex_t lock; + pthread_cond_t cond; + int err_send; + int err_recv; + unsigned elsize; +}; + +int av_thread_message_queue_alloc(AVThreadMessageQueue **mq, + unsigned elsize, + unsigned nelem) +{ +#if HAVE_THREADS + AVThreadMessageQueue *rmq; + int ret = 0; + + if (nelem > INT_MAX / elsize) + return AVERROR(EINVAL); + if (!(rmq = av_mallocz(sizeof(*rmq)))) + return AVERROR(ENOMEM); + if ((ret = pthread_mutex_init(&rmq->lock, NULL))) { + av_free(rmq); + return AVERROR(ret); + } + if ((ret = pthread_cond_init(&rmq->cond, NULL))) { + pthread_mutex_destroy(&rmq->lock); + av_free(rmq); + return AVERROR(ret); + } + if (!(rmq->fifo = av_fifo_alloc(elsize * nelem))) { + pthread_cond_destroy(&rmq->cond); + pthread_mutex_destroy(&rmq->lock); + av_free(rmq); + return AVERROR(ret); + } + rmq->elsize = elsize; + *mq = rmq; + return 0; +#else + return AVERROR(ENOSYS); +#endif /* HAVE_THREADS */ +} + +void av_thread_message_queue_free(AVThreadMessageQueue **mq) +{ + if (*mq) { + av_fifo_free((*mq)->fifo); +#if HAVE_THREADS + pthread_cond_destroy(&(*mq)->cond); + pthread_mutex_destroy(&(*mq)->lock); +#endif + av_freep(mq); + } +} + +#if HAVE_THREADS + +static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq, + void *msg, + unsigned flags) +{ + while (!mq->err_send && av_fifo_space(mq->fifo) < mq->elsize) { + if ((flags & AV_THREAD_MESSAGE_NONBLOCK)) + return AVERROR(EAGAIN); + pthread_cond_wait(&mq->cond, &mq->lock); + } + if (mq->err_send) + return mq->err_send; + av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL); + pthread_cond_signal(&mq->cond); + return 0; +} + +static int av_thread_message_queue_recv_locked(AVThreadMessageQueue *mq, + void *msg, + unsigned flags) +{ + while (!mq->err_recv && av_fifo_size(mq->fifo) < mq->elsize) { + if ((flags & AV_THREAD_MESSAGE_NONBLOCK)) + return AVERROR(EAGAIN); + pthread_cond_wait(&mq->cond, &mq->lock); + } + if (av_fifo_size(mq->fifo) < mq->elsize) + return mq->err_recv; + av_fifo_generic_read(mq->fifo, msg, mq->elsize, NULL); + pthread_cond_signal(&mq->cond); + return 0; +} + +#endif /* HAVE_THREADS */ + +int av_thread_message_queue_send(AVThreadMessageQueue *mq, + void *msg, + unsigned flags) +{ +#if HAVE_THREADS + int ret; + + pthread_mutex_lock(&mq->lock); + ret = av_thread_message_queue_send_locked(mq, msg, flags); + pthread_mutex_unlock(&mq->lock); + return ret; +#else + return AVERROR(ENOSYS); +#endif /* HAVE_THREADS */ +} + +int av_thread_message_queue_recv(AVThreadMessageQueue *mq, + void *msg, + unsigned flags) +{ +#if HAVE_THREADS + int ret; + + pthread_mutex_lock(&mq->lock); + ret = av_thread_message_queue_recv_locked(mq, msg, flags); + pthread_mutex_unlock(&mq->lock); + return ret; +#else + return AVERROR(ENOSYS); +#endif /* HAVE_THREADS */ +} + +void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq, + int err) +{ +#if HAVE_THREADS + pthread_mutex_lock(&mq->lock); + mq->err_send = err; + pthread_cond_broadcast(&mq->cond); + pthread_mutex_unlock(&mq->lock); +#endif /* HAVE_THREADS */ +} + +void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq, + int err) +{ +#if HAVE_THREADS + pthread_mutex_lock(&mq->lock); + mq->err_recv = err; + pthread_cond_broadcast(&mq->cond); + pthread_mutex_unlock(&mq->lock); +#endif /* HAVE_THREADS */ +} diff --git a/libavutil/threadmessage.h b/libavutil/threadmessage.h new file mode 100644 index 0000000..0c77bcf --- /dev/null +++ b/libavutil/threadmessage.h @@ -0,0 +1,92 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 License + * along with FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVUTIL_THREADMESSAGE_H +#define AVUTIL_THREADMESSAGE_H + +#include <pthread.h> +#include "fifo.h" + +typedef struct AVThreadMessageQueue AVThreadMessageQueue; + +typedef enum AVThreadMessageFlags { + + /** + * Perform non-blocking operation. + * If this flag is set, send and recv operations are non-blocking and + * return AVERROR(EAGAIN) immediately if they can not proceed. + */ + AV_THREAD_MESSAGE_NONBLOCK = 1, + +} AVThreadMessageFlags; + +/** + * Allocate a new message queue. + * + * @param mq pointer to the message queue + * @param elsize size of each element in the queue + * @param nelem minimum number of elements in the queue + * @return >=0 for success; <0 for error, in particular AVERROR(ENOSYS) if + * lavu was built without thread support + */ +int av_thread_message_queue_alloc(AVThreadMessageQueue **mq, + unsigned elsize, + unsigned nelem); + +/** + * Free a message queue. + */ +void av_thread_message_queue_free(AVThreadMessageQueue **mq); + +/** + * Send a message on the queue. + */ +int av_thread_message_queue_send(AVThreadMessageQueue *mq, + void *msg, + unsigned flags); + +/** + * Receive a message from the queue. + */ +int av_thread_message_queue_recv(AVThreadMessageQueue *mq, + void *msg, + unsigned flags); + +/** + * Set the sending error code. + * + * If the error code is set to non-zero, av_thread_message_queue_recv() will + * return it immediately when there are no longer available messages. + * Conventional values, such as AVERROR_EOF or AVERROR(EAGAIN), can be used + * to cause the receiving thread to stop or suspend its operation. + */ +void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq, + int err); + +/** + * Set the receiving error code. + * + * If the error code is set to non-zero, av_thread_message_queue_send() will + * return it immediately. Conventional values, such as AVERROR_EOF or + * AVERROR(EAGAIN), can be used to cause the sending thread to stop or + * suspend its operation. + */ +void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq, + int err); + +#endif /* AVUTIL_THREADMESSAGE_H */ -- 1.8.5.3
Signed-off-by: Nicolas George <george@nsup.org> --- ffmpeg.c | 98 ++++++++++++++++++---------------------------------------------- ffmpeg.h | 5 ++-- 2 files changed, 29 insertions(+), 74 deletions(-) This applies on top of "ffmpeg: make reading packets from thread blocking.", because the logic to determine if the file should be non-blocking is still used. This makes the helgrind error disappear. diff --git a/ffmpeg.c b/ffmpeg.c index 28bebe5..d3defff 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -59,6 +59,7 @@ #include "libavutil/timestamp.h" #include "libavutil/bprint.h" #include "libavutil/time.h" +#include "libavutil/threadmessage.h" #include "libavformat/os_support.h" #include "libavformat/ffm.h" // not public API @@ -137,11 +138,6 @@ AVIOContext *progress_avio = NULL; static uint8_t *subtitle_out; -#if HAVE_PTHREADS -/* signal to input threads that they should exit; set by the main thread */ -static int transcoding_finished; -#endif - #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass" InputStream **input_streams = NULL; @@ -2943,32 +2939,31 @@ static void *input_thread(void *arg) InputFile *f = arg; int ret = 0; - while (!transcoding_finished && ret >= 0) { + while (1) { AVPacket pkt; ret = av_read_frame(f->ctx, &pkt); if (ret == AVERROR(EAGAIN)) { av_usleep(10000); - ret = 0; continue; - } else if (ret < 0) + } + if (ret < 0) { + av_thread_message_queue_set_err_recv(f->in_thread_queue, ret); break; - - pthread_mutex_lock(&f->fifo_lock); - while (!av_fifo_space(f->fifo)) - pthread_cond_wait(&f->fifo_cond, &f->fifo_lock); - + } av_dup_packet(&pkt); - av_fifo_generic_write(f->fifo, &pkt, sizeof(pkt), NULL); - pthread_cond_signal(&f->fifo_cond); - - pthread_mutex_unlock(&f->fifo_lock); + ret = av_thread_message_queue_send(f->in_thread_queue, &pkt, 0); + if (ret < 0) { + if (ret != AVERROR_EOF) + av_log(f->ctx, AV_LOG_ERROR, + "Unable to send packet to main thread: %s\n", + av_err2str(ret)); + av_free_packet(&pkt); + av_thread_message_queue_set_err_recv(f->in_thread_queue, ret); + break; + } } - pthread_mutex_lock(&f->fifo_lock); - f->finished = 1; - pthread_cond_signal(&f->fifo_cond); - pthread_mutex_unlock(&f->fifo_lock); return NULL; } @@ -2976,34 +2971,19 @@ static void free_input_threads(void) { int i; - if (nb_input_files == 1) - return; - - transcoding_finished = 1; - for (i = 0; i < nb_input_files; i++) { InputFile *f = input_files[i]; AVPacket pkt; - if (!f->fifo || f->joined) + if (!f->in_thread_queue) continue; - - pthread_mutex_lock(&f->fifo_lock); - while (av_fifo_size(f->fifo)) { - av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL); + av_thread_message_queue_set_err_send(f->in_thread_queue, AVERROR_EOF); + while (av_thread_message_queue_recv(f->in_thread_queue, &pkt, 0) >= 0) av_free_packet(&pkt); - } - pthread_cond_signal(&f->fifo_cond); - pthread_mutex_unlock(&f->fifo_lock); pthread_join(f->thread, NULL); f->joined = 1; - - while (av_fifo_size(f->fifo)) { - av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL); - av_free_packet(&pkt); - } - av_fifo_free(f->fifo); + av_thread_message_queue_free(&f->in_thread_queue); } } @@ -3017,15 +2997,13 @@ static int init_input_threads(void) for (i = 0; i < nb_input_files; i++) { InputFile *f = input_files[i]; - if (!(f->fifo = av_fifo_alloc(8*sizeof(AVPacket)))) - return AVERROR(ENOMEM); - if (f->ctx->pb ? !f->ctx->pb->seekable : strcmp(f->ctx->iformat->name, "lavfi")) f->non_blocking = 1; - - pthread_mutex_init(&f->fifo_lock, NULL); - pthread_cond_init (&f->fifo_cond, NULL); + ret = av_thread_message_queue_alloc(&f->in_thread_queue, + sizeof(AVPacket), 8); + if (ret < 0) + return ret; if ((ret = pthread_create(&f->thread, NULL, input_thread, f))) return AVERROR(ret); @@ -3035,31 +3013,9 @@ static int init_input_threads(void) static int get_input_packet_mt(InputFile *f, AVPacket *pkt) { - int ret = 0; - - pthread_mutex_lock(&f->fifo_lock); - - while (1) { - if (av_fifo_size(f->fifo)) { - av_fifo_generic_read(f->fifo, pkt, sizeof(*pkt), NULL); - pthread_cond_signal(&f->fifo_cond); - break; - } else { - if (f->finished) { - ret = AVERROR_EOF; - break; - } - if (f->non_blocking) { - ret = AVERROR(EAGAIN); - break; - } - pthread_cond_wait(&f->fifo_cond, &f->fifo_lock); - } - } - - pthread_mutex_unlock(&f->fifo_lock); - - return ret; + return av_thread_message_queue_recv(f->in_thread_queue, pkt, + f->non_blocking ? + AV_THREAD_MESSAGE_NONBLOCK : 0); } #endif diff --git a/ffmpeg.h b/ffmpeg.h index 151667d..8e3eefb 100644 --- a/ffmpeg.h +++ b/ffmpeg.h @@ -44,6 +44,7 @@ #include "libavutil/fifo.h" #include "libavutil/pixfmt.h" #include "libavutil/rational.h" +#include "libavutil/threadmessage.h" #include "libswresample/swresample.h" @@ -326,13 +327,11 @@ typedef struct InputFile { int accurate_seek; #if HAVE_PTHREADS + AVThreadMessageQueue *in_thread_queue; pthread_t thread; /* thread reading from this file */ int non_blocking; /* reading packets from the thread should not block */ int finished; /* the thread has exited */ int joined; /* the thread has been joined */ - pthread_mutex_t fifo_lock; /* lock for access to fifo */ - pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */ - AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */ #endif } InputFile; -- 1.8.5.3
On Thu, Feb 20, 2014 at 04:22:14PM +0100, Nicolas George wrote:
Signed-off-by: Nicolas George <george@nsup.org> --- ffmpeg.c | 98 ++++++++++++++++++---------------------------------------------- ffmpeg.h | 5 ++-- 2 files changed, 29 insertions(+), 74 deletions(-)
This applies on top of "ffmpeg: make reading packets from thread blocking.", because the logic to determine if the file should be non-blocking is still used.
This makes the helgrind error disappear.
\o/
diff --git a/ffmpeg.c b/ffmpeg.c index 28bebe5..d3defff 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -59,6 +59,7 @@ #include "libavutil/timestamp.h" #include "libavutil/bprint.h" #include "libavutil/time.h" +#include "libavutil/threadmessage.h" #include "libavformat/os_support.h"
#include "libavformat/ffm.h" // not public API @@ -137,11 +138,6 @@ AVIOContext *progress_avio = NULL;
static uint8_t *subtitle_out;
-#if HAVE_PTHREADS -/* signal to input threads that they should exit; set by the main thread */ -static int transcoding_finished; -#endif - #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
InputStream **input_streams = NULL; @@ -2943,32 +2939,31 @@ static void *input_thread(void *arg) InputFile *f = arg; int ret = 0;
- while (!transcoding_finished && ret >= 0) { + while (1) {
No risk by having this loop entered once while it previously wouldn't have? (like typically in case of a premature error?)
AVPacket pkt; ret = av_read_frame(f->ctx, &pkt);
[...] -- Clément B.
Le duodi 2 ventôse, an CCXXII, Clément Bœsch a écrit :
No risk by having this loop entered once while it previously wouldn't have? (like typically in case of a premature error?)
There is no risk of that: the code that did set transcoding_finished to 1 to stop the loop now calls av_thread_message_queue_set_err_send() and drains the queue, which will cause the packet to be discarded. Regards, -- Nicolas George
On Thu, Feb 20, 2014 at 04:22:13PM +0100, Nicolas George wrote:
TODO minor bump and APIchanges entry.
Signed-off-by: Nicolas George <george@nsup.org> --- libavutil/Makefile | 2 + libavutil/threadmessage.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++ libavutil/threadmessage.h | 92 +++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 libavutil/threadmessage.c create mode 100644 libavutil/threadmessage.h
diff --git a/libavutil/Makefile b/libavutil/Makefile index e4ee47c..4b173d8 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -50,6 +50,7 @@ HEADERS = adler32.h \ sha.h \ sha512.h \ stereo3d.h \ + threadmessage.h \ time.h \ timecode.h \ timestamp.h \ @@ -115,6 +116,7 @@ OBJS = adler32.o \ sha.o \ sha512.o \ stereo3d.o \ + threadmessage.o \ time.o \ timecode.o \ tree.o \ diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c new file mode 100644 index 0000000..5389246 --- /dev/null +++ b/libavutil/threadmessage.c @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2014 Nicolas George + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 License + * along with FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <pthread.h> +#include "fifo.h" +#include "threadmessage.h" +
+#include <pthread.h> +#include "fifo.h"
double include it seems. Also, I think pthread.h include should be conditional.
+struct AVThreadMessageQueue { + AVFifoBuffer *fifo;
+ pthread_mutex_t lock; + pthread_cond_t cond;
This might not compile without pthread
+ int err_send; + int err_recv; + unsigned elsize; +}; + +int av_thread_message_queue_alloc(AVThreadMessageQueue **mq, + unsigned elsize, + unsigned nelem) +{
+#if HAVE_THREADS
Code in ffmpeg.c seems to rely on HAVE_PTHREADS; is this fine? [...]
+int av_thread_message_queue_send(AVThreadMessageQueue *mq, + void *msg, + unsigned flags) +{ +#if HAVE_THREADS + int ret; + + pthread_mutex_lock(&mq->lock); + ret = av_thread_message_queue_send_locked(mq, msg, flags); + pthread_mutex_unlock(&mq->lock); + return ret; +#else + return AVERROR(ENOSYS); +#endif /* HAVE_THREADS */
so without thread, this will return ENOSYS; won't that cause problems with your new loop in ffmpeg.c? [...]
diff --git a/libavutil/threadmessage.h b/libavutil/threadmessage.h new file mode 100644 index 0000000..0c77bcf --- /dev/null +++ b/libavutil/threadmessage.h @@ -0,0 +1,92 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 License + * along with FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVUTIL_THREADMESSAGE_H +#define AVUTIL_THREADMESSAGE_H + +#include <pthread.h>
You should not have this include here since the prototypes do not depend on pthread specific code; if you did, it would have mean that the code can not compile without threading. [...] -- Clément B.
Le sextidi 6 ventôse, an CCXXII, Clément Bœsch a écrit :
double include it seems.
Fixed.
Also, I think pthread.h include should be conditional.
Fixed.
This might not compile without pthread
Fixed.
Code in ffmpeg.c seems to rely on HAVE_PTHREADS; is this fine?
I assume the w32-pthread compat code will take care of that. If someone could test on windows, that would be nice.
so without thread, this will return ENOSYS; won't that cause problems with your new loop in ffmpeg.c?
You mean if a ffmpeg compiled with threads is used with a lavu compiled without? Do we support that setup?
You should not have this include here since the prototypes do not depend on pthread specific code; if you did, it would have mean that the code can not compile without threading.
Fixed. New patch attached, no change on the second one except rebasing. FATE passes on Debian both with and without threads. Regards, -- Nicolas George
On Thu, Apr 17, 2014 at 04:55:21PM +0200, Nicolas George wrote: [...]
so without thread, this will return ENOSYS; won't that cause problems with your new loop in ffmpeg.c?
You mean if a ffmpeg compiled with threads is used with a lavu compiled without? Do we support that setup?
(just for the context, we are talking about av_thread_message_queue_send() returning ENOSYS in case HAVE_THREADS is false) I was under the impression the function calling av_thread_message_queue_send() was called even without threading, typically assuming that lavu/threadmessage.c would be a working API even without threading if possible. I guess I was wrong. No more comment from me. [...] -- Clément B.
On Thu, Apr 17, 2014 at 04:55:21PM +0200, Nicolas George wrote:
Le sextidi 6 ventôse, an CCXXII, Clément Bœsch a écrit :
double include it seems.
Fixed.
Also, I think pthread.h include should be conditional.
Fixed.
This might not compile without pthread
Fixed.
Code in ffmpeg.c seems to rely on HAVE_PTHREADS; is this fine?
I assume the w32-pthread compat code will take care of that. If someone could test on windows, that would be nice.
so without thread, this will return ENOSYS; won't that cause problems with your new loop in ffmpeg.c?
You mean if a ffmpeg compiled with threads is used with a lavu compiled without? Do we support that setup?
You should not have this include here since the prototypes do not depend on pthread specific code; if you did, it would have mean that the code can not compile without threading.
Fixed.
New patch attached, no change on the second one except rebasing. FATE passes on Debian both with and without threads.
Regards,
-- Nicolas George
Makefile | 2 threadmessage.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ threadmessage.h | 89 ++++++++++++++++++++++++++++ 3 files changed, 265 insertions(+) 4064f9e984a05443d17d8ba22d925becad01820e 0001-lavu-add-thread-message-API.patch From eb1230be20ba6765f7c44165742443ca02245089 Mon Sep 17 00:00:00 2001 From: Nicolas George <george@nsup.org> Date: Thu, 20 Feb 2014 14:14:34 +0100 Subject: [PATCH 1/2] lavu: add thread message API.
TODO minor bump and APIchanges entry.
Signed-off-by: Nicolas George <george@nsup.org> --- libavutil/Makefile | 2 + libavutil/threadmessage.c | 174 +++++++++++++++++++++++++++++++++++++++++++++ libavutil/threadmessage.h | 89 +++++++++++++++++++++++ 3 files changed, 265 insertions(+) create mode 100644 libavutil/threadmessage.c create mode 100644 libavutil/threadmessage.h
diff --git a/libavutil/Makefile b/libavutil/Makefile index d2ebebc..5082e92 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -51,6 +51,7 @@ HEADERS = adler32.h \ sha.h \ sha512.h \ stereo3d.h \ + threadmessage.h \ time.h \ timecode.h \ timestamp.h \ @@ -117,6 +118,7 @@ OBJS = adler32.o \ sha.o \ sha512.o \ stereo3d.o \ + threadmessage.o \ time.o \ timecode.o \ tree.o \ diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c new file mode 100644 index 0000000..effbb48 --- /dev/null +++ b/libavutil/threadmessage.c @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2014 Nicolas George + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 License + * along with FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "fifo.h" +#include "threadmessage.h"
+#if HAVE_THREADS +#include <pthread.h> +#endif
this will fail if there are no pthreads no more comments from me [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I have never wished to cater to the crowd; for what I know they do not approve, and what they approve I do not know. -- Epicurus
Le nonidi 29 germinal, an CCXXII, Michael Niedermayer a écrit :
this will fail if there are no pthreads
You are right. I remembered wrong how the compat system works. Updated version attached. Clément:
(just for the context, we are talking about av_thread_message_queue_send() returning ENOSYS in case HAVE_THREADS is false)
I was under the impression the function calling av_thread_message_queue_send() was called even without threading, typically assuming that lavu/threadmessage.c would be a working API even without threading if possible. I guess I was wrong.
Even if av_thread_message_queue_send() were to be called, it would fail since av_thread_message_queue_alloc() would have failed too and returned a NULL structure. I tested the last version with --disable-threads, and it worked, so I guess the #ifdef are correct. Regards, -- Nicolas George
Le primidi 1er floréal, an CCXXII, Nicolas George a écrit :
You are right. I remembered wrong how the compat system works. Updated version attached.
Sorry, forgot to commit before sending. Regards, -- Nicolas George
On Sun, Apr 20, 2014 at 07:58:00PM +0200, Nicolas George wrote:
Le primidi 1er floréal, an CCXXII, Nicolas George a écrit :
You are right. I remembered wrong how the compat system works. Updated version attached.
Sorry, forgot to commit before sending.
Regards,
-- Nicolas George
Makefile | 2 threadmessage.c | 182 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ threadmessage.h | 89 +++++++++++++++++++++++++++ 3 files changed, 273 insertions(+) 2e0c5cf4cb82d683a14c433bd53cabdaae07c4d2 0001-lavu-add-thread-message-API.patch From 129ff96d38409e79ced27944bfd31613f917ded5 Mon Sep 17 00:00:00 2001 From: Nicolas George <george@nsup.org> Date: Thu, 20 Feb 2014 14:14:34 +0100 Subject: [PATCH 1/2] lavu: add thread message API.
TODO minor bump and APIchanges entry.
Signed-off-by: Nicolas George <george@nsup.org> --- libavutil/Makefile | 2 + libavutil/threadmessage.c | 182 +++++++++++++++++++++++++++++++++++++++++++++ libavutil/threadmessage.h | 89 ++++++++++++++++++++++ 3 files changed, 273 insertions(+) create mode 100644 libavutil/threadmessage.c create mode 100644 libavutil/threadmessage.h
diff --git a/libavutil/Makefile b/libavutil/Makefile index d2ebebc..5082e92 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -51,6 +51,7 @@ HEADERS = adler32.h \ sha.h \ sha512.h \ stereo3d.h \ + threadmessage.h \ time.h \ timecode.h \ timestamp.h \ @@ -117,6 +118,7 @@ OBJS = adler32.o \ sha.o \ sha512.o \ stereo3d.o \ + threadmessage.o \ time.o \ timecode.o \ tree.o \ diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c new file mode 100644 index 0000000..2c64440 --- /dev/null +++ b/libavutil/threadmessage.c @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2014 Nicolas George + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 License + * along with FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "fifo.h" +#include "threadmessage.h" +#if HAVE_THREADS +#if HAVE_PTHREADS +#include <pthread.h> +#elif HAVE_W32THREADS +#include "compat/w32pthreads.h" +#elif HAVE_OS2THREADS +#include "compat/os2threads.h" +#else +#error "Unknown threads implementation" +#endif +#endif + +struct AVThreadMessageQueue { +#if HAVE_THREADS + AVFifoBuffer *fifo; + pthread_mutex_t lock; + pthread_cond_t cond; + int err_send; + int err_recv; + unsigned elsize; +#endif +}; + +int av_thread_message_queue_alloc(AVThreadMessageQueue **mq, + unsigned elsize, + unsigned nelem) +{ +#if HAVE_THREADS + AVThreadMessageQueue *rmq; + int ret = 0; + + if (nelem > INT_MAX / elsize) + return AVERROR(EINVAL); + if (!(rmq = av_mallocz(sizeof(*rmq)))) + return AVERROR(ENOMEM); + if ((ret = pthread_mutex_init(&rmq->lock, NULL))) { + av_free(rmq); + return AVERROR(ret); + }
+ if ((ret = pthread_cond_init(&rmq->cond, NULL))) {
fails on mingw see: compat/w32pthreads.h:static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr) [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB There will always be a question for which you do not know the correct answer.
Le primidi 1er floréal, an CCXXII, Michael Niedermayer a écrit :
fails on mingw see: compat/w32pthreads.h:static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Indeed. Patch adding a proper return code to the w32pthreads version attached, completely untested but quite obvious. And I opened https://trac.ffmpeg.org/ticket/3579 for the corresponding problem at the call sites. Regards, -- Nicolas George
On Mon, Apr 21, 2014 at 12:13:07PM +0200, Nicolas George wrote:
Le primidi 1er floréal, an CCXXII, Michael Niedermayer a écrit :
fails on mingw see: compat/w32pthreads.h:static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Indeed. Patch adding a proper return code to the w32pthreads version attached, completely untested but quite obvious. And I opened https://trac.ffmpeg.org/ticket/3579 for the corresponding problem at the call sites.
Regards,
-- Nicolas George
w32pthreads.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) d0e420604231d7981855a595bccdb5dfdb1c96ed 0001-compat-w32pthreads-add-return-value-to-pthread_cond_.patch From 76209c85ce7e17a9ff7e5b37705afa6b62603e1d Mon Sep 17 00:00:00 2001 From: Nicolas George <george@nsup.org> Date: Mon, 21 Apr 2014 11:14:22 +0200 Subject: [PATCH] compat/w32pthreads: add return value to pthread_cond_init().
Signed-off-by: Nicolas George <george@nsup.org> --- compat/w32pthreads.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/compat/w32pthreads.h b/compat/w32pthreads.h index 7b51c25..916750f 100644 --- a/compat/w32pthreads.h +++ b/compat/w32pthreads.h @@ -134,28 +134,29 @@ typedef struct win32_cond_t { volatile int is_broadcast; } win32_cond_t;
-static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr) +static int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr) { win32_cond_t *win32_cond = NULL; if (cond_init) { cond_init(cond); - return; + return 0; }
/* non native condition variables */ win32_cond = av_mallocz(sizeof(win32_cond_t)); if (!win32_cond) - return; + return AVERROR(ENOMEM); cond->ptr = win32_cond; win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL); if (!win32_cond->semaphore) - return; + return AVERROR_EXTERNAL; win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL); if (!win32_cond->waiters_done) - return; + return AVERROR_EXTERNAL;
i dont think these error codes can be correct for a posix function
pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL); pthread_mutex_init(&win32_cond->mtx_broadcast, NULL); + return 0; }
static void pthread_cond_destroy(pthread_cond_t *cond) -- 1.7.10.4
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
-- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Those who are best at talking, realize last or never when they are wrong.
Le tridi 3 floréal, an CCXXII, Michael Niedermayer a écrit :
i dont think these error codes can be correct for a posix function
You are perfectly right, thanks for spotting it. New version attached, still untested. I had a glance at microsoft's documentation on the web, apparently GetLastError() would give the exact error code, but it would require mapping it to POSIX codes; with the use in FFmpeg, I believe only ENOMEM can happen, the other possible errors were about reusing handles. Regards, -- Nicolas George
On Tue, Apr 22, 2014 at 05:04:42PM +0200, Nicolas George wrote:
Le tridi 3 floréal, an CCXXII, Michael Niedermayer a écrit :
i dont think these error codes can be correct for a posix function
You are perfectly right, thanks for spotting it.
New version attached, still untested. I had a glance at microsoft's documentation on the web, apparently GetLastError() would give the exact error code, but it would require mapping it to POSIX codes; with the use in FFmpeg, I believe only ENOMEM can happen, the other possible errors were about reusing handles.
build & fate passes with mingw32+wine and this patch [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The real ebay dictionary, page 1 "Used only once" - "Some unspecified defect prevented a second use" "In good condition" - "Can be repaird by experienced expert" "As is" - "You wouldnt want it even if you were payed for it, if you knew ..."
+#ifndef AVUTIL_THREADMESSAGE_H +#define AVUTIL_THREADMESSAGE_H + +typedef struct AVThreadMessageQueue AVThreadMessageQueue; + +typedef enum AVThreadMessageFlags { + + /** + * Perform non-blocking operation. + * If this flag is set, send and recv operations are non-blocking and + * return AVERROR(EAGAIN) immediately if they can not proceed. + */ + AV_THREAD_MESSAGE_NONBLOCK = 1, + +} AVThreadMessageFlags; + +/** + * Allocate a new message queue. + * + * @param mq pointer to the message queue + * @param elsize size of each element in the queue + * @param nelem minimum number of elements in the queue
hmm, Isn't it a maximum number of elements? You can add that thread will be blocked or AVERROR(EAGIAN) will be returned depending on flag when queue is full. Here or at send doxy.
+ * @return >=0 for success; <0 for error, in particular AVERROR(ENOSYS) if + * lavu was built without thread support + */ +int av_thread_message_queue_alloc(AVThreadMessageQueue **mq, + unsigned elsize, + unsigned nelem); + +/** + * Free a message queue. + */ +void av_thread_message_queue_free(AVThreadMessageQueue **mq);
This function is not thread-safe. Second thread may be in the middle of sending/receiving a message. In such API it seems to be serious.
+/** + * Send a message on the queue. + */ +int av_thread_message_queue_send(AVThreadMessageQueue *mq, + void *msg, + unsigned flags); + +/** + * Receive a message from the queue. + */ +int av_thread_message_queue_recv(AVThreadMessageQueue *mq, + void *msg, + unsigned flags);
It would be nice to have also timeout, but I see w32threads are missing pthread_cond_timedwait() There is no way to get notified there is a message in the queue for receiving thread. It has to poll or get blocked. Maybe add a callback for it?
Le duodi 2 floréal, an CCXXII, Lukasz Marek a écrit :
hmm, Isn't it a maximum number of elements?
Fixed.
You can add that thread will be blocked or AVERROR(EAGIAN) will be returned depending on flag when queue is full. Here or at send doxy.
The doxy for AV_THREAD_MESSAGE_NONBLOCK already says it. Do you believe it needs to be repeated? It seems pretty obvious to me.
This function is not thread-safe. Second thread may be in the middle of sending/receiving a message. In such API it seems to be serious.
Added a caveat in the documentation. The program should free the message queue once it is fully no longer in use. Something like that: ret = av_thread_message_queue_recv(mq, msg, 0); if (ret == AVERROR_EOF) { av_thread_message_queue_free(&mq); return; } or: av_thread_message_queue_set_err_recv(mq, AVERROR_EOF); pthread_join(other_thread); av_thread_message_queue_free(&mq);
It would be nice to have also timeout, but I see w32threads are missing pthread_cond_timedwait()
I consider programs that need a timeout in this kind of place to be misdesigned. They usually mean that the program is waking up regularly to check for other things: useless wake ups (power consumption) when nothing happened and latency when something happens. I suppose there would be some legitimate cases for timeouts, but in the general case, I consider it best if all wake-ups causes are aggregated into the single message queue. In other words, if you need a timeout, arrange for another thread to send some kind of MESSAGE_TYPE_TIMEOUT at the appropriate time. In fact, I believe even the non-blocking variant is evil, but I can imagine immediate non-blocking to be necessary in some cases. (And also, the current ffmpeg.c code requires it.)
There is no way to get notified there is a message in the queue for receiving thread. It has to poll or get blocked. Maybe add a callback for it?
I am pretty sure there is no way of preempting another thread to make it execute a callback. (Well, there would be signals, but mixing signals with threads is a sure way of messing everything up.) Regards, -- Nicolas George
On Thu, Feb 20, 2014 at 04:22:13PM +0100, Nicolas George wrote:
TODO minor bump and APIchanges entry.
Signed-off-by: Nicolas George <george@nsup.org> --- libavutil/Makefile | 2 + libavutil/threadmessage.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++ libavutil/threadmessage.h | 92 +++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 libavutil/threadmessage.c create mode 100644 libavutil/threadmessage.h
ping? I would be curious to finally have helgrind instance giving useful results; it's probable some real issues are hidden in the current haystack. [...] -- Clément B.
participants (4)
-
Clément Bœsch -
Lukasz Marek -
Michael Niedermayer -
Nicolas George