From 252439c1af49807826ab58402f84f0cfbcbc9431 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 21 Sep 2016 01:59:43 +0300 Subject: [PATCH] Fixed a deadlocking race condition that might happen when reading APU memory in the hex viewer --- Core/apu.c | 5 ++++- Core/gb.h | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Core/apu.c b/Core/apu.c index 9bedc5e..4743fcb 100755 --- a/Core/apu.c +++ b/Core/apu.c @@ -63,8 +63,9 @@ static int16_t step_lfsr(uint16_t lfsr, bool uses_7_bit) static void GB_apu_run_internal(GB_gameboy_t *gb) { + while (!__sync_bool_compare_and_swap(&gb->apu_lock, false, true)); uint32_t steps = gb->apu.apu_cycles / (CPU_FREQUENCY/APU_FREQUENCY); - if (!steps) return; + if (!steps) goto exit; gb->apu.apu_cycles %= (CPU_FREQUENCY/APU_FREQUENCY); for (uint8_t i = 0; i < 4; i++) { @@ -123,6 +124,8 @@ static void GB_apu_run_internal(GB_gameboy_t *gb) gb->apu.wave_channels[0].cur_sweep_steps = gb->apu.wave_channels[0].sweep_steps; } } +exit: + gb->apu_lock = false; } void GB_apu_get_samples_and_update_pcm_regs(GB_gameboy_t *gb, GB_sample_t *samples) diff --git a/Core/gb.h b/Core/gb.h index 22ee9fd..9123dc3 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -363,6 +363,7 @@ typedef struct GB_gameboy_s { unsigned int audio_position; bool audio_stream_started; // detects first copy request to minimize lag volatile bool audio_copy_in_progress; + volatile bool apu_lock; /* Callbacks */ void *user_data;