comparison decoders/modplug.c @ 395:53ce18591f5d

Mutex'd a potential race condition.
author Ryan C. Gordon <icculus@icculus.org>
date Thu, 11 Jul 2002 04:56:08 +0000
parents fb519e6028e3
children c66080364dff d0d381a6f2bb
comparison
equal deleted inserted replaced
394:b495d05a88b3 395:53ce18591f5d
109 109
110 110
111 static ModPlug_Settings settings; 111 static ModPlug_Settings settings;
112 static Sound_AudioInfo current_audioinfo; 112 static Sound_AudioInfo current_audioinfo;
113 static unsigned int total_mods_decoding = 0; 113 static unsigned int total_mods_decoding = 0;
114 static SDL_mutex *modplug_mutex = NULL;
114 115
115 static int MODPLUG_init(void) 116 static int MODPLUG_init(void)
116 { 117 {
118 assert(modplug_mutex == NULL);
119
117 /* 120 /*
118 * The settings will require some experimenting. I've borrowed some 121 * The settings will require some experimenting. I've borrowed some
119 * of them from the XMMS ModPlug plugin. 122 * of them from the XMMS ModPlug plugin.
120 */ 123 */
121 settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING; 124 settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
143 current_audioinfo.channels = 2; 146 current_audioinfo.channels = 2;
144 current_audioinfo.rate = 44100; 147 current_audioinfo.rate = 44100;
145 current_audioinfo.format = AUDIO_S16SYS; 148 current_audioinfo.format = AUDIO_S16SYS;
146 total_mods_decoding = 0; 149 total_mods_decoding = 0;
147 150
151 modplug_mutex = SDL_CreateMutex();
152
148 ModPlug_SetSettings(&settings); 153 ModPlug_SetSettings(&settings);
149 return(1); /* success. */ 154 return(1); /* success. */
150 } /* MODPLUG_init */ 155 } /* MODPLUG_init */
151 156
152 157
153 static void MODPLUG_quit(void) 158 static void MODPLUG_quit(void)
154 { 159 {
155 assert(total_mods_decoding == 0); 160 assert(total_mods_decoding == 0);
156 /* it's a no-op. */ 161
162 if (modplug_mutex != NULL)
163 {
164 SDL_DestroyMutex(modplug_mutex);
165 modplug_mutex = NULL;
166 } /* if */
157 } /* MODPLUG_quit */ 167 } /* MODPLUG_quit */
158 168
159 169
160 /* 170 /*
161 * Most MOD files I've seen have tended to be a few hundred KB, even if some 171 * Most MOD files I've seen have tended to be a few hundred KB, even if some
213 223
214 /* 224 /*
215 * It's only safe to change these settings when there're 225 * It's only safe to change these settings when there're
216 * no other mods being decoded... 226 * no other mods being decoded...
217 */ 227 */
218 if (total_mods_decoding > 0) /* !!! FIXME: Should we mutex this? */ 228 if (modplug_mutex != NULL)
229 SDL_LockMutex(modplug_mutex);
230
231 if (total_mods_decoding > 0)
219 { 232 {
220 /* other mods decoding: use the same settings they are. */ 233 /* other mods decoding: use the same settings they are. */
221 memcpy(&sample->actual, &current_audioinfo, sizeof (Sound_AudioInfo)); 234 memcpy(&sample->actual, &current_audioinfo, sizeof (Sound_AudioInfo));
222 } /* if */ 235 } /* if */
223 else 236 else
242 * The buffer may be a bit too large, but that doesn't matter. I think 255 * The buffer may be a bit too large, but that doesn't matter. I think
243 * it's safe to free it as soon as ModPlug_Load() is finished anyway. 256 * it's safe to free it as soon as ModPlug_Load() is finished anyway.
244 */ 257 */
245 module = ModPlug_Load((void *) data, size); 258 module = ModPlug_Load((void *) data, size);
246 free(data); 259 free(data);
247 BAIL_IF_MACRO(module == NULL, "MODPLUG: Not a module file.", 0); 260 if (module == NULL)
261 {
262 if (modplug_mutex != NULL)
263 SDL_UnlockMutex(modplug_mutex);
264
265 BAIL_MACRO("MODPLUG: Not a module file.", 0);
266 } /* if */
267
268 total_mods_decoding++;
269
270 if (modplug_mutex != NULL)
271 SDL_UnlockMutex(modplug_mutex);
248 272
249 SNDDBG(("MODPLUG: [%d ms] %s\n", 273 SNDDBG(("MODPLUG: [%d ms] %s\n",
250 ModPlug_GetLength(module), ModPlug_GetName(module))); 274 ModPlug_GetLength(module), ModPlug_GetName(module)));
251 275
252 internal->decoder_private = (void *) module; 276 internal->decoder_private = (void *) module;
253 sample->flags = SOUND_SAMPLEFLAG_CANSEEK; 277 sample->flags = SOUND_SAMPLEFLAG_CANSEEK;
254
255 total_mods_decoding++; /* !!! FIXME: Should we mutex this? */
256 278
257 SNDDBG(("MODPLUG: Accepting data stream\n")); 279 SNDDBG(("MODPLUG: Accepting data stream\n"));
258 return(1); /* we'll handle this data. */ 280 return(1); /* we'll handle this data. */
259 } /* MODPLUG_open */ 281 } /* MODPLUG_open */
260 282
261 283
262 static void MODPLUG_close(Sound_Sample *sample) 284 static void MODPLUG_close(Sound_Sample *sample)
263 { 285 {
264 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; 286 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
265 ModPlugFile *module = (ModPlugFile *) internal->decoder_private; 287 ModPlugFile *module = (ModPlugFile *) internal->decoder_private;
288
289 if (modplug_mutex != NULL)
290 SDL_LockMutex(modplug_mutex);
291
266 total_mods_decoding--; 292 total_mods_decoding--;
293
294 if (modplug_mutex != NULL)
295 SDL_UnlockMutex(modplug_mutex);
267 296
268 ModPlug_Unload(module); 297 ModPlug_Unload(module);
269 } /* MODPLUG_close */ 298 } /* MODPLUG_close */
270 299
271 300