1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-06 22:26:32 +02:00

Make thread safe (I think).

This commit is contained in:
Teo Mrnjavac
2013-02-04 17:48:34 +01:00
parent 4aa8436b76
commit 90774b48f4
2 changed files with 11 additions and 2 deletions

View File

@@ -19,6 +19,7 @@
#include "ScriptCommandQueue.h" #include "ScriptCommandQueue.h"
#include <QMetaType> #include <QMetaType>
#include <QMutex>
ScriptCommandQueue::ScriptCommandQueue( QObject* parent ) ScriptCommandQueue::ScriptCommandQueue( QObject* parent )
: QObject( parent ) : QObject( parent )
@@ -31,7 +32,10 @@ ScriptCommandQueue::ScriptCommandQueue( QObject* parent )
void void
ScriptCommandQueue::enqueue( const QSharedPointer< ScriptCommand >& req ) ScriptCommandQueue::enqueue( const QSharedPointer< ScriptCommand >& req )
{ {
QMutexLocker locker( &m_mutex );
m_queue.append( req ); m_queue.append( req );
locker.unlock();
if ( m_queue.count() == 1 ) if ( m_queue.count() == 1 )
nextCommand(); nextCommand();
} }
@@ -61,9 +65,10 @@ ScriptCommandQueue::onCommandDone()
{ {
m_timer->stop(); m_timer->stop();
QMutexLocker locker( &m_mutex );
const QSharedPointer< ScriptCommand > req = m_queue.first(); const QSharedPointer< ScriptCommand > req = m_queue.first();
m_queue.removeAll( req ); m_queue.removeAll( req );
locker.unlock();
disconnect( req.data(), SIGNAL( done() ), disconnect( req.data(), SIGNAL( done() ),
this, SLOT( onCommandDone() ) ); this, SLOT( onCommandDone() ) );
@@ -80,10 +85,12 @@ ScriptCommandQueue::onTimeout()
{ {
m_timer->stop(); m_timer->stop();
QMutexLocker locker( &m_mutex );
const QSharedPointer< ScriptCommand > req = m_queue.first(); const QSharedPointer< ScriptCommand > req = m_queue.first();
m_queue.removeAll( req );
locker.unlock();
req->reportFailure(); req->reportFailure();
m_queue.removeAll( req );
disconnect( req.data(), SIGNAL( done() ), disconnect( req.data(), SIGNAL( done() ),
this, SLOT( onCommandDone() ) ); this, SLOT( onCommandDone() ) );

View File

@@ -25,6 +25,7 @@
#include <QSharedPointer> #include <QSharedPointer>
#include <QTimer> #include <QTimer>
#include <QMetaType> #include <QMetaType>
#include <QMutex>
class ScriptCommandQueue : public QObject class ScriptCommandQueue : public QObject
{ {
@@ -43,6 +44,7 @@ private slots:
private: private:
QQueue< QSharedPointer< ScriptCommand > > m_queue; QQueue< QSharedPointer< ScriptCommand > > m_queue;
QTimer* m_timer; QTimer* m_timer;
QMutex m_mutex;
}; };
Q_DECLARE_METATYPE( QSharedPointer< ScriptCommand > ) Q_DECLARE_METATYPE( QSharedPointer< ScriptCommand > )