diff mbox series

[FFmpeg-devel,05/24] fftools/thread_queue: do not return elements for receive-finished streams

Message ID 20231104092125.10213-6-anton@khirnov.net
State Accepted
Commit 4f7b91a6980d4d593ce0bf5ae398996f878a18e2
Headers show
Series [FFmpeg-devel,01/24] lavf/mux: do not apply max_interleave_delta to subtitles | expand

Commit Message

Anton Khirnov Nov. 4, 2023, 7:56 a.m. UTC
It does not cause any issues in current callers, but still should not
happen.
---
 fftools/thread_queue.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/fftools/thread_queue.c b/fftools/thread_queue.c
index a1ab4ce92e..feac6a7748 100644
--- a/fftools/thread_queue.c
+++ b/fftools/thread_queue.c
@@ -164,7 +164,12 @@  static int receive_locked(ThreadQueue *tq, int *stream_idx,
     FifoElem elem;
     unsigned int nb_finished = 0;
 
-    if (av_fifo_read(tq->fifo, &elem, 1) >= 0) {
+    while (av_fifo_read(tq->fifo, &elem, 1) >= 0) {
+        if (tq->finished[elem.stream_idx] & FINISHED_RECV) {
+            objpool_release(tq->obj_pool, &elem.obj);
+            continue;
+        }
+
         tq->obj_move(data, elem.obj);
         objpool_release(tq->obj_pool, &elem.obj);
         *stream_idx = elem.stream_idx;
@@ -197,7 +202,14 @@  int tq_receive(ThreadQueue *tq, int *stream_idx, void *data)
     pthread_mutex_lock(&tq->lock);
 
     while (1) {
+        size_t can_read = av_fifo_can_read(tq->fifo);
+
         ret = receive_locked(tq, stream_idx, data);
+
+        // signal other threads if the fifo state changed
+        if (can_read != av_fifo_can_read(tq->fifo))
+            pthread_cond_broadcast(&tq->cond);
+
         if (ret == AVERROR(EAGAIN)) {
             pthread_cond_wait(&tq->cond, &tq->lock);
             continue;
@@ -206,9 +218,6 @@  int tq_receive(ThreadQueue *tq, int *stream_idx, void *data)
         break;
     }
 
-    if (ret == 0)
-        pthread_cond_broadcast(&tq->cond);
-
     pthread_mutex_unlock(&tq->lock);
 
     return ret;