[FFmpeg-devel] [PATCH] avutil/threadmessage: fix condition broadcasting
Clément Bœsch
u at pkh.me
Tue Dec 1 16:24:16 CET 2015
From: Clément Bœsch <clement at stupeflix.com>
Fix a dead lock under certain conditions. Let's assume we have a queue of 1
message max, 2 senders, and 1 receiver.
Scenario (real record obtained with debug added):
[...]
SENDER #0: acquired lock
SENDER #0: queue is full, wait
SENDER #1: acquired lock
SENDER #1: queue is full, wait
RECEIVER: acquired lock
RECEIVER: reading a msg from the queue
RECEIVER: signal the cond
RECEIVER: acquired lock
RECEIVER: queue is empty, wait
SENDER #0: writing a msg the queue
SENDER #0: signal the cond
SENDER #0: acquired lock
SENDER #0: queue is full, wait
SENDER #1: queue is full, wait
Translated:
- initially the queue contains 1/1 message with 2 senders blocking on
it, waiting to push another message.
- Meanwhile the receiver is obtaining the lock, read the message,
signal & release the lock. For some reason it is able to acquire the
lock again before the signal wakes up one of the sender. Since it
just emptied the queue, the reader waits for the queue to fill up
again.
- The signal finally reaches one of the sender, which writes a message
and then signal the condition. Unfortunately, instead of waking up
the reader, it actually wakes up the other worker (signal = notify
the condition just for 1 waiter), who can't push another message in
the queue because it's full.
- Meanwhile, the receiver is still waiting. Deadlock.
This scenario can be triggered with for example:
tests/api/api-threadmessage-test 2 1 100 100 999
The fix is simply to make sure to notify everyone when work is done (be
it reading or writing). Another solution would be to use 2 distincts
conditions.
---
libavutil/threadmessage.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c
index b7fcbe2..2089c6d 100644
--- a/libavutil/threadmessage.c
+++ b/libavutil/threadmessage.c
@@ -107,7 +107,7 @@ static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
if (mq->err_send)
return mq->err_send;
av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL);
- pthread_cond_signal(&mq->cond);
+ pthread_cond_broadcast(&mq->cond);
return 0;
}
@@ -123,7 +123,7 @@ static int av_thread_message_queue_recv_locked(AVThreadMessageQueue *mq,
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);
+ pthread_cond_broadcast(&mq->cond);
return 0;
}
--
2.6.2
More information about the ffmpeg-devel
mailing list