Mercurial > repos > blastem
comparison render_sdl.c @ 361:946ae3749260
Fix deadlock on quit
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 27 May 2013 20:56:02 -0700 |
parents | 79e4b466e7d0 |
children | 62177cc39049 |
comparison
equal
deleted
inserted
replaced
360:c42fae88d346 | 361:946ae3749260 |
---|---|
23 uint32_t missing_count; | 23 uint32_t missing_count; |
24 | 24 |
25 SDL_mutex * audio_mutex; | 25 SDL_mutex * audio_mutex; |
26 SDL_cond * audio_ready; | 26 SDL_cond * audio_ready; |
27 SDL_cond * audio_cond; | 27 SDL_cond * audio_cond; |
28 uint8_t quitting = 0; | |
28 | 29 |
29 void audio_callback(void * userdata, uint8_t *byte_stream, int len) | 30 void audio_callback(void * userdata, uint8_t *byte_stream, int len) |
30 { | 31 { |
31 //puts("audio_callback"); | 32 //puts("audio_callback"); |
32 int16_t * stream = (int16_t *)byte_stream; | 33 int16_t * stream = (int16_t *)byte_stream; |
33 int samples = len/(sizeof(int16_t)*2); | 34 int samples = len/(sizeof(int16_t)*2); |
34 int16_t * source_buf; | 35 int16_t * source_buf; |
35 | 36 uint8_t local_quit; |
36 SDL_LockMutex(audio_mutex); | 37 SDL_LockMutex(audio_mutex); |
37 while (!current_audio) { | 38 while (!current_audio && !quitting) { |
38 SDL_CondWait(audio_ready, audio_mutex); | 39 SDL_CondWait(audio_ready, audio_mutex); |
39 } | 40 } |
41 local_quit = quitting; | |
40 source_buf = current_audio; | 42 source_buf = current_audio; |
41 current_audio = NULL; | 43 current_audio = NULL; |
42 SDL_CondSignal(audio_cond); | 44 SDL_CondSignal(audio_cond); |
43 SDL_UnlockMutex(audio_mutex); | 45 SDL_UnlockMutex(audio_mutex); |
44 | 46 if (!local_quit) { |
45 for (int i = 0; i < samples; i++) { | 47 for (int i = 0; i < samples; i++) { |
46 *(stream++) = source_buf[i]; | 48 *(stream++) = source_buf[i]; |
47 *(stream++) = source_buf[i]; | 49 *(stream++) = source_buf[i]; |
48 } | 50 } |
51 } | |
52 } | |
53 | |
54 void render_close_audio() | |
55 { | |
56 SDL_LockMutex(audio_mutex); | |
57 quitting = 1; | |
58 SDL_CondSignal(audio_ready); | |
59 SDL_UnlockMutex(audio_mutex); | |
60 SDL_CloseAudio(); | |
49 } | 61 } |
50 | 62 |
51 void render_init(int width, int height, char * title, uint32_t fps) | 63 void render_init(int width, int height, char * title, uint32_t fps) |
52 { | 64 { |
53 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { | 65 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { |
54 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); | 66 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); |
55 exit(1); | 67 exit(1); |
56 } | 68 } |
57 atexit(SDL_Quit); | 69 atexit(SDL_Quit); |
70 atexit(render_close_audio); | |
58 printf("width: %d, height: %d\n", width, height); | 71 printf("width: %d, height: %d\n", width, height); |
59 screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_ANYFORMAT); | 72 screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_ANYFORMAT); |
60 if (!screen) { | 73 if (!screen) { |
61 fprintf(stderr, "Unable to get SDL surface: %s\n", SDL_GetError()); | 74 fprintf(stderr, "Unable to get SDL surface: %s\n", SDL_GetError()); |
62 exit(1); | 75 exit(1); |