From 643c1ea22266c911393a90e56b1a3e462cb6640d Mon Sep 17 00:00:00 2001 From: tracer-ics2023 Date: Sat, 21 Sep 2024 14:31:34 +0800 Subject: [PATCH] =?UTF-8?q?>=20=20compile=20NEMU=20221220000=20=E5=BC=A0?= =?UTF-8?q?=E4=B8=89=20Linux=20zzy=205.15.146.1-microsoft-standard-WSL2=20?= =?UTF-8?q?#1=20SMP=20Thu=20Jan=2011=2004:09:03=20UTC=202024=20x86=5F64=20?= =?UTF-8?q?x86=5F64=20x86=5F64=20GNU/Linux=20=2014:31:34=20up=204=20days,?= =?UTF-8?q?=20=209:37,=20=201=20user,=20=20load=20average:=200.37,=200.48,?= =?UTF-8?q?=200.62?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nemu/src/device/audio.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/nemu/src/device/audio.c b/nemu/src/device/audio.c index 16cf7bf..64f5977 100644 --- a/nemu/src/device/audio.c +++ b/nemu/src/device/audio.c @@ -17,6 +17,8 @@ #include #include +#include + enum { reg_freq, reg_channels, @@ -30,13 +32,12 @@ enum { static uint8_t *sbuf = NULL; static uint32_t *audio_base = NULL; static uint32_t pos_read = 0; -static uint32_t audio_count = 0; +static atomic_char32_t audio_count = 0; #define MIN(a, b) ((a) < (b) ? (a) : (b)) static void audio_callback(void *userdata, uint8_t *stream, int len) { - SDL_LockAudio(); - uint32_t count = audio_count; + uint32_t count = atomic_load(&audio_count); uint32_t read_cnt = MIN(count, len); uint32_t to_end_cnt = CONFIG_SB_SIZE - pos_read; int32_t out_bound_cnt = read_cnt - to_end_cnt; @@ -50,8 +51,8 @@ static void audio_callback(void *userdata, uint8_t *stream, int len) { pos_read += read_cnt; pos_read %= CONFIG_SB_SIZE; // if (len > read_cnt) memset(stream + read_cnt, 0, len - read_cnt); - audio_count -= read_cnt; - SDL_UnlockAudio(); + count -= read_cnt; + atomic_store(&audio_count, count); } static inline void init_audio_dev() { @@ -85,11 +86,9 @@ static void audio_io_handler(uint32_t offset, int len, bool is_write) { } break; case reg_count: - SDL_LockAudio(); // TODO: must be add a lock here - if (is_write) audio_count = audio_base[reg_count]; - else audio_base[reg_count] = audio_count; - SDL_UnlockAudio(); + if (is_write) atomic_store(&audio_count, audio_base[reg_count]); + else audio_base[reg_count] = atomic_load(&audio_count); break; default: assert(0);