diff options
author | Andreas Rheinhardt <andreas.rheinhardt@gmail.com> | 2020-12-03 01:57:16 +0300 |
---|---|---|
committer | Andreas Rheinhardt <andreas.rheinhardt@outlook.com> | 2021-04-02 20:12:43 +0300 |
commit | a2a38b160620d91bc3f895dadc4501c589998b9c (patch) | |
tree | 069b85d66253830a524b0ba61413c793a9662c60 /libavutil | |
parent | f38f791a23a8acade7ea6554c80455dce7543dbd (diff) |
avutil/cpu: Fix race condition in av_cpu_count()
av_cpu_count() intends to emit a debug message containing the number of
logical cores when called the first time. The check currently works with
a static volatile int; yet this does not help at all in case of
concurrent accesses by multiple threads. So replace this with an
atomic_int.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Diffstat (limited to 'libavutil')
-rw-r--r-- | libavutil/cpu.c | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/libavutil/cpu.c b/libavutil/cpu.c index 52f6b9a3bf..8e3576a1f3 100644 --- a/libavutil/cpu.c +++ b/libavutil/cpu.c @@ -274,7 +274,7 @@ int av_parse_cpu_caps(unsigned *flags, const char *s) int av_cpu_count(void) { - static volatile int printed; + static atomic_int printed = ATOMIC_VAR_INIT(0); int nb_cpus = 1; #if HAVE_WINRT @@ -306,10 +306,8 @@ int av_cpu_count(void) nb_cpus = sysinfo.dwNumberOfProcessors; #endif - if (!printed) { + if (!atomic_exchange_explicit(&printed, 1, memory_order_relaxed)) av_log(NULL, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); - printed = 1; - } return nb_cpus; } |