avutil: fix data race in av_get_cpu_flags()
authorWan-Teh Chang <wtc@google.com>
Thu, 8 Dec 2016 00:16:02 +0000 (16:16 -0800)
committerVittorio Giovara <vittorio.giovara@gmail.com>
Thu, 8 Dec 2016 20:53:58 +0000 (15:53 -0500)
commit2170017a1cd033b6f28e16476921022712a522d8
treecfcbe102ef4c0fcddd52e2b51b9fca5a850db146
parentd5759701a82926059ae3e2530805e900041a5419
avutil: fix data race in av_get_cpu_flags()

Make the one-time initialization in av_get_cpu_flags() thread-safe. The
static variables |flags|, |cpuflags_mask|, and |checked| in
libavutil/cpu.c are read and written using normal load and store
operations. These are considered as data races. The fix is to use atomic
load and store operations.

Remove the |checked| variable because the invalid value of -1 for
|flags| can be used to indicate the same condition. Rename |flags| to
|cpu_flags| and move it to file scope.

The fix can be verified by running the libavutil/tests/cpu_init.c test
program under ThreadSanitizer:
    ./configure --toolchain=clang-tsan
    make libavutil/tests/cpu_init
    libavutil/tests/cpu_init

There should be no warnings from ThreadSanitizer.

Co-author: Dmitry Vyukov of Google, who suggested the data race fix.

Signed-off-by: Wan-Teh Chang <wtc@google.com>
libavutil/Makefile
libavutil/cpu.c
libavutil/cpu.h
libavutil/tests/.gitignore
libavutil/tests/cpu_init.c [new file with mode: 0644]
tests/fate/libavutil.mak