From fe7d269c803be014fd31fed3ec63e69e8aa78ec4 Mon Sep 17 00:00:00 2001 From: Paul Giblock Date: Tue, 2 Sep 2008 03:09:12 +0000 Subject: [PATCH] Add locking to trackContainer::m_tracks git-svn-id: https://lmms.svn.sf.net/svnroot/lmms/trunk/lmms@1527 0778d3d1-df1d-0410-868b-ea421aaaa00d --- ChangeLog | 10 ++++++++++ include/track_container.h | 6 ++++++ src/core/song.cpp | 11 ++++++++--- src/core/track.cpp | 6 +++--- src/core/track_container.cpp | 13 ++++++++++++- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 98cec2ffc..948d11bfb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2008-09-01 Paul Giblock + + * include/track_container.h: + * src/core/song.cpp: + * src/core/track_container.cpp: + * src/core/track.cpp: + Add locking to trackContainer::m_tracks. This should fix most crashes + causes by moving/adding TCOs. This also seems to fix the no-sound bug + that I hate so much. + 2008-09-02 Csaba Hruska * include/song.h: diff --git a/include/track_container.h b/include/track_container.h index 42a9dc766..07e2d714c 100644 --- a/include/track_container.h +++ b/include/track_container.h @@ -27,6 +27,8 @@ #ifndef _TRACK_CONTAINER_H #define _TRACK_CONTAINER_H +#include + #include "track.h" #include "journalling_object.h" @@ -64,10 +66,12 @@ public: void clearAllTracks( void ); + /* trackList & tracks( void ) { return( m_tracks ); } + */ const trackList & tracks( void ) const { @@ -83,6 +87,8 @@ public: signals: void trackAdded( track * _track ); +protected: + mutable QReadWriteLock m_tracksMutex; private: trackList m_tracks; diff --git a/src/core/song.cpp b/src/core/song.cpp index 222ef6d28..1aa03a113 100644 --- a/src/core/song.cpp +++ b/src/core/song.cpp @@ -564,6 +564,7 @@ void song::playPattern( pattern * _patternToPlay, bool _loop ) void song::updateLength( void ) { m_length = 0; + m_tracksMutex.lockForRead(); for( trackList::const_iterator it = tracks().begin(); it != tracks().end(); ++it ) { @@ -573,6 +574,7 @@ void song::updateLength( void ) m_length = cur; } } + m_tracksMutex.unlock(); } @@ -638,11 +640,13 @@ void song::stopExport( void ) void song::insertBar( void ) { - for( trackList::iterator it = tracks().begin(); + m_tracksMutex.lockForRead(); + for( trackList::const_iterator it = tracks().begin(); it != tracks().end(); ++it ) { ( *it )->insertTact( m_playPos[Mode_PlaySong] ); } + m_tracksMutex.unlock(); } @@ -650,11 +654,13 @@ void song::insertBar( void ) void song::removeBar( void ) { - for( trackList::iterator it = tracks().begin(); + m_tracksMutex.lockForRead(); + for( trackList::const_iterator it = tracks().begin(); it != tracks().end(); ++it ) { ( *it )->removeTact( m_playPos[Mode_PlaySong] ); } + m_tracksMutex.unlock(); } @@ -666,7 +672,6 @@ void song::addBBTrack( void ) track * t = track::create( track::BBTrack, this ); engine::getBBTrackContainer()->setCurrentBB( bbTrack::numOfBBTrack( t ) ); - engine::getMixer()->unlock(); } diff --git a/src/core/track.cpp b/src/core/track.cpp index 49c9cad53..4dc284035 100644 --- a/src/core/track.cpp +++ b/src/core/track.cpp @@ -2029,10 +2029,10 @@ tact track::length( void ) const */ void track::toggleSolo( void ) { - trackContainer::trackList & tl = m_trackContainer->tracks(); + const trackContainer::trackList & tl = m_trackContainer->tracks(); bool solo_before = FALSE; - for( trackContainer::trackList::iterator it = tl.begin(); + for( trackContainer::trackList::const_iterator it = tl.begin(); it != tl.end(); ++it ) { if( *it != this ) @@ -2046,7 +2046,7 @@ void track::toggleSolo( void ) } const bool solo = m_soloModel.value(); - for( trackContainer::trackList::iterator it = tl.begin(); + for( trackContainer::trackList::const_iterator it = tl.begin(); it != tl.end(); ++it ) { if( solo ) diff --git a/src/core/track_container.cpp b/src/core/track_container.cpp index 412bcb1e9..15ffa89c4 100644 --- a/src/core/track_container.cpp +++ b/src/core/track_container.cpp @@ -37,7 +37,8 @@ trackContainer::trackContainer( void ) : model( NULL ), journallingObject(), - m_tracks() + m_tracks(), + m_tracksMutex() { } @@ -60,10 +61,12 @@ void trackContainer::saveSettings( QDomDocument & _doc, QDomElement & _this ) //mainWindow::saveWidgetState( this, _this ); // save settings of each track + m_tracksMutex.lockForRead(); for( int i = 0; i < m_tracks.size(); ++i ) { m_tracks[i]->saveState( _doc, _this ); } + m_tracksMutex.unlock(); } @@ -137,7 +140,9 @@ void trackContainer::addTrack( track * _track ) { if( _track->type() != track::HiddenAutomationTrack ) { + m_tracksMutex.lockForWrite(); m_tracks.push_back( _track ); + m_tracksMutex.unlock(); emit trackAdded( _track ); } } @@ -150,7 +155,9 @@ void trackContainer::removeTrack( track * _track ) int index = m_tracks.indexOf( _track ); if( index != -1 ) { + m_tracksMutex.lockForWrite(); m_tracks.remove( index ); + m_tracksMutex.unlock(); if( engine::getSong() ) { @@ -173,10 +180,12 @@ void trackContainer::updateAfterTrackAdd( void ) void trackContainer::clearAllTracks( void ) { + //m_tracksMutex.lockForWrite(); while( !m_tracks.isEmpty() ) { delete m_tracks.first(); } + //m_tracksMutex.unlock(); } @@ -185,6 +194,7 @@ void trackContainer::clearAllTracks( void ) int trackContainer::countTracks( track::TrackTypes _tt ) const { int cnt = 0; + m_tracksMutex.lockForRead(); for( int i = 0; i < m_tracks.size(); ++i ) { if( m_tracks[i]->type() == _tt || _tt == track::NumTrackTypes ) @@ -192,6 +202,7 @@ int trackContainer::countTracks( track::TrackTypes _tt ) const ++cnt; } } + m_tracksMutex.unlock(); return( cnt ); }