From 82b5a39ea448d3f68cd540734bcf1d8f532ac947 Mon Sep 17 00:00:00 2001 From: Richard Stanway Date: Sun, 16 Jan 2022 19:08:41 +0100 Subject: [PATCH] libobs: Mark raw_active and gpu_encoder_active as volatile These were operated on by atomic functions but were not marked as volatile or loaded with os_atomic_load_long, potentially introducing subtle race conditions. Detected by ThreadSanitizer. --- libobs/obs-internal.h | 4 ++-- libobs/obs-video.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libobs/obs-internal.h b/libobs/obs-internal.h index 2e8ef1048..83f832036 100644 --- a/libobs/obs-internal.h +++ b/libobs/obs-internal.h @@ -270,8 +270,8 @@ struct obs_core_video { gs_samplerstate_t *point_sampler; gs_stagesurf_t *mapped_surfaces[NUM_CHANNELS]; int cur_texture; - long raw_active; - long gpu_encoder_active; + volatile long raw_active; + volatile long gpu_encoder_active; pthread_mutex_t gpu_encoder_mutex; struct circlebuf gpu_encoder_queue; struct circlebuf gpu_encoder_avail_queue; diff --git a/libobs/obs-video.c b/libobs/obs-video.c index 7921e578c..1eceeac88 100644 --- a/libobs/obs-video.c +++ b/libobs/obs-video.c @@ -931,9 +931,10 @@ bool obs_graphics_thread_loop(struct obs_graphics_context *context) uint64_t frame_start = os_gettime_ns(); uint64_t frame_time_ns; - bool raw_active = obs->video.raw_active > 0; + bool raw_active = os_atomic_load_long(&obs->video.raw_active) > 0; #ifdef _WIN32 - const bool gpu_active = obs->video.gpu_encoder_active > 0; + const bool gpu_active = + os_atomic_load_long(&obs->video.gpu_encoder_active) > 0; const bool active = raw_active || gpu_active; #else const bool gpu_active = 0;