From b6b2b7f58b02d8b9c6f5da4fd824e88262460ff6 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Fri, 10 Jan 2014 21:16:00 -0800 Subject: [PATCH] first cut at making GenericThread use QThread instead of pthread --- libraries/shared/src/GenericThread.cpp | 27 +++++++++++++++----------- libraries/shared/src/GenericThread.h | 17 ++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/libraries/shared/src/GenericThread.cpp b/libraries/shared/src/GenericThread.cpp index 9b8b311ce4..55fe2b0f38 100644 --- a/libraries/shared/src/GenericThread.cpp +++ b/libraries/shared/src/GenericThread.cpp @@ -23,19 +23,30 @@ GenericThread::~GenericThread() { void GenericThread::initialize(bool isThreaded) { _isThreaded = isThreaded; if (_isThreaded) { - pthread_create(&_thread, NULL, GenericThreadEntry, this); + QThread* _thread = new QThread(this); + + // when the worker thread is started, call our engine's run.. + connect(_thread, SIGNAL(started()), this, SLOT(threadRoutine())); + + // when the thread is terminated, add both scriptEngine and thread to the deleteLater queue + //connect(this, SIGNAL(finished()), this, SLOT(deleteLater())); + //connect(_thread, SIGNAL(finished()), _thread, SLOT(deleteLater())); + + this->moveToThread(_thread); + + // Starts an event loop, and emits _thread->started() + _thread->start(); } } void GenericThread::terminate() { if (_isThreaded) { _stopThread = true; - pthread_join(_thread, NULL); - _isThreaded = false; + //_isThreaded = false; } } -void* GenericThread::threadRoutine() { +void GenericThread::threadRoutine() { while (!_stopThread) { // override this function to do whatever your class actually does, return false to exit thread early @@ -51,12 +62,6 @@ void* GenericThread::threadRoutine() { } if (_isThreaded) { - pthread_exit(0); + emit finished(); } - return NULL; } - -extern "C" void* GenericThreadEntry(void* arg) { - GenericThread* genericThread = (GenericThread*)arg; - return genericThread->threadRoutine(); -} \ No newline at end of file diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h index d083a46dce..76ceed83cb 100644 --- a/libraries/shared/src/GenericThread.h +++ b/libraries/shared/src/GenericThread.h @@ -13,8 +13,7 @@ #include #include - -#include +#include /// A basic generic "thread" class. Handles a single thread of control within the application. Can operate in non-threaded /// mode but caller must regularly call threadRoutine() method. @@ -31,14 +30,18 @@ public: /// Call to stop the thread void terminate(); - /// If you're running in non-threaded mode, you must call this regularly - void* threadRoutine(); - /// Override this function to do whatever your class actually does, return false to exit thread early. virtual bool process() = 0; bool isThreaded() const { return _isThreaded; } +public slots: + /// If you're running in non-threaded mode, you must call this regularly + void threadRoutine(); + +signals: + void finished(); + protected: /// Locks all the resources of the thread. @@ -54,9 +57,7 @@ private: bool _stopThread; bool _isThreaded; - pthread_t _thread; + QThread* _thread; }; -extern "C" void* GenericThreadEntry(void* arg); - #endif // __shared__GenericThread__