Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 80dbc355 authored by Stan Iliev's avatar Stan Iliev
Browse files

Tell JVM to not wait for HWUI worker threads upon shutdown

RenderThread is setup as a daemon thread, which allows JVM to
exit without waiting on it. This CL does same setup for HWUI
worker threads, which offload work from the RenderThread.
This fixes an issue exposed by Vulkan pipeline, which is pushing
different loads to the worker threads and causing some java tests
to hang on exit. This is not a Vulkan specific issue, because GL
also hangs if worker thread is started.

Bug: 123374538
Test: Ran DismissDialogsInstrumentation test
Change-Id: Ie4ee94737ced975323a0792f57f8426c958e8056
parent 912ca402
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1207,7 +1207,7 @@ static void attachRenderThreadToJvm() {

    JavaVMAttachArgs args;
    args.version = JNI_VERSION_1_4;
    args.name = (char*) "RenderThread";
    args.name = NULL;
    args.group = NULL;
    JNIEnv* env;
    mJvm->AttachCurrentThreadAsDaemon(&env, (void*) &args);
+6 −2
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanosecond

static bool gHasRenderThreadInstance = false;

static void (*gOnStartHook)() = nullptr;
static JVMAttachHook gOnStartHook = nullptr;

class DisplayEventReceiverWrapper : public VsyncSource {
public:
@@ -111,11 +111,15 @@ bool RenderThread::hasInstance() {
    return gHasRenderThreadInstance;
}

void RenderThread::setOnStartHook(void (*onStartHook)()) {
void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
    LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
    gOnStartHook = onStartHook;
}

JVMAttachHook RenderThread::getOnStartHook() {
    return gOnStartHook;
}

RenderThread& RenderThread::getInstance() {
    // This is a pointer because otherwise __cxa_finalize
    // will try to delete it like a Good Citizen but that causes us to crash
+4 −1
Original line number Diff line number Diff line
@@ -75,12 +75,15 @@ struct VsyncSource {

class DummyVsyncSource;

typedef void (*JVMAttachHook)();

class RenderThread : private ThreadBase {
    PREVENT_COPY_AND_ASSIGN(RenderThread);

public:
    // Sets a callback that fires before any RenderThread setup has occurred.
    ANDROID_API static void setOnStartHook(void (*onStartHook)());
    ANDROID_API static void setOnStartHook(JVMAttachHook onStartHook);
    static JVMAttachHook getOnStartHook();

    WorkQueue& queue() { return ThreadBase::queue(); }

+6 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include "TaskManager.h"
#include "TaskProcessor.h"
#include "utils/MathUtils.h"
#include "renderthread/RenderThread.h"

namespace android {
namespace uirenderer {
@@ -84,6 +85,11 @@ bool TaskManager::addTaskBase(const sp<TaskBase>& task, const sp<TaskProcessorBa

status_t TaskManager::WorkerThread::readyToRun() {
    setpriority(PRIO_PROCESS, 0, PRIORITY_FOREGROUND);
    auto onStartHook = renderthread::RenderThread::getOnStartHook();
    if (onStartHook) {
        onStartHook();
    }

    return NO_ERROR;
}

+2 −1
Original line number Diff line number Diff line
@@ -77,7 +77,8 @@ private:

    class WorkerThread : public Thread {
    public:
        explicit WorkerThread(const String8& name) : mSignal(Condition::WAKE_UP_ONE), mName(name) {}
        explicit WorkerThread(const String8& name)
                : Thread(false), mSignal(Condition::WAKE_UP_ONE), mName(name) {}

        bool addTask(const TaskWrapper& task);
        size_t getTaskCount() const;