comparison render_sdl.c @ 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 ed6c38cd288c
comparison
equal deleted inserted replaced
1796:51417bb557b6 1797:5ff8f0d28188
163 static conv_func convert; 163 static conv_func convert;
164 164
165 static void audio_callback(void * userdata, uint8_t *byte_stream, int len) 165 static void audio_callback(void * userdata, uint8_t *byte_stream, int len)
166 { 166 {
167 uint8_t num_populated; 167 uint8_t num_populated;
168 float *mix_dest = mix_buf ? mix_buf : (float *)byte_stream;
169
170 int samples = len / sample_size;
171 memset(mix_dest, 0, samples * sizeof(float));
172 SDL_LockMutex(audio_mutex); 168 SDL_LockMutex(audio_mutex);
173 do { 169 do {
174 num_populated = 0; 170 num_populated = 0;
175 for (uint8_t i = 0; i < num_audio_sources; i++) 171 for (uint8_t i = 0; i < num_audio_sources; i++)
176 { 172 {
181 if (!quitting && num_populated < num_audio_sources) { 177 if (!quitting && num_populated < num_audio_sources) {
182 fflush(stdout); 178 fflush(stdout);
183 SDL_CondWait(audio_ready, audio_mutex); 179 SDL_CondWait(audio_ready, audio_mutex);
184 } 180 }
185 } while(!quitting && num_populated < num_audio_sources); 181 } while(!quitting && num_populated < num_audio_sources);
182 int samples = len / sample_size;
183 float *mix_dest = mix_buf ? mix_buf : (float *)byte_stream;
184 memset(mix_dest, 0, samples * sizeof(float));
186 if (!quitting) { 185 if (!quitting) {
187 for (uint8_t i = 0; i < num_audio_sources; i++) 186 for (uint8_t i = 0; i < num_audio_sources; i++)
188 { 187 {
189 mix_f32(audio_sources[i], mix_dest, samples); 188 mix_f32(audio_sources[i], mix_dest, samples);
190 audio_sources[i]->front_populated = 0; 189 audio_sources[i]->front_populated = 0;
191 SDL_CondSignal(audio_sources[i]->cond); 190 SDL_CondSignal(audio_sources[i]->cond);
192 } 191 }
193 } 192 }
193 convert(mix_dest, byte_stream, samples);
194 SDL_UnlockMutex(audio_mutex); 194 SDL_UnlockMutex(audio_mutex);
195 convert(mix_dest, byte_stream, samples);
196 } 195 }
197 196
198 #define NO_LAST_BUFFERED -2000000000 197 #define NO_LAST_BUFFERED -2000000000
199 static int32_t last_buffered = NO_LAST_BUFFERED; 198 static int32_t last_buffered = NO_LAST_BUFFERED;
200 static float average_change; 199 static float average_change;
246 static void render_close_audio() 245 static void render_close_audio()
247 { 246 {
248 SDL_LockMutex(audio_mutex); 247 SDL_LockMutex(audio_mutex);
249 quitting = 1; 248 quitting = 1;
250 SDL_CondSignal(audio_ready); 249 SDL_CondSignal(audio_ready);
251 if (mix_buf) {
252 free(mix_buf);
253 mix_buf = NULL;
254 }
255 SDL_UnlockMutex(audio_mutex); 250 SDL_UnlockMutex(audio_mutex);
256 SDL_CloseAudio(); 251 SDL_CloseAudio();
252 if (mix_buf) {
253 free(mix_buf);
254 mix_buf = NULL;
255 }
257 } 256 }
258 257
259 #define BUFFER_INC_RES 0x40000000UL 258 #define BUFFER_INC_RES 0x40000000UL
260 259
261 void render_audio_adjust_clock(audio_source *src, uint64_t master_clock, uint64_t sample_divider) 260 void render_audio_adjust_clock(audio_source *src, uint64_t master_clock, uint64_t sample_divider)
520 warning("Failed to open shader file %s for reading\n", fname); 519 warning("Failed to open shader file %s for reading\n", fname);
521 return 0; 520 return 0;
522 } 521 }
523 fsize = fsize32; 522 fsize = fsize32;
524 } 523 }
524 text[fsize] = 0;
525 525
526 if (strncmp(text, "#version", strlen("#version"))) { 526 if (strncmp(text, "#version", strlen("#version"))) {
527 GLchar *tmp = text; 527 GLchar *tmp = text;
528 text = alloc_concat(shader_prefix, tmp); 528 text = alloc_concat(shader_prefix, tmp);
529 free(tmp); 529 free(tmp);