From c9dc3ae6fd924a0a1e29325d87edcd8d3316c0ad Mon Sep 17 00:00:00 2001 From: Brian Nicholson Date: Tue, 1 Oct 2013 12:55:00 -0700 Subject: [PATCH] Bug 897162 - Lower Gecko thread priority when showing Top Sites page. r=lucasr --- mobile/android/base/home/TopSitesPage.java | 15 +++++++++ mobile/android/base/util/ThreadUtils.java | 36 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/mobile/android/base/home/TopSitesPage.java b/mobile/android/base/home/TopSitesPage.java index af0a638785b..04c38e206f1 100644 --- a/mobile/android/base/home/TopSitesPage.java +++ b/mobile/android/base/home/TopSitesPage.java @@ -108,6 +108,9 @@ public class TopSitesPage extends HomeFragment { // Max number of entries shown in the grid from the cursor. private int mMaxGridEntries; + // Time in ms until the Gecko thread is reset to normal priority. + private static final long PRIORITY_RESET_TIMEOUT = 10000; + /** * Class to hold the bitmap of cached thumbnails/favicons. */ @@ -359,6 +362,14 @@ public class TopSitesPage extends HomeFragment { @Override protected void load() { getLoaderManager().initLoader(LOADER_ID_TOP_SITES, null, mCursorLoaderCallbacks); + + // Since this is the primary fragment that loads whenever about:home is + // visited, we want to load it as quickly as possible. Heavy load on + // the Gecko thread can slow down the time it takes for thumbnails to + // appear, especially during startup (bug 897162). By minimizing the + // Gecko thread priority, we ensure that the UI appears quickly. The + // priority is reset to normal once thumbnails are loaded. + ThreadUtils.reduceGeckoPriority(PRIORITY_RESET_TIMEOUT); } /** @@ -769,6 +780,10 @@ public class TopSitesPage extends HomeFragment { if (mGridAdapter != null) { mGridAdapter.updateThumbnails(thumbnails); } + + // Once thumbnails have finished loading, the UI is ready. Reset + // Gecko to normal priority. + ThreadUtils.resetGeckoPriority(); } @Override diff --git a/mobile/android/base/util/ThreadUtils.java b/mobile/android/base/util/ThreadUtils.java index 11416567611..dc9a1d3600a 100644 --- a/mobile/android/base/util/ThreadUtils.java +++ b/mobile/android/base/util/ThreadUtils.java @@ -27,6 +27,9 @@ public final class ThreadUtils { public static MessageQueue sGeckoQueue; public static Thread sGeckoThread; + // Delayed Runnable that resets the Gecko thread priority. + private static volatile Runnable sPriorityResetRunnable; + @SuppressWarnings("serial") public static class UiThreadBlockedException extends RuntimeException { public UiThreadBlockedException() { @@ -127,4 +130,37 @@ public final class ThreadUtils { public static boolean isOnThread(Thread thread) { return (Thread.currentThread().getId() == thread.getId()); } + + /** + * Reduces the priority of the Gecko thread, allowing other operations + * (such as those related to the UI and database) to take precedence. + * + * Note that there are no guards in place to prevent multiple calls + * to this method from conflicting with each other. + * + * @param timeout Timeout in ms after which the priority will be reset + */ + public static void reduceGeckoPriority(long timeout) { + sGeckoThread.setPriority(Thread.MIN_PRIORITY); + sPriorityResetRunnable = new Runnable() { + @Override + public void run() { + resetGeckoPriority(); + } + }; + getUiHandler().postDelayed(sPriorityResetRunnable, timeout); + } + + /** + * Resets the priority of a thread whose priority has been reduced + * by reduceGeckoPriority. + */ + public static void resetGeckoPriority() { + if (sPriorityResetRunnable != null) { + sGeckoThread.setPriority(Thread.NORM_PRIORITY); + + getUiHandler().removeCallbacks(sPriorityResetRunnable); + sPriorityResetRunnable = null; + } + } }