• Clément Bœsch's avatar
    avutil/threadmessage: split the pthread condition in two · bd5c860f
    Clément Bœsch authored
    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 1 2 100 100 1 1000 1000
    
    One working solution is to make av_thread_message_queue_{send,recv}()
    call pthread_cond_broadcast() instead of pthread_cond_signal() so both
    senders and receivers are unlocked when work is done (be it reading or
    writing).
    
    This second solution replaces the condition with two: one to notify the
    senders, and one to notify the receivers. This prevents senders from
    notifying other senders instead of a reader, and the other way around.
    It also avoid broadcasting to everyone like the first solution, and is,
    as a result in theory more optimized.
    bd5c860f
Name
Last commit
Last update
compat Loading commit data...
doc Loading commit data...
libavcodec Loading commit data...
libavdevice Loading commit data...
libavfilter Loading commit data...
libavformat Loading commit data...
libavresample Loading commit data...
libavutil Loading commit data...
libpostproc Loading commit data...
libswresample Loading commit data...
libswscale Loading commit data...
presets Loading commit data...
tests Loading commit data...
tools Loading commit data...
.gitattributes Loading commit data...
.gitignore Loading commit data...
.travis.yml Loading commit data...
COPYING.GPLv2 Loading commit data...
COPYING.GPLv3 Loading commit data...
COPYING.LGPLv2.1 Loading commit data...
COPYING.LGPLv3 Loading commit data...
CREDITS Loading commit data...
Changelog Loading commit data...
INSTALL.md Loading commit data...
LICENSE.md Loading commit data...
MAINTAINERS Loading commit data...
Makefile Loading commit data...
README.md Loading commit data...
RELEASE Loading commit data...
arch.mak Loading commit data...
cmdutils.c Loading commit data...
cmdutils.h Loading commit data...
cmdutils_common_opts.h Loading commit data...
cmdutils_opencl.c Loading commit data...
common.mak Loading commit data...
configure Loading commit data...
ffmpeg.c Loading commit data...
ffmpeg.h Loading commit data...
ffmpeg_dxva2.c Loading commit data...
ffmpeg_filter.c Loading commit data...
ffmpeg_opt.c Loading commit data...
ffmpeg_qsv.c Loading commit data...
ffmpeg_vdpau.c Loading commit data...
ffmpeg_videotoolbox.c Loading commit data...
ffplay.c Loading commit data...
ffprobe.c Loading commit data...
ffserver.c Loading commit data...
ffserver_config.c Loading commit data...
ffserver_config.h Loading commit data...
library.mak Loading commit data...
version.sh Loading commit data...