aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLionel Ulmer2002-06-03 21:20:11 +0000
committerLionel Ulmer2002-06-03 21:20:11 +0000
commita639c1be6f858a6a655aa2a1571b6f6ad843ca95 (patch)
tree084ffd3b5e24d0ad1ca99b5e72a165d70f89102b
parent27ea39a7be9f2756fd9ff3aebc6afb67a83117a2 (diff)
downloadscummvm-rg350-a639c1be6f858a6a655aa2a1571b6f6ad843ca95.tar.gz
scummvm-rg350-a639c1be6f858a6a655aa2a1571b6f6ad843ca95.tar.bz2
scummvm-rg350-a639c1be6f858a6a655aa2a1571b6f6ad843ca95.zip
Added the mutex support to ease the streaming (ie to prevent all race
conditions between an 'append' and a playing by the sound thread). Porters should add the relevant stuff to their OSystem interfaces. But finally, the bug reported by Valgrind was much more trivial than that and is also fixed in this commit :-) svn-id: r4398
-rw-r--r--sound/mixer.cpp17
-rw-r--r--sound/mixer.h6
2 files changed, 17 insertions, 6 deletions
diff --git a/sound/mixer.cpp b/sound/mixer.cpp
index 723b790e38..5f1d3fba11 100644
--- a/sound/mixer.cpp
+++ b/sound/mixer.cpp
@@ -39,14 +39,18 @@ void SoundMixer::uninsert(Channel *chan) {
}
int SoundMixer::append(int index, void *sound, uint32 size, uint rate, byte flags) {
+ _syst->lock_mutex(_mutex);
+
Channel *chan = _channels[index];
if (!chan) {
warning("Trying to stream to an unexistant streamer ");
play_stream(NULL, index, sound, size, rate, flags);
chan = _channels[index];
+ } else {
+ chan->append(sound, size);
}
- chan->append(sound, size);
+ _syst->unlock_mutex(_mutex);
return 1;
}
@@ -111,10 +115,12 @@ void SoundMixer::mix(int16 *buf, uint len) {
memset(buf, 0, 2 * len * sizeof(int16));
}
+ _syst->lock_mutex(_mutex);
/* now mix all channels */
for(int i=0; i!=NUM_CHANNELS; i++)
if (_channels[i])
_channels[i]->mix(buf, len);
+ _syst->unlock_mutex(_mutex);
}
void SoundMixer::on_generate_samples(void *s, byte *samples, int len) {
@@ -128,9 +134,12 @@ bool SoundMixer::bind_to_system(OSystem *syst) {
_output_rate = rate;
+ _syst = syst;
+ _mutex = _syst->create_mutex();
+
if (rate == 0)
error("OSystem returned invalid sample rate");
-
+
return syst->set_sound_proc(this, on_generate_samples, OSystem::SOUND_16BIT);
}
@@ -408,14 +417,14 @@ SoundMixer::Channel_STREAM::Channel_STREAM(SoundMixer *mixer, void *sound, uint3
void SoundMixer::Channel_STREAM::append(void *data, uint32 len) {
byte *new_end = _end_of_data + len;
byte *cur_pos = _pos; /* This is just to prevent the variable to move during the tests :-) */
-
if (new_end > (_ptr + _buffer_size)) {
/* Wrap-around case */
if ((_end_of_data < cur_pos) ||
(new_end >= cur_pos)) {
warning("Mixer full... Trying to not break too much ");
return;
- } memcpy(_end_of_data, data, (_ptr + _buffer_size) - _end_of_data);
+ }
+ memcpy(_end_of_data, data, (_ptr + _buffer_size) - _end_of_data);
memcpy(_ptr, (byte *) data + ((_ptr + _buffer_size) - _end_of_data), len - ((_ptr + _buffer_size) - _end_of_data));
} else {
if ((_end_of_data < cur_pos) &&
diff --git a/sound/mixer.h b/sound/mixer.h
index 99b36903bb..ac38312b19 100644
--- a/sound/mixer.h
+++ b/sound/mixer.h
@@ -69,7 +69,6 @@ private:
uint32 _buffer_size;
uint32 _rate;
byte _flags;
-
public:
void append(void *sound, uint32 size);
@@ -125,6 +124,9 @@ private:
public:
typedef void PremixProc(void *param, int16 *data, uint len);
+ OSystem *_syst;
+ void *_mutex;
+
uint _output_rate;
int16 *_volume_table;
@@ -140,7 +142,7 @@ public:
Channel *_channels[NUM_CHANNELS];
PlayingSoundHandle *_handles[NUM_CHANNELS];
-
+
int insert(PlayingSoundHandle *handle, Channel *chan);
int insert_at(PlayingSoundHandle *handle, int index, Channel *chan);
void append(void *data, uint32 len);