From 1fe8a708cbdb957be55b6162c5379102a46b3ed8 Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Fri, 3 Oct 2014 14:24:40 +0200 Subject: [PATCH] Android: deadlock avoidance This change adds deadlock protection to all places where we lock one thread while waiting for the other to do something. If we detect that the other thread is going to block, we abort the operation. This could cause unexpected problems, such as painting errors, text input errors, or even crashes, but the alternative is a guaranteed deadlock. Task-number: QTBUG-41369 Change-Id: I2627a955cfafc4bce54eb9d0d38e19b768b06956 Reviewed-by: Christian Stromme --- src/plugins/platforms/android/android.pro | 2 + .../android/androiddeadlockprotector.cpp | 37 ++++++++++ .../android/androiddeadlockprotector.h | 67 +++++++++++++++++++ .../android/qandroidinputcontext.cpp | 11 ++- .../android/qandroidplatformopenglwindow.cpp | 5 ++ .../android/qandroidplatformscreen.cpp | 4 ++ 6 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/plugins/platforms/android/androiddeadlockprotector.cpp create mode 100644 src/plugins/platforms/android/androiddeadlockprotector.h diff --git a/src/plugins/platforms/android/android.pro b/src/plugins/platforms/android/android.pro index f55bc40a55e..3c3a4b4b2ed 100644 --- a/src/plugins/platforms/android/android.pro +++ b/src/plugins/platforms/android/android.pro @@ -26,6 +26,7 @@ INCLUDEPATH += \ $$QT_SOURCE_TREE/src/3rdparty/android SOURCES += $$PWD/androidplatformplugin.cpp \ + $$PWD/androiddeadlockprotector.cpp \ $$PWD/androidjnimain.cpp \ $$PWD/androidjniaccessibility.cpp \ $$PWD/androidjniinput.cpp \ @@ -53,6 +54,7 @@ SOURCES += $$PWD/androidplatformplugin.cpp \ $$PWD/qandroideventdispatcher.cpp HEADERS += $$PWD/qandroidplatformintegration.h \ + $$PWD/androidandroiddeadlockprotector.h \ $$PWD/androidjnimain.h \ $$PWD/androidjniaccessibility.h \ $$PWD/androidjniinput.h \ diff --git a/src/plugins/platforms/android/androiddeadlockprotector.cpp b/src/plugins/platforms/android/androiddeadlockprotector.cpp new file mode 100644 index 00000000000..e53e0c2447e --- /dev/null +++ b/src/plugins/platforms/android/androiddeadlockprotector.cpp @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "androiddeadlockprotector.h" + +QAtomicInt AndroidDeadlockProtector::s_blocked(0); + diff --git a/src/plugins/platforms/android/androiddeadlockprotector.h b/src/plugins/platforms/android/androiddeadlockprotector.h new file mode 100644 index 00000000000..a0a82aa9d1b --- /dev/null +++ b/src/plugins/platforms/android/androiddeadlockprotector.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ANDROID_DEADLOCKPROTECTOR_H +#define ANDROID_DEADLOCKPROTECTOR_H + +#include + +QT_BEGIN_NAMESPACE + +class AndroidDeadlockProtector +{ +public: + AndroidDeadlockProtector() + : m_acquired(0) + { + } + + ~AndroidDeadlockProtector() { + if (m_acquired) + s_blocked.storeRelease(0); + } + + bool acquire() { + m_acquired = s_blocked.testAndSetAcquire(0, 1); + return m_acquired; + } + +private: + static QAtomicInt s_blocked; + int m_acquired; +}; + +QT_END_NAMESPACE + +#endif // ANDROID_DEADLOCKPROTECTOR_H + diff --git a/src/plugins/platforms/android/qandroidinputcontext.cpp b/src/plugins/platforms/android/qandroidinputcontext.cpp index e3d488b9583..a3848c9c2b1 100644 --- a/src/plugins/platforms/android/qandroidinputcontext.cpp +++ b/src/plugins/platforms/android/qandroidinputcontext.cpp @@ -38,6 +38,7 @@ #include "androidjnimain.h" #include "androidjniinput.h" #include "qandroideventdispatcher.h" +#include "androiddeadlockprotector.h" #include #include #include @@ -998,6 +999,9 @@ QVariant QAndroidInputContext::queryFocusObjectThreadSafe(Qt::InputMethodQuery q const bool inMainThread = qGuiApp->thread() == QThread::currentThread(); if (QAndroidEventDispatcherStopper::stopped() && !inMainThread) return retval; + AndroidDeadlockProtector protector; + if (!inMainThread && !protector.acquire()) + return retval; QMetaObject::invokeMethod(this, "queryFocusObjectUnsafe", inMainThread ? Qt::DirectConnection : Qt::BlockingQueuedConnection, @@ -1015,6 +1019,9 @@ QSharedPointer QAndroidInputContext::focusObjectInputMet const bool inMainThread = qGuiApp->thread() == QThread::currentThread(); if (QAndroidEventDispatcherStopper::stopped() && !inMainThread) return QSharedPointer(); + AndroidDeadlockProtector protector; + if (!inMainThread && !protector.acquire()) + return QSharedPointer(); QInputMethodQueryEvent *queryEvent = 0; QMetaObject::invokeMethod(this, "focusObjectInputMethodQueryUnsafe", @@ -1052,7 +1059,9 @@ void QAndroidInputContext::sendInputMethodEventThreadSafe(QInputMethodEvent *eve const bool inMainThread = qGuiApp->thread() == QThread::currentThread(); if (QAndroidEventDispatcherStopper::stopped() && !inMainThread) return; - + AndroidDeadlockProtector protector; + if (!inMainThread && !protector.acquire()) + return; QMetaObject::invokeMethod(this, "sendInputMethodEventUnsafe", inMainThread ? Qt::DirectConnection : Qt::BlockingQueuedConnection, Q_ARG(QInputMethodEvent*, event)); diff --git a/src/plugins/platforms/android/qandroidplatformopenglwindow.cpp b/src/plugins/platforms/android/qandroidplatformopenglwindow.cpp index 8afc35c57a1..8dc8e84f0ae 100644 --- a/src/plugins/platforms/android/qandroidplatformopenglwindow.cpp +++ b/src/plugins/platforms/android/qandroidplatformopenglwindow.cpp @@ -37,6 +37,7 @@ #include "qandroidplatformscreen.h" #include "androidjnimain.h" #include "qandroideventdispatcher.h" +#include "androiddeadlockprotector.h" #include #include @@ -120,6 +121,10 @@ EGLSurface QAndroidPlatformOpenGLWindow::eglSurface(EGLConfig config) QMutexLocker lock(&m_surfaceMutex); if (m_nativeSurfaceId == -1) { + AndroidDeadlockProtector protector; + if (!protector.acquire()) + return m_eglSurface; + const bool windowStaysOnTop = bool(window()->flags() & Qt::WindowStaysOnTopHint); m_nativeSurfaceId = QtAndroid::createSurface(this, geometry(), windowStaysOnTop, 32); m_surfaceWaitCondition.wait(&m_surfaceMutex); diff --git a/src/plugins/platforms/android/qandroidplatformscreen.cpp b/src/plugins/platforms/android/qandroidplatformscreen.cpp index 1775b442c49..684ae64703f 100644 --- a/src/plugins/platforms/android/qandroidplatformscreen.cpp +++ b/src/plugins/platforms/android/qandroidplatformscreen.cpp @@ -43,6 +43,7 @@ #include "qandroidplatformwindow.h" #include "androidjnimain.h" #include "androidjnimenu.h" +#include "androiddeadlockprotector.h" #include #include @@ -299,6 +300,9 @@ void QAndroidPlatformScreen::doRedraw() QMutexLocker lock(&m_surfaceMutex); if (m_id == -1 && m_rasterSurfaces) { m_id = QtAndroid::createSurface(this, m_availableGeometry, true, m_depth); + AndroidDeadlockProtector protector; + if (!protector.acquire()) + return; m_surfaceWaitCondition.wait(&m_surfaceMutex); }