summaryrefslogtreecommitdiffstats
path: root/src/kernel/qthread_unix.cpp
diff options
context:
space:
mode:
authorTimothy Pearson <kb9vqf@pearsoncomputing.net>2012-12-06 16:47:27 -0600
committerTimothy Pearson <kb9vqf@pearsoncomputing.net>2012-12-06 16:47:27 -0600
commit78125ea2f051107b84fdc0354acdedb7885308ee (patch)
treec162d7f55467f81a01e2e669ce297acce06b3947 /src/kernel/qthread_unix.cpp
parent7aa5ac7f0e76c5b87e4ca837b75b3edd522a3372 (diff)
downloadqt3-78125ea2f051107b84fdc0354acdedb7885308ee.tar.gz
qt3-78125ea2f051107b84fdc0354acdedb7885308ee.zip
Add real threading support, including per-thread event loops, to QThread
Diffstat (limited to 'src/kernel/qthread_unix.cpp')
-rw-r--r--src/kernel/qthread_unix.cpp46
1 files changed, 30 insertions, 16 deletions
diff --git a/src/kernel/qthread_unix.cpp b/src/kernel/qthread_unix.cpp
index e4d6625..52b070e 100644
--- a/src/kernel/qthread_unix.cpp
+++ b/src/kernel/qthread_unix.cpp
@@ -52,11 +52,6 @@ typedef pthread_mutex_t Q_MUTEX_T;
#include <sched.h>
-static QThreadInstance main_instance = {
- 0, { 0, &main_instance }, 0, 0, 1, 0, PTHREAD_COND_INITIALIZER, 0
-};
-
-
static QMutexPool *qt_thread_mutexpool = 0;
@@ -82,10 +77,20 @@ static void create_storage_key()
** QThreadInstance
*************************************************************************/
+void QThreadInstance::setCurrentThread(QThread *thread)
+{
+ pthread_once(&storage_key_once, create_storage_key);
+ pthread_setspecific(storage_key, thread);
+}
+
QThreadInstance *QThreadInstance::current()
{
+ QThreadInstance *ret = NULL;
pthread_once( &storage_key_once, create_storage_key );
- QThreadInstance *ret = (QThreadInstance *) pthread_getspecific( storage_key );
+ QThread *thread = (QThread *) pthread_getspecific( storage_key );
+ if (thread) {
+ ret = thread->d;
+ }
return ret;
}
@@ -101,6 +106,8 @@ void QThreadInstance::init(unsigned int stackSize)
pthread_cond_init(&thread_done, NULL);
thread_id = 0;
+ eventLoop = 0;
+
// threads have not been initialized yet, do it now
if (! qt_thread_mutexpool) QThread::initialize();
}
@@ -114,8 +121,8 @@ void *QThreadInstance::start( void *_arg )
{
void **arg = (void **) _arg;
- pthread_once( &storage_key_once, create_storage_key );
- pthread_setspecific( storage_key, arg[1] );
+ setCurrentThread( (QThread *) arg[0] );
+
pthread_cleanup_push( QThreadInstance::finish, arg[1] );
pthread_testcancel();
@@ -192,9 +199,6 @@ void QThread::initialize()
qt_global_mutexpool = new QMutexPool( TRUE, 73 );
if ( ! qt_thread_mutexpool )
qt_thread_mutexpool = new QMutexPool( FALSE, 127 );
-
- pthread_once( &storage_key_once, create_storage_key );
- pthread_setspecific( storage_key, &main_instance );
}
/*! \internal
@@ -206,11 +210,6 @@ void QThread::cleanup()
delete qt_thread_mutexpool;
qt_global_mutexpool = 0;
qt_thread_mutexpool = 0;
-
- QThreadInstance::finish(&main_instance);
-
- pthread_once( &storage_key_once, create_storage_key );
- pthread_setspecific( storage_key, 0 );
}
/*!
@@ -470,5 +469,20 @@ bool QThread::wait( unsigned long time )
return (ret == 0);
}
+/*!
+ Returns a pointer to the currently executing QThread. If the
+ current thread was not started using the QThread API, this
+ function returns zero.
+
+ Note that QApplication creates a QThread object to represent the
+ main thread; calling this function from main() after creating
+ QApplication will return a valid pointer.
+*/
+QThread *QThread::currentThreadObject()
+{
+ pthread_once(&storage_key_once, create_storage_key);
+ return reinterpret_cast<QThread *>(pthread_getspecific(storage_key));
+}
+
#endif // QT_THREAD_SUPPORT