Commit e5a5666c authored by Clinton Stimpson's avatar Clinton Stimpson

BUG: Fix a race condition. Hopefully fix the timeouts on some tests.

parent a4eb74c9
......@@ -47,7 +47,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <QVariant>
#include <QFile>
#include <QtDebug>
#include <QMutex>
#include <QCoreApplication>
#include <QEvent>
#include <QStringList>
......@@ -120,10 +119,8 @@ QtTesting_getProperty(PyObject* /*self*/, PyObject* args)
if(Instance && QThread::currentThread() != QApplication::instance()->thread())
{
QMutex mut;
mut.lock();
QMetaObject::invokeMethod(Instance, "threadGetProperty", Qt::QueuedConnection);
if(!Instance->waitForGUI(mut))
if(!Instance->waitForGUI())
{
PyErr_SetString(PyExc_ValueError, "error getting property");
return NULL;
......@@ -171,10 +168,8 @@ QtTesting_setProperty(PyObject* /*self*/, PyObject* args)
if(Instance && QThread::currentThread() != QApplication::instance()->thread())
{
QMutex mut;
mut.lock();
QMetaObject::invokeMethod(Instance, "threadSetProperty", Qt::QueuedConnection);
if(!Instance->waitForGUI(mut))
if(!Instance->waitForGUI())
{
PyErr_SetString(PyExc_ValueError, "error setting property");
return NULL;
......@@ -253,10 +248,8 @@ QtTesting_getChildren(PyObject* /*self*/, PyObject* args)
if(Instance && QThread::currentThread() != QApplication::instance()->thread())
{
QMutex mut;
mut.lock();
QMetaObject::invokeMethod(Instance, "threadGetChildren", Qt::QueuedConnection);
if(!Instance->waitForGUI(mut))
if(!Instance->waitForGUI())
{
PyErr_SetString(PyExc_ValueError, "error getting children");
return NULL;
......@@ -305,10 +298,8 @@ QtTesting_invokeMethod(PyObject* /*self*/, PyObject* args)
if(Instance && QThread::currentThread() != QApplication::instance()->thread())
{
QMutex mut;
mut.lock();
QMetaObject::invokeMethod(Instance, "threadInvokeMethod", Qt::QueuedConnection);
if(!Instance->waitForGUI(mut))
if(!Instance->waitForGUI())
{
PyErr_SetString(PyExc_ValueError, "error invoking method");
return NULL;
......
......@@ -59,12 +59,22 @@ public:
pqThreadedEventSource& Source;
QWaitCondition WaitCondition;
QAtomic Waiting;
QAtomic ShouldStop;
QAtomic GotEvent;
QString CurrentObject;
QString CurrentCommand;
QString CurrentArgument;
class ThreadHelper : public QThread
{
public:
static void msleep(int msecs)
{
QThread::msleep(msecs);
}
};
};
pqThreadedEventSource::pqThreadedEventSource(QObject* p)
......@@ -125,14 +135,12 @@ bool pqThreadedEventSource::postNextEvent(const QString& object,
const QString& command,
const QString& argument)
{
QMutex mut;
mut.lock();
QMetaObject::invokeMethod(this, "relayEvent", Qt::QueuedConnection,
Q_ARG(QString, object),
Q_ARG(QString, command),
Q_ARG(QString, argument));
return this->waitForGUI(mut);
return this->waitForGUI();
}
void pqThreadedEventSource::start()
......@@ -143,22 +151,31 @@ void pqThreadedEventSource::start()
void pqThreadedEventSource::stop()
{
this->Internal->ShouldStop = 1;
this->guiAcknowledge();
}
bool pqThreadedEventSource::waitForGUI(QMutex& m)
bool pqThreadedEventSource::waitForGUI()
{
if(this->Internal->ShouldStop)
this->Internal->Waiting = true;
while(this->Internal->Waiting == true &&
!this->Internal->ShouldStop)
{
return false;
pqInternal::ThreadHelper::msleep(50);
}
this->Internal->WaitCondition.wait(&m);
this->Internal->Waiting = false;
return !this->Internal->ShouldStop;
}
void pqThreadedEventSource::guiAcknowledge()
{
this->Internal->WaitCondition.wakeAll();
while(this->Internal->Waiting == false)
{
pqInternal::ThreadHelper::msleep(10);
}
this->Internal->Waiting = false;
}
void pqThreadedEventSource::done(int success)
......
......@@ -75,7 +75,7 @@ public:
For use by the testing thread.
If return value is false, an error occurred and
the testing thread should terminate. */
bool waitForGUI(QMutex& m);
bool waitForGUI();
/** Give the testing thread an acknowledgement.
For use by the GUI thread */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment