mirror of https://github.com/FFmpeg/FFmpeg.git
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>pull/254/merge
parent
d5759701a8
commit
2170017a1c
6 changed files with 92 additions and 18 deletions
@ -0,0 +1,65 @@ |
|||||||
|
/*
|
||||||
|
* This file is part of Libav. |
||||||
|
* |
||||||
|
* Libav is free software; you can redistribute it and/or |
||||||
|
* modify it under the terms of the GNU Lesser General Public |
||||||
|
* License as published by the Free Software Foundation; either |
||||||
|
* version 2.1 of the License, or (at your option) any later version. |
||||||
|
* |
||||||
|
* Libav is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||||
|
* Lesser General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Lesser General Public |
||||||
|
* License along with Libav; if not, write to the Free Software |
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||||
|
*/ |
||||||
|
|
||||||
|
/*
|
||||||
|
* This test program tests whether the one-time initialization in |
||||||
|
* av_get_cpu_flags() has data races. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <stdio.h> |
||||||
|
#include <string.h> |
||||||
|
|
||||||
|
#include "libavutil/cpu.h" |
||||||
|
#include "libavutil/thread.h" |
||||||
|
|
||||||
|
static void *thread_main(void *arg) |
||||||
|
{ |
||||||
|
int *flags = arg; |
||||||
|
|
||||||
|
*flags = av_get_cpu_flags(); |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
int main(void) |
||||||
|
{ |
||||||
|
int cpu_flags1; |
||||||
|
int cpu_flags2; |
||||||
|
int ret; |
||||||
|
pthread_t thread1; |
||||||
|
pthread_t thread2; |
||||||
|
|
||||||
|
if ((ret = pthread_create(&thread1, NULL, thread_main, &cpu_flags1))) { |
||||||
|
fprintf(stderr, "pthread_create failed: %s.\n", strerror(ret)); |
||||||
|
return 1; |
||||||
|
} |
||||||
|
if ((ret = pthread_create(&thread2, NULL, thread_main, &cpu_flags2))) { |
||||||
|
fprintf(stderr, "pthread_create failed: %s.\n", strerror(ret)); |
||||||
|
return 1; |
||||||
|
} |
||||||
|
pthread_join(thread1, NULL); |
||||||
|
pthread_join(thread2, NULL); |
||||||
|
|
||||||
|
if (cpu_flags1 < 0) |
||||||
|
return 2; |
||||||
|
if (cpu_flags2 < 0) |
||||||
|
return 2; |
||||||
|
if (cpu_flags1 != cpu_flags2) |
||||||
|
return 3; |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
Loading…
Reference in new issue