Skip to content

Android:Make MessageQueueThreadImpl startNewBackgroundThread more eff… #14116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

package com.facebook.react.bridge.queue;

import android.os.Looper;
import android.os.Process;

class MessageQueueHandlerThread extends Thread {
private Looper mLooper;

protected MessageQueueHandlerThread(String name, long stackSize) {
super(null, null, name, stackSize);
}

@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Looper.loop();
}


public Looper getLooper() {
if (!isAlive()) {
return null;
}

synchronized (this) {
while (isAlive() && mLooper == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
return mLooper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,11 @@ public void run() {
* When this method exits, the new MessageQueueThreadImpl is ready to receive events.
*/
private static MessageQueueThreadImpl startNewBackgroundThread(
final String name,
long stackSize,
QueueThreadExceptionHandler exceptionHandler) {
final SimpleSettableFuture<Looper> looperFuture = new SimpleSettableFuture<>();
Thread bgThread = new Thread(null,
new Runnable() {
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did this go?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually quite important to prioritize bridge startup. I believe we have some changes that reduce the priority after startup has completed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final SimpleSettableFuture<Looper> looperFuture = new SimpleSettableFuture<>();
final SimpleSettableFuture<MessageQueueThread> mqtFuture = new SimpleSettableFuture<>();

Doing so for additional performance overhead.

Looper.prepare();

looperFuture.set(Looper.myLooper());
Looper.loop();
}
}, "mqt_" + name, stackSize);
bgThread.start();

Looper myLooper = looperFuture.getOrThrow();
return new MessageQueueThreadImpl(name, myLooper, exceptionHandler);
final String name,
long stackSize,
QueueThreadExceptionHandler exceptionHandler) {
MessageQueueHandlerThread messageQueueHandlerThread = new MessageQueueHandlerThread("mqt_" + name, stackSize);
messageQueueHandlerThread.start();
return new MessageQueueThreadImpl(name, messageQueueHandlerThread.getLooper(), exceptionHandler);
}
}