SDL_mixer: change master_volume into an SDL_atomic_t

From 844ac997e9bd53c8c500ad3c8c4462a32f503fdd Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Tue, 17 May 2022 23:55:10 +0300
Subject: [PATCH] change master_volume into an SDL_atomic_t

---
 src/mixer.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/mixer.c b/src/mixer.c
index 5e5710de..9508cf0f 100644
--- a/src/mixer.c
+++ b/src/mixer.c
@@ -110,7 +110,7 @@ static void *music_data = NULL;
 static const char **chunk_decoders = NULL;
 static int num_decoders = 0;
 
-static int master_volume = MIX_MAX_VOLUME;
+static SDL_atomic_t master_volume = { MIX_MAX_VOLUME };
 
 int Mix_GetNumChunkDecoders(void)
 {
@@ -278,19 +278,20 @@ static void SDLCALL
 mix_channels(void *udata, Uint8 *stream, int len)
 {
     Uint8 *mix_input;
-    int i, mixable, volume = Mix_MasterVolume(-1);
+    int i, mixable, volume, master_vol;
     Uint32 sdl_ticks;
 
     (void)udata;
 
-#if SDL_VERSION_ATLEAST(1, 3, 0)
     /* Need to initialize the stream in SDL 1.3+ */
     SDL_memset(stream, mixer.silence, (size_t)len);
-#endif
 
     /* Mix the music (must be done before the channels are added) */
     mix_music(music_data, stream, len);
 
+    volume = Mix_MasterVolume(-1);
+    master_vol = volume;
+
     /* Mix any playing channels... */
     sdl_ticks = SDL_GetTicks();
     for (i=0; i<num_channels; ++i) {
@@ -327,7 +328,7 @@ mix_channels(void *udata, Uint8 *stream, int len)
                 int remaining = len;
                 while (mix_channel[i].playing > 0 && index < len) {
                     remaining = len - index;
-                    volume = (master_volume * (mix_channel[i].volume * mix_channel[i].chunk->volume)) / (MIX_MAX_VOLUME * MIX_MAX_VOLUME);
+                    volume = (master_vol * (mix_channel[i].volume * mix_channel[i].chunk->volume)) / (MIX_MAX_VOLUME * MIX_MAX_VOLUME);
                     mixable = mix_channel[i].playing;
                     if (mixable > remaining) {
                         mixable = remaining;
@@ -1643,14 +1644,14 @@ void Mix_UnlockAudio(void)
 
 int Mix_MasterVolume(int volume)
 {
-    int prev_volume = master_volume;
+    int prev_volume = SDL_AtomicGet(&master_volume);
     if (volume < 0) {
         return prev_volume;
     }
     if (volume > SDL_MIX_MAXVOLUME) {
         volume = SDL_MIX_MAXVOLUME;
     }
-    master_volume = volume;
+    SDL_AtomicSet(&master_volume, volume);
     return(prev_volume);
 }