changeset 1797:5ff8f0d28188

Make sure there are no races between main thread and audio thread around mix_buf. Fix lack of proper termination in shader loading code
author Mike Pavone <pavone@retrodev.com>
date Sun, 24 Mar 2019 13:31:22 -0700
parents 51417bb557b6
children 5278b6e44fc1
files render_sdl.c
diffstat 1 files changed, 9 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/render_sdl.c	Sat Mar 23 17:18:10 2019 -0700
+++ b/render_sdl.c	Sun Mar 24 13:31:22 2019 -0700
@@ -165,10 +165,6 @@
 static void audio_callback(void * userdata, uint8_t *byte_stream, int len)
 {
 	uint8_t num_populated;
-	float *mix_dest = mix_buf ? mix_buf : (float *)byte_stream;
-	
-	int samples = len / sample_size;
-	memset(mix_dest, 0, samples * sizeof(float));
 	SDL_LockMutex(audio_mutex);
 		do {
 			num_populated = 0;
@@ -183,6 +179,9 @@
 				SDL_CondWait(audio_ready, audio_mutex);
 			}
 		} while(!quitting && num_populated < num_audio_sources);
+		int samples = len / sample_size;
+		float *mix_dest = mix_buf ? mix_buf : (float *)byte_stream;
+		memset(mix_dest, 0, samples * sizeof(float));
 		if (!quitting) {
 			for (uint8_t i = 0; i < num_audio_sources; i++)
 			{
@@ -191,8 +190,8 @@
 				SDL_CondSignal(audio_sources[i]->cond);
 			}
 		}
+		convert(mix_dest, byte_stream, samples);
 	SDL_UnlockMutex(audio_mutex);
-	convert(mix_dest, byte_stream, samples);
 }
 
 #define NO_LAST_BUFFERED -2000000000
@@ -248,12 +247,12 @@
 	SDL_LockMutex(audio_mutex);
 		quitting = 1;
 		SDL_CondSignal(audio_ready);
-		if (mix_buf) {
-			free(mix_buf);
-			mix_buf = NULL;
-		}
 	SDL_UnlockMutex(audio_mutex);
 	SDL_CloseAudio();
+	if (mix_buf) {
+		free(mix_buf);
+		mix_buf = NULL;
+	}
 }
 
 #define BUFFER_INC_RES 0x40000000UL
@@ -522,6 +521,7 @@
 		}
 		fsize = fsize32;
 	}
+	text[fsize] = 0;
 	
 	if (strncmp(text, "#version", strlen("#version"))) {
 		GLchar *tmp = text;