aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-07-17 23:45:36 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-07-17 23:45:36 +0000
commit9e92105657f48973da9522feccc665bbd3cb5b86 (patch)
tree1b29127084ec055d488b134a38cde5e77d677f0c /src
parent55b4d0487c9222dd9bdc76dda0fd990d188d7e32 (diff)
Fix threading resource use bugs
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@139 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src')
-rw-r--r--src/audio.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/audio.c b/src/audio.c
index 1303792..f0c0e4f 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -114,7 +114,8 @@ static void *audio_play_wav(void *add_void) {
}
if ( idx == AUDIO_MAX_SOUNDS ) {
fprintf(stderr, "Not enough audio channels to play '%s'\n", filename);
- return NULL;
+ pthread_mutex_unlock(&a->sounds_mutex);
+ goto out;
}
a->sounds[idx].inuse = 1;
pthread_mutex_unlock(&a->sounds_mutex);
@@ -123,12 +124,17 @@ static void *audio_play_wav(void *add_void) {
/* Load the sound file and convert it to 16-bit stereo at 44.1 kHz */
if ( SDL_LoadWAV(filename, &wave, &data, &dlen) == NULL ) {
fprintf(stderr, "Couldn't load %s: (channel %i) %s\n", filename, idx, SDL_GetError());
- return NULL;
+ a->sounds[idx].inuse = 0;
+ goto out;
}
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 44100);
cvt.buf = malloc(dlen*cvt.len_mult);
if ( cvt.buf == NULL ) {
fprintf(stderr, "Not enough memory to convert audio (channel %i)\n", idx);
+ a->sounds[idx].inuse = 0;
+ SDL_FreeWAV(data);
+ free(cvt.buf);
+ goto out;
}
memcpy(cvt.buf, data, dlen);
cvt.len = dlen;
@@ -144,6 +150,7 @@ static void *audio_play_wav(void *add_void) {
a->sounds[idx].volume = add->volume;
a->sounds[idx].playing = 1; /* Must be done last - tell the mixer thread it can use this */
+out:
free(add->filename);
if ( a->debug ) printf("AU: Channel %i: WAV dispatch thread completed.\n", idx);
a->dispatch_threads_inuse[add->idx] = 0;
@@ -175,7 +182,8 @@ static void *audio_play_vorbis(void *add_void) {
}
if ( idx == AUDIO_MAX_SOUNDS ) {
fprintf(stderr, "Not enough audio channels to play '%s'\n", filename);
- return NULL;
+ pthread_mutex_unlock(&a->sounds_mutex);
+ goto out;
}
a->sounds[idx].inuse = 1;
pthread_mutex_unlock(&a->sounds_mutex);
@@ -184,7 +192,8 @@ static void *audio_play_vorbis(void *add_void) {
err = ov_fopen(filename, &vf);
if ( err != 0 ) {
fprintf(stderr, "Couldn't open Vorbis file '%s' (channel %i, code %i,%i)\n", filename, idx, err, errno);
- return NULL;
+ a->sounds[idx].inuse = 0;
+ goto out;
}
len = ov_pcm_total(&vf, -1); /* Length in samples 'per channel' */
@@ -194,6 +203,9 @@ static void *audio_play_vorbis(void *add_void) {
data = malloc(vi->channels*2*len); /* Two bytes per sample per channel */
if ( data == NULL ) {
fprintf(stderr, "Not enough memory to decode Vorbis stream (channel %i)\n", idx);
+ a->sounds[idx].inuse = 0;
+ ov_clear(&vf);
+ goto out;
}
offs = 0;
@@ -216,12 +228,16 @@ static void *audio_play_vorbis(void *add_void) {
cvt.buf = malloc(vi->channels*2*len*cvt.len_mult);
if ( cvt.buf == NULL ) {
fprintf(stderr, "Not enough memory to convert audio (channel %i)\n", idx);
+ free(cvt.buf);
+ a->sounds[idx].inuse = 0;
+ goto out;
}
memcpy(cvt.buf, data, vi->channels*2*len);
cvt.len = vi->channels*2*len;
SDL_ConvertAudio(&cvt);
free(data);
+ /* Needed until now */
ov_clear(&vf);
/* Put the sound data in the slot */
@@ -233,6 +249,7 @@ static void *audio_play_vorbis(void *add_void) {
a->sounds[idx].volume = add->volume;
a->sounds[idx].playing = 1; /* Must be done last - tell the mixer thread it can use this */
+out:
free(add->filename);
if ( a->debug ) printf("AU: Channel %i: Vorbis dispatch thread completed.\n", idx);
a->dispatch_threads_inuse[add->idx] = 0;