Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ensure mutexes are always properly unlocked
Error exists with Qt4 and Qt5.  Qt5 generated helpful warnings that made
it easier to debug.
  • Loading branch information
maurerpe authored and wwmayer committed Feb 24, 2016
1 parent 8de9436 commit 31fd2d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
40 changes: 19 additions & 21 deletions src/Gui/CommandTest.cpp
Expand Up @@ -27,6 +27,7 @@
# include <QEventLoop>
# include <QFileDialog>
# include <QMutex>
# include <QMutexLocker>
# include <QThread>
# include <QTimer>
# include <QMdiArea>
Expand Down Expand Up @@ -294,10 +295,10 @@ CmdTestProgress1::CmdTestProgress1()

void CmdTestProgress1::activated(int iMsg)
{
QMutex mutex;
QMutexLocker ml(&mutex);
try
{
QMutex mutex;
mutex.lock();
unsigned long steps = 1000;
Base::SequencerLauncher seq("Starting progress bar", steps);

Expand All @@ -306,8 +307,6 @@ void CmdTestProgress1::activated(int iMsg)
seq.next(true);
QWaitCondition().wait(&mutex, 30);
}

mutex.unlock();
}
catch (...)
{
Expand Down Expand Up @@ -337,10 +336,11 @@ CmdTestProgress2::CmdTestProgress2()

void CmdTestProgress2::activated(int iMsg)
{
QMutex mutex;
QMutexLocker ml(&mutex);

try
{
QMutex mutex;
mutex.lock();
unsigned long steps = 1000;
Base::SequencerLauncher seq("Starting progress bar", steps);

Expand Down Expand Up @@ -378,11 +378,12 @@ CmdTestProgress3::CmdTestProgress3()

void CmdTestProgress3::activated(int iMsg)
{
QMutex mutex;
QMutexLocker ml(&mutex);

try
{
// level 1
QMutex mutex;
mutex.lock();
unsigned long steps = 5;
Base::SequencerLauncher seq1("Starting progress bar", steps);
for (unsigned long i=0; i<steps;i++)
Expand Down Expand Up @@ -446,10 +447,11 @@ CmdTestProgress4::CmdTestProgress4()

void CmdTestProgress4::activated(int iMsg)
{
QMutex mutex;
QMutexLocker ml(&mutex);

try
{
QMutex mutex;
mutex.lock();
unsigned long steps = 50;
Base::SequencerLauncher* seq = new Base::SequencerLauncher("Starting progress bar", steps);

Expand Down Expand Up @@ -508,18 +510,18 @@ class BarThread : public QThread
}
void run()
{
QMutex mutex;
QMutexLocker ml(&mutex);

try
{
QMutex mutex;
mutex.lock();
Base::SequencerLauncher seq("Starting progress bar in thread", steps);

for (unsigned long i=0; i<this->steps;i++)
{
seq.next(true);
QWaitCondition().wait(&mutex, 5);
}
mutex.unlock();
}
catch (...)
{
Expand Down Expand Up @@ -662,27 +664,23 @@ class TestConsoleObserver : public Base::ConsoleObserver
}
virtual void Warning(const char * msg)
{
mutex.lock();
QMutexLocker ml(&mutex);
matchWrn += strcmp(msg, "Write a warning to the console output.\n");
mutex.unlock();
}
virtual void Message(const char * msg)
{
mutex.lock();
QMutexLocker ml(&mutex);
matchMsg += strcmp(msg, "Write a message to the console output.\n");
mutex.unlock();
}
virtual void Error(const char * msg)
{
mutex.lock();
QMutexLocker ml(&mutex);
matchErr += strcmp(msg, "Write an error to the console output.\n");
mutex.unlock();
}
virtual void Log(const char * msg)
{
mutex.lock();
QMutexLocker ml(&mutex);
matchLog += strcmp(msg, "Write a log to the console output.\n");
mutex.unlock();
}
};

Expand Down
11 changes: 5 additions & 6 deletions src/Gui/Quarter/SignalThread.cpp
Expand Up @@ -30,6 +30,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

#include <QMutexLocker>

#include "SignalThread.h"

using namespace SIM::Coin3D::Quarter;
Expand All @@ -47,31 +49,28 @@ void
SignalThread::trigger(void)
{
// lock first to make sure the QThread is actually waiting for a signal
this->mutex.lock();
QMutexLocker ml(&this->mutex);
this->waitcond.wakeOne();
this->mutex.unlock();
}

void
SignalThread::stopThread(void)
{
this->mutex.lock();
QMutexLocker ml(&this->mutex);
this->isstopped = true;
this->waitcond.wakeOne();
this->mutex.unlock();
}


void
SignalThread::run(void)
{
this->mutex.lock();
QMutexLocker ml(&this->mutex);
while (!this->isstopped) {
// just wait, and trigger every time we receive a signal
this->waitcond.wait(&this->mutex);
if (!this->isstopped) {
emit triggerSignal();
}
}
this->mutex.unlock();
}
2 changes: 1 addition & 1 deletion src/Gui/Splashscreen.cpp
Expand Up @@ -134,7 +134,7 @@ class SplashObserver : public Base::ConsoleObserver

splash->showMessage(msg.replace(QLatin1String("\n"), QString()), alignment, textColor);
QMutex mutex;
mutex.lock();
QMutexLocker ml(&mutex);
QWaitCondition().wait(&mutex, 50);
}

Expand Down

0 comments on commit 31fd2d1

Please sign in to comment.