File indexing completed on 2024-12-08 03:36:34

0001 /*
0002     SPDX-FileCopyrightText: 2010 Nokia Corporation and /or its subsidiary(-ies) <qt-info@nokia.com>
0003     All rights reserved.
0004 
0005     This file is part of the examples of the Qt Toolkit.
0006 
0007     SPDX-License-Identifier: BSD-3-Clause
0008 */
0009 
0010 #include "Thread.h"
0011 
0012 #include <QSignalMapper>
0013 
0014 Thread::Thread( QObject * const parent )
0015     : QThread( parent )
0016 {
0017     // we need a class that receives signals from other threads and emits a signal in response
0018     m_shutDownHelper = new QSignalMapper;
0019     m_shutDownHelper->setMapping( this, 0 );
0020     connect( this, SIGNAL(started()), this, SLOT(setReadyStatus()), Qt::DirectConnection );
0021     connect( this, SIGNAL(aboutToStop()), m_shutDownHelper, SLOT(map()));
0022 }
0023 
0024 Thread::~Thread()
0025 {
0026     delete m_shutDownHelper;
0027 }
0028 
0029 // starts thread, moves worker to this thread and blocks
0030 void Thread::launchWorker( QObject * const worker )
0031 {
0032     m_worker = worker;
0033     start();
0034     m_worker->moveToThread( this );
0035     m_shutDownHelper->moveToThread( this );
0036     connect( m_shutDownHelper, SIGNAL(mapped(int)), this, SLOT(stopExecutor()), Qt::DirectConnection );
0037     m_mutex.lock();
0038     m_waitCondition.wait( &m_mutex );
0039 }
0040 
0041 // puts a command to stop processing in the event queue of worker thread
0042 void Thread::stop()
0043 {
0044     emit aboutToStop();
0045 }
0046 
0047 // methods above this line should be called in ui thread context
0048 // methods below this line are private and will be run in  secondary thread context
0049 
0050 void Thread::stopExecutor()  //secondary thread context
0051 {
0052     exit();
0053 }
0054 
0055 void Thread::setReadyStatus()
0056 {
0057     m_waitCondition.wakeAll();
0058 }
0059 
0060 #include "moc_Thread.cpp"