mirror of
https://github.com/genodelabs/genode.git
synced 2025-01-30 16:14:13 +00:00
Qt4: fix Genode thread deletion
With this patch the Genode thread used by the QThread class gets deleted when 'QThread::wait()' gets called and the timeout didn't trigger or when 'QThread::terminate()' gets called. Fixes #193.
This commit is contained in:
parent
c3fcd834b0
commit
b448a67151
@ -870,7 +870,7 @@ diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h
|
||||
#include "qplatformdefs.h"
|
||||
#include "QtCore/qthread.h"
|
||||
#include "QtCore/qmutex.h"
|
||||
@@ -136,6 +140,39 @@
|
||||
@@ -136,6 +140,56 @@
|
||||
static QThread *threadForId(int id);
|
||||
|
||||
#ifdef Q_OS_UNIX
|
||||
@ -878,17 +878,34 @@ diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h
|
||||
+
|
||||
+ class Genode_thread : public Genode::Thread_qt
|
||||
+ {
|
||||
+ private:
|
||||
+
|
||||
+ /*
|
||||
+ * The '_finished_lock' is necessary because 'QThreadPrivate::mutex'
|
||||
+ * uses a 'Genode::Timed_semaphore' internally and it isn't safe
|
||||
+ * to delete a Genode thread that just called 'Semaphore::up()',
|
||||
+ * because the 'Semaphore::_meta_lock' could still be locked.
|
||||
+ */
|
||||
+ Genode::Lock _finished_lock;
|
||||
+ QThread *_qthread;
|
||||
+
|
||||
+ public:
|
||||
+ Genode_thread(QThread *qthread) : _qthread(qthread) { }
|
||||
+
|
||||
+ Genode_thread(QThread *qthread)
|
||||
+ : _finished_lock(Genode::Lock::LOCKED),
|
||||
+ _qthread(qthread) { }
|
||||
+
|
||||
+ virtual void entry()
|
||||
+ {
|
||||
+ QThreadPrivate::start(_qthread);
|
||||
+ QThreadPrivate::finish(_qthread);
|
||||
+ _finished_lock.unlock();
|
||||
+ }
|
||||
+
|
||||
+ private:
|
||||
+ QThread *_qthread;
|
||||
+ void join()
|
||||
+ {
|
||||
+ _finished_lock.lock();
|
||||
+ }
|
||||
+ };
|
||||
+
|
||||
+ Genode_thread *genode_thread;
|
||||
@ -910,7 +927,7 @@ diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h
|
||||
pthread_t thread_id;
|
||||
QWaitCondition thread_done;
|
||||
|
||||
@@ -145,6 +182,7 @@
|
||||
@@ -145,6 +199,7 @@
|
||||
#else
|
||||
static void finish(void *);
|
||||
#endif
|
||||
|
@ -337,14 +337,28 @@ void QThread::terminate()
|
||||
QMutexLocker locker(&d->mutex);
|
||||
|
||||
if (QThreadPrivate::tls.value(QThread::currentThreadId()).termination_enabled) {
|
||||
|
||||
if (d->genode_thread) {
|
||||
delete d->genode_thread;
|
||||
d->genode_thread = 0;
|
||||
}
|
||||
|
||||
d->terminated = true;
|
||||
d->running = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static inline void join_and_delete_genode_thread(QThreadPrivate *d)
|
||||
{
|
||||
if (d->genode_thread) {
|
||||
d->genode_thread->join();
|
||||
delete d->genode_thread;
|
||||
d->genode_thread = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
Blocks the thread until either of these conditions is met:
|
||||
|
||||
@ -374,14 +388,18 @@ bool QThread::wait(unsigned long time)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (d->finished || !d->running)
|
||||
if (d->finished || !d->running) {
|
||||
join_and_delete_genode_thread(d);
|
||||
return true;
|
||||
}
|
||||
|
||||
while (d->running) {
|
||||
if (!d->thread_done.wait(locker.mutex(), time))
|
||||
return false;
|
||||
}
|
||||
|
||||
join_and_delete_genode_thread(d);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -251,10 +251,10 @@ bool QWaitCondition::wait(QMutex *mutex, unsigned long time)
|
||||
} else {
|
||||
try {
|
||||
d->sem.down((Genode::Alarm::Time) time);
|
||||
result = true;
|
||||
} catch (Genode::Timeout_exception) {
|
||||
return false;
|
||||
result = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
mutex->lock();
|
||||
|
Loading…
x
Reference in New Issue
Block a user