From 9ded30d62ebbb62cfe35cc01cae4a931529ea2a3 Mon Sep 17 00:00:00 2001 From: Gene Lian Date: Thu, 26 Apr 2012 16:46:52 +0800 Subject: [PATCH] Bug 743064: Shutdown FramebufferWatcher thread gracefully by using it as a pthread. r=cjones a=b2g-only --- widget/gonk/nsWindow.cpp | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/widget/gonk/nsWindow.cpp b/widget/gonk/nsWindow.cpp index 585b6b09ee98..076f063941e6 100644 --- a/widget/gonk/nsWindow.cpp +++ b/widget/gonk/nsWindow.cpp @@ -79,7 +79,7 @@ static android::FramebufferNativeWindow *gNativeWindow = nsnull; static bool sFramebufferOpen; static bool sUsingOMTC; static nsRefPtr sOMTCSurface; -static nsCOMPtr sFramebufferWatchThread; +static pthread_t sFramebufferWatchThread; namespace { @@ -112,17 +112,15 @@ private: static const char* kSleepFile = "/sys/power/wait_for_fb_sleep"; static const char* kWakeFile = "/sys/power/wait_for_fb_wake"; -class FramebufferWatcher : public nsRunnable { -public: - FramebufferWatcher() - : mScreenOnEvent(new ScreenOnOffEvent(true)) - , mScreenOffEvent(new ScreenOnOffEvent(false)) - {} +static void *frameBufferWatcher(void *) { - NS_IMETHOD Run() { - int len = 0; - char buf; + int len = 0; + char buf; + nsRefPtr mScreenOnEvent = new ScreenOnOffEvent(true); + nsRefPtr mScreenOffEvent = new ScreenOnOffEvent(false); + + while (true) { // Cannot use epoll here because kSleepFile and kWakeFile are // always ready to read and blocking. { @@ -142,17 +140,10 @@ public: NS_WARN_IF_FALSE(len >= 0, "WAIT_FOR_FB_WAKE failed"); NS_DispatchToMainThread(mScreenOnEvent); } - - // Dispatch to ourself. - NS_DispatchToCurrentThread(this); - - return NS_OK; } - -private: - nsRefPtr mScreenOnEvent; - nsRefPtr mScreenOffEvent; -}; + + return NULL; +} } // anonymous namespace @@ -162,8 +153,11 @@ nsWindow::nsWindow() // workaround Bug 725143 hal::SetScreenEnabled(true); - // Watching screen on/off state - NS_NewThread(getter_AddRefs(sFramebufferWatchThread), new FramebufferWatcher()); + // Watching screen on/off state by using a pthread + // which implicitly calls exit() when the main thread ends + if (pthread_create(&sFramebufferWatchThread, NULL, frameBufferWatcher, NULL)) { + NS_RUNTIMEABORT("Failed to create framebufferWatcherThread, aborting..."); + } sUsingOMTC = Preferences::GetBool("layers.offmainthreadcomposition.enabled", false);