1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-01-24 13:56:33 +02:00

avcodec/ff_init_vlc_sparse: use a spinlock for thread sync

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
Michael Niedermayer 2013-07-13 13:32:39 +02:00
parent a9903f7ec1
commit 6af8326354
2 changed files with 20 additions and 9 deletions

View File

@ -28,6 +28,7 @@
* bitstream api. * bitstream api.
*/ */
#include "libavutil/atomic.h"
#include "libavutil/avassert.h" #include "libavutil/avassert.h"
#include "avcodec.h" #include "avcodec.h"
#include "mathops.h" #include "mathops.h"
@ -269,14 +270,17 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
{ {
VLCcode *buf; VLCcode *buf;
int i, j, ret; int i, j, ret;
void *state;
vlc->bits = nb_bits; vlc->bits = nb_bits;
if (flags & INIT_VLC_USE_NEW_STATIC) { if (flags & INIT_VLC_USE_NEW_STATIC) {
if (vlc->table_size && vlc->table_size == vlc->table_allocated) { while (state = avpriv_atomic_ptr_cas(&vlc->init_state, NULL, vlc)) {
return 0; if (state == vlc + 1) {
} else if(vlc->table_size) { av_assert0(vlc->table_size && vlc->table_size == vlc->table_allocated);
abort(); // fatal error, we are called on a partially initialized table return 0;
}
} }
av_assert0(!vlc->table_size);
} else { } else {
vlc->table = NULL; vlc->table = NULL;
vlc->table_allocated = 0; 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); ret = build_table(vlc, nb_bits, nb_codes, buf, flags);
av_free(buf); av_free(buf);
if (ret < 0) { if (flags & INIT_VLC_USE_NEW_STATIC) {
av_freep(&vlc->table); if(vlc->table_size != vlc->table_allocated)
return ret; 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; return 0;
} }

View File

@ -64,6 +64,7 @@ typedef struct VLC {
int bits; int bits;
VLC_TYPE (*table)[2]; ///< code, bits VLC_TYPE (*table)[2]; ///< code, bits
int table_size, table_allocated; int table_size, table_allocated;
void *init_state;
} VLC; } VLC;
typedef struct RL_VLC_ELEM { typedef struct RL_VLC_ELEM {