# HG changeset patch # User Ryan C. Gordon # Date 1026363368 0 # Node ID 53ce18591f5d19812cda1a433a8f4ec6e801c9a1 # Parent b495d05a88b302e351427f1bcdb02ae2cd700d51 Mutex'd a potential race condition. diff -r b495d05a88b3 -r 53ce18591f5d decoders/modplug.c --- a/decoders/modplug.c Thu Jul 11 04:33:17 2002 +0000 +++ b/decoders/modplug.c Thu Jul 11 04:56:08 2002 +0000 @@ -111,9 +111,12 @@ static ModPlug_Settings settings; static Sound_AudioInfo current_audioinfo; static unsigned int total_mods_decoding = 0; +static SDL_mutex *modplug_mutex = NULL; static int MODPLUG_init(void) { + assert(modplug_mutex == NULL); + /* * The settings will require some experimenting. I've borrowed some * of them from the XMMS ModPlug plugin. @@ -145,6 +148,8 @@ current_audioinfo.format = AUDIO_S16SYS; total_mods_decoding = 0; + modplug_mutex = SDL_CreateMutex(); + ModPlug_SetSettings(&settings); return(1); /* success. */ } /* MODPLUG_init */ @@ -153,7 +158,12 @@ static void MODPLUG_quit(void) { assert(total_mods_decoding == 0); - /* it's a no-op. */ + + if (modplug_mutex != NULL) + { + SDL_DestroyMutex(modplug_mutex); + modplug_mutex = NULL; + } /* if */ } /* MODPLUG_quit */ @@ -215,7 +225,10 @@ * It's only safe to change these settings when there're * no other mods being decoded... */ - if (total_mods_decoding > 0) /* !!! FIXME: Should we mutex this? */ + if (modplug_mutex != NULL) + SDL_LockMutex(modplug_mutex); + + if (total_mods_decoding > 0) { /* other mods decoding: use the same settings they are. */ memcpy(&sample->actual, ¤t_audioinfo, sizeof (Sound_AudioInfo)); @@ -244,7 +257,18 @@ */ module = ModPlug_Load((void *) data, size); free(data); - BAIL_IF_MACRO(module == NULL, "MODPLUG: Not a module file.", 0); + if (module == NULL) + { + if (modplug_mutex != NULL) + SDL_UnlockMutex(modplug_mutex); + + BAIL_MACRO("MODPLUG: Not a module file.", 0); + } /* if */ + + total_mods_decoding++; + + if (modplug_mutex != NULL) + SDL_UnlockMutex(modplug_mutex); SNDDBG(("MODPLUG: [%d ms] %s\n", ModPlug_GetLength(module), ModPlug_GetName(module))); @@ -252,8 +276,6 @@ internal->decoder_private = (void *) module; sample->flags = SOUND_SAMPLEFLAG_CANSEEK; - total_mods_decoding++; /* !!! FIXME: Should we mutex this? */ - SNDDBG(("MODPLUG: Accepting data stream\n")); return(1); /* we'll handle this data. */ } /* MODPLUG_open */ @@ -263,8 +285,15 @@ { Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; ModPlugFile *module = (ModPlugFile *) internal->decoder_private; + + if (modplug_mutex != NULL) + SDL_LockMutex(modplug_mutex); + total_mods_decoding--; + if (modplug_mutex != NULL) + SDL_UnlockMutex(modplug_mutex); + ModPlug_Unload(module); } /* MODPLUG_close */