diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2013-07-13 13:32:39 +0200 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2013-07-29 16:48:41 +0200 |
commit | 6af8326354ed6c1c68b53b3f2bba6697fb2d3bff (patch) | |
tree | d5f3a4e92e9c63dbf64d46514113d20e0ce5d090 | |
parent | a9903f7ec123582d3adc3939854ab98d12fb10df (diff) | |
download | ffmpeg-6af8326354ed6c1c68b53b3f2bba6697fb2d3bff.tar.gz |
avcodec/ff_init_vlc_sparse: use a spinlock for thread sync
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
-rw-r--r-- | libavcodec/bitstream.c | 28 | ||||
-rw-r--r-- | libavcodec/get_bits.h | 1 |
2 files changed, 20 insertions, 9 deletions
diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c index 3c4d176f30..aac32a5178 100644 --- a/libavcodec/bitstream.c +++ b/libavcodec/bitstream.c @@ -28,6 +28,7 @@ * bitstream api. */ +#include "libavutil/atomic.h" #include "libavutil/avassert.h" #include "avcodec.h" #include "mathops.h" @@ -269,14 +270,17 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, { VLCcode *buf; int i, j, ret; + void *state; vlc->bits = nb_bits; if (flags & INIT_VLC_USE_NEW_STATIC) { - if (vlc->table_size && vlc->table_size == vlc->table_allocated) { - return 0; - } else if(vlc->table_size) { - abort(); // fatal error, we are called on a partially initialized table + while (state = avpriv_atomic_ptr_cas(&vlc->init_state, NULL, vlc)) { + if (state == vlc + 1) { + av_assert0(vlc->table_size && vlc->table_size == vlc->table_allocated); + return 0; + } } + av_assert0(!vlc->table_size); } else { vlc->table = NULL; vlc->table_allocated = 0; @@ -326,12 +330,18 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, ret = build_table(vlc, nb_bits, nb_codes, buf, flags); av_free(buf); - if (ret < 0) { - av_freep(&vlc->table); - return ret; + if (flags & INIT_VLC_USE_NEW_STATIC) { + if(vlc->table_size != vlc->table_allocated) + av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated); + state = avpriv_atomic_ptr_cas(&vlc->init_state, vlc, vlc+1); + av_assert0(state == vlc); + av_assert0(ret >= 0); + } else { + if (ret < 0) { + av_freep(&vlc->table); + return ret; + } } - if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated) - av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated); return 0; } diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index b6cc75a474..3967b49b51 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -64,6 +64,7 @@ typedef struct VLC { int bits; VLC_TYPE (*table)[2]; ///< code, bits int table_size, table_allocated; + void *init_state; } VLC; typedef struct RL_VLC_ELEM { |