changeset 361:946ae3749260

Fix deadlock on quit
author Mike Pavone <pavone@retrodev.com>
date Mon, 27 May 2013 20:56:02 -0700
parents c42fae88d346
children b7c3facee762
files render_sdl.c
diffstat 1 files changed, 19 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/render_sdl.c	Mon May 27 20:37:55 2013 -0700
+++ b/render_sdl.c	Mon May 27 20:56:02 2013 -0700
@@ -25,6 +25,7 @@
 SDL_mutex * audio_mutex;
 SDL_cond * audio_ready;
 SDL_cond * audio_cond;
+uint8_t quitting = 0;
 
 void audio_callback(void * userdata, uint8_t *byte_stream, int len)
 {
@@ -32,22 +33,33 @@
 	int16_t * stream = (int16_t *)byte_stream;
 	int samples = len/(sizeof(int16_t)*2);
 	int16_t * source_buf;
-	
+	uint8_t local_quit;
 	SDL_LockMutex(audio_mutex);
-		while (!current_audio) {
+		while (!current_audio && !quitting) {
 			SDL_CondWait(audio_ready, audio_mutex);
 		}
+		local_quit = quitting;
 		source_buf = current_audio;
 		current_audio = NULL;
 		SDL_CondSignal(audio_cond);
 	SDL_UnlockMutex(audio_mutex);
-	
-	for (int i = 0; i < samples; i++) {
-		*(stream++) = source_buf[i];
-		*(stream++) = source_buf[i];
+	if (!local_quit) {
+		for (int i = 0; i < samples; i++) {
+			*(stream++) = source_buf[i];
+			*(stream++) = source_buf[i];
+		}
 	}
 }
 
+void render_close_audio()
+{
+	SDL_LockMutex(audio_mutex);
+		quitting = 1;
+		SDL_CondSignal(audio_ready);
+	SDL_UnlockMutex(audio_mutex);
+	SDL_CloseAudio();
+}
+
 void render_init(int width, int height, char * title, uint32_t fps)
 {
 	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
@@ -55,6 +67,7 @@
         exit(1);
     }
     atexit(SDL_Quit);
+    atexit(render_close_audio);
     printf("width: %d, height: %d\n", width, height);
     screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_ANYFORMAT);
     if (!screen) {