Commit fac4f9d4c38946415dc5be1d53c097d4cc888057

Authored by Charles Otto
1 parent 15f38d8f

Don't use the main thread's event loop to start processes

Due to Qt's limitations, it is necessary to have a consistent thread start
and stop a given process, but that thread does not have to be the main thread
rather, any thread in an event loop is fine.
Showing 1 changed file with 27 additions and 19 deletions
openbr/plugins/process.cpp
@@ -25,13 +25,11 @@ public: @@ -25,13 +25,11 @@ public:
25 CommunicationManager() 25 CommunicationManager()
26 { 26 {
27 basis = new QThread; 27 basis = new QThread;
28 - basis->moveToThread(QCoreApplication::instance()->thread());  
29 moveToThread(basis); 28 moveToThread(basis);
30 server.moveToThread(basis); 29 server.moveToThread(basis);
31 outbound.moveToThread(basis); 30 outbound.moveToThread(basis);
32 31
33 // signals for our sever 32 // signals for our sever
34 - connect(this, SIGNAL(pulseEndThread()), basis, SLOT(quit() ));  
35 connect(&server, SIGNAL(newConnection()), this, SLOT(receivedConnection() )); 33 connect(&server, SIGNAL(newConnection()), this, SLOT(receivedConnection() ));
36 connect(this, SIGNAL(pulseStartServer(QString)), this, SLOT(startServerInternal(QString)), Qt::BlockingQueuedConnection); 34 connect(this, SIGNAL(pulseStartServer(QString)), this, SLOT(startServerInternal(QString)), Qt::BlockingQueuedConnection);
37 connect(this, SIGNAL(pulseOutboundConnect(QString)), this, SLOT(startConnectInternal(QString) ), Qt::BlockingQueuedConnection); 35 connect(this, SIGNAL(pulseOutboundConnect(QString)), this, SLOT(startConnectInternal(QString) ), Qt::BlockingQueuedConnection);
@@ -62,7 +60,6 @@ public: @@ -62,7 +60,6 @@ public:
62 60
63 void shutDownThread() 61 void shutDownThread()
64 { 62 {
65 - emit pulseEndThread();  
66 basis->quit(); 63 basis->quit();
67 basis->wait(); 64 basis->wait();
68 delete basis; 65 delete basis;
@@ -276,8 +273,6 @@ signals: @@ -276,8 +273,6 @@ signals:
276 void pulseSendSerialized(); 273 void pulseSendSerialized();
277 void pulseShutdown(); 274 void pulseShutdown();
278 void pulseOutboundConnect(QString serverName); 275 void pulseOutboundConnect(QString serverName);
279 - void pulseEndThread();  
280 -  
281 276
282 public: 277 public:
283 QByteArray readArray; 278 QByteArray readArray;
@@ -448,28 +443,39 @@ class ProcessInterface : public QObject @@ -448,28 +443,39 @@ class ProcessInterface : public QObject
448 { 443 {
449 Q_OBJECT 444 Q_OBJECT
450 public: 445 public:
  446 + QThread *basis;
  447 +
  448 + ~ProcessInterface()
  449 + {
  450 + basis->quit();
  451 + basis->wait();
  452 + delete basis;
  453 + }
  454 +
451 ProcessInterface() 455 ProcessInterface()
452 { 456 {
453 - this->moveToThread(QCoreApplication::instance()->thread());  
454 - workerProcess.moveToThread(QCoreApplication::instance()->thread()); 457 + basis = new QThread();
  458 +
  459 + moveToThread(basis);
  460 + workerProcess.moveToThread(basis);
  461 +
455 connect(this, SIGNAL(pulseEnd()), this, SLOT(endProcessInternal()), Qt::BlockingQueuedConnection); 462 connect(this, SIGNAL(pulseEnd()), this, SLOT(endProcessInternal()), Qt::BlockingQueuedConnection);
456 connect(this, SIGNAL(pulseStart(QStringList)), this, SLOT(startProcessInternal(QStringList)), Qt::BlockingQueuedConnection); 463 connect(this, SIGNAL(pulseStart(QStringList)), this, SLOT(startProcessInternal(QStringList)), Qt::BlockingQueuedConnection);
  464 +
  465 + basis->start();
457 } 466 }
  467 +
458 QProcess workerProcess; 468 QProcess workerProcess;
459 void endProcess() 469 void endProcess()
460 { 470 {
461 - if (QThread::currentThread() != QCoreApplication::instance()->thread())  
462 - emit pulseEnd();  
463 - else  
464 - endProcessInternal(); 471 + emit pulseEnd();
465 } 472 }
  473 +
466 void startProcess(QStringList arguments) 474 void startProcess(QStringList arguments)
467 { 475 {
468 - if (QThread::currentThread() != QCoreApplication::instance()->thread() )  
469 - emit pulseStart(arguments);  
470 - else  
471 - startProcessInternal(arguments); 476 + emit pulseStart(arguments);
472 } 477 }
  478 +
473 signals: 479 signals:
474 void pulseEnd(); 480 void pulseEnd();
475 void pulseStart(QStringList); 481 void pulseStart(QStringList);
@@ -500,10 +506,12 @@ struct ProcessData @@ -500,10 +506,12 @@ struct ProcessData
500 506
501 ~ProcessData() 507 ~ProcessData()
502 { 508 {
503 - comm.sendSignal(CommunicationManager::SHOULD_END);  
504 - proc.endProcess();  
505 - comm.shutdown();  
506 - comm.shutDownThread(); 509 + if (initialized) {
  510 + comm.sendSignal(CommunicationManager::SHOULD_END);
  511 + proc.endProcess();
  512 + comm.shutdown();
  513 + comm.shutDownThread();
  514 + }
507 } 515 }
508 }; 516 };
509 517