-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from sintefmath/cdyk_qt_run_functor_as_gui
introduces an invoker object in the HTTP server that handles running functions as the main thread. This is used for grabbing images from opengl and for creating renderlists.
- Loading branch information
Showing
11 changed files
with
331 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include <QObject> | ||
#include <QMutex> | ||
#include <QRunnable> | ||
#include <QWaitCondition> | ||
|
||
namespace tinia { | ||
namespace qtcontroller { | ||
namespace impl { | ||
|
||
|
||
class Invoker : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
|
||
|
||
/** Create a new invoker object. | ||
* \note Must be created in main thread. | ||
*/ | ||
explicit Invoker( QObject* parent ); | ||
|
||
void | ||
invokeInMainThread( QRunnable* function, bool block = true ); | ||
|
||
|
||
signals: | ||
|
||
void | ||
requestMainThreadInvocation( QRunnable* function, bool* i_am_done ); | ||
|
||
private slots: | ||
|
||
void | ||
handleMainThreadInvocation( QRunnable* function, bool *i_am_done ); | ||
|
||
private: | ||
QThread* m_main_thread; | ||
QMutex m_wait_mutex; | ||
|
||
/** Shared wait conditions for all invocations. | ||
* | ||
* This wait condition is shared by all invocations, as the number of | ||
* concurrent invocations is currently assumed to be low. We use a bool | ||
* flag to specify which condition that is true. | ||
* | ||
* Refers to \ref m_wait_mutex. | ||
*/ | ||
QWaitCondition m_wait_condition; | ||
|
||
}; | ||
|
||
} // namespace impl | ||
} // namespace qtcontroller | ||
} // namespace tinia |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <QtGlobal> | ||
#include <QThread> | ||
#include <QMutexLocker> | ||
#include "tinia/qtcontroller/moc/Invoker.hpp" | ||
namespace tinia { | ||
namespace qtcontroller { | ||
namespace impl { | ||
|
||
Invoker::Invoker( QObject* parent ) | ||
: QObject( parent ), | ||
m_main_thread( QThread::currentThread() ) | ||
{ | ||
connect( this, SIGNAL( requestMainThreadInvocation(QRunnable*,bool*)), | ||
this, SLOT( handleMainThreadInvocation(QRunnable*,bool*)) ); | ||
} | ||
|
||
void | ||
Invoker::invokeInMainThread( QRunnable* function, bool block ) | ||
{ | ||
Q_ASSERT( function != NULL ); | ||
if( !block ) { | ||
emit requestMainThreadInvocation( function, NULL ); | ||
} | ||
else { | ||
bool i_am_done = false; // specific condition for this request. | ||
emit requestMainThreadInvocation( function, &i_am_done ); | ||
|
||
// wait for function to complete | ||
QMutexLocker locker( &m_wait_mutex ); | ||
while ( !i_am_done ) { | ||
m_wait_condition.wait( &m_wait_mutex ); | ||
} | ||
} | ||
} | ||
|
||
|
||
void | ||
Invoker::handleMainThreadInvocation( QRunnable* function, bool* i_am_done ) | ||
{ | ||
Q_ASSERT( QThread::currentThread() == m_main_thread ); | ||
function->run(); | ||
|
||
// notify completion if invocation is blocking. | ||
if( i_am_done != NULL ) { | ||
QMutexLocker locker( &m_wait_mutex ); | ||
*i_am_done = true; | ||
m_wait_condition.wakeAll(); | ||
} | ||
} | ||
|
||
|
||
|
||
} // namespace impl | ||
} // namespace qtcontroller | ||
} // namespace tinia |
Oops, something went wrong.