[Scummvm-cvs-logs] CVS: scummvm/sound mixer.cpp,1.28,1.29 mixer.h,1.17,1.18

Lionel Ulmer bbrox at users.sourceforge.net
Mon Jun 3 14:21:02 CEST 2002


Update of /cvsroot/scummvm/scummvm/sound
In directory usw-pr-cvs1:/tmp/cvs-serv18284/sound

Modified Files:
	mixer.cpp mixer.h 
Log Message:
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 :-)



Index: mixer.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/mixer.cpp,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -d -r1.28 -r1.29
--- mixer.cpp	2 Jun 2002 20:30:21 -0000	1.28
+++ mixer.cpp	3 Jun 2002 21:20:11 -0000	1.29
@@ -39,14 +39,18 @@
 }
 
 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 @@
 		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 @@
 
 	_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 @@
 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) &&

Index: mixer.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/mixer.h,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- mixer.h	2 Jun 2002 20:30:21 -0000	1.17
+++ mixer.h	3 Jun 2002 21:20:11 -0000	1.18
@@ -69,7 +69,6 @@
 		uint32 _buffer_size;
 		uint32 _rate;
 		byte _flags;
-		
 
 	public:
 		void append(void *sound, uint32 size);		
@@ -125,6 +124,9 @@
 public:
 	typedef void PremixProc(void *param, int16 *data, uint len);
 
+	OSystem *_syst;
+	void *_mutex;
+	
 	uint _output_rate;
 
 	int16 *_volume_table;
@@ -140,7 +142,7 @@
 
 	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);





More information about the Scummvm-git-logs mailing list